1use serde::Deserialize;
10use serde_json::Value;
11
12const RAW: &str = include_str!("../../secret-templates.json");
13
14#[derive(Debug, Clone, Deserialize)]
15pub struct Template {
16 pub id: String,
17 pub name: String,
18 pub icon: String,
19 pub category: String,
20 #[serde(rename = "secretType")]
21 pub secret_type: String,
22 pub defaults: serde_json::Map<String, Value>,
24 #[serde(rename = "requiredFields", default)]
25 pub required_fields: Vec<String>,
26 #[serde(default)]
27 pub hints: serde_json::Map<String, Value>,
28}
29
30#[derive(Deserialize)]
31struct File {
32 templates: Vec<Template>,
33}
34
35pub fn list() -> Vec<Template> {
36 serde_json::from_str::<File>(RAW)
37 .expect("secret-templates.json failed to parse: a build-time defect")
38 .templates
39}
40
41pub fn find(id: &str) -> Option<Template> {
42 list().into_iter().find(|t| t.id.eq_ignore_ascii_case(id))
43}
44
45#[cfg(test)]
46mod tests {
47 use super::*;
48
49 #[test]
50 fn every_template_parses_and_has_a_known_type() {
51 let all = list();
52 assert!(all.len() >= 10);
53 for t in &all {
54 assert!(
55 crate::secret_types::find(&t.secret_type).is_some(),
56 "{}: unknown secretType {}",
57 t.id,
58 t.secret_type
59 );
60 assert!(!t.defaults.is_empty(), "{} pre-fills nothing", t.id);
61 }
62 let mut ids: Vec<_> = all.iter().map(|t| t.id.clone()).collect();
63 ids.sort();
64 ids.dedup();
65 assert_eq!(ids.len(), all.len(), "duplicate template id");
66 }
67
68 #[test]
69 fn find_is_case_insensitive() {
70 assert_eq!(find("GitHub-PAT").unwrap().id, "github-pat");
71 assert!(find("nope").is_none());
72 }
73}