tekton/models/
friendly.rs

1//! Structures to model the JSON snippet format
2
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6/// A custom type to shorten function signatures
7pub type Table = HashMap<String, FriendlySnippetBody>;
8
9/// The Struct representing the JSON file of snippets
10#[derive(Debug, Serialize, Deserialize)]
11pub struct FriendlySnippets {
12    /// The hashmap (table) that represents the source snippet file
13    #[serde(flatten)]
14    pub snippets: Table,
15}
16
17impl FriendlySnippets {
18    pub fn new() -> Self {
19        Self {
20            snippets: HashMap::new(),
21        }
22    }
23}
24
25impl Default for FriendlySnippets {
26    fn default() -> Self {
27        Self::new()
28    }
29}
30
31/// A struct representing the body of a snippet from the `FriendlySnippets` hashmap
32#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
33pub struct FriendlySnippetBody {
34    /// The trigger for the snippet
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub prefix: Option<String>,
37    /// The 'snippet' contents
38    pub body: Vec<String>,
39    /// An optional description explaining the snippet
40    #[serde(skip_serializing_if = "Option::is_none")]
41    pub description: Option<String>,
42}
43
44impl FriendlySnippetBody {
45    pub fn new(
46        prefix: Option<String>,
47        body: Vec<String>,
48        description: Option<String>,
49    ) -> FriendlySnippetBody {
50        FriendlySnippetBody {
51            prefix,
52            body,
53            description,
54        }
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61    #[test]
62    fn test_snippet_body_creation() {
63        let body = FriendlySnippetBody::new(
64            Some("snip".to_string()),
65            Vec::new(),
66            Some("Description".to_string()),
67        );
68        assert_eq!(body.prefix, Some("snip".to_string()));
69        assert_eq!(body.body.len(), 0);
70    }
71
72    #[test]
73    fn test_friendly_snippets() {
74        let mut hp: FriendlySnippets = FriendlySnippets {
75            snippets: HashMap::new(),
76        };
77        let body = FriendlySnippetBody::new(
78            Some("snip".to_string()),
79            Vec::new(),
80            Some("Description".to_string()),
81        );
82        let expected_body = FriendlySnippetBody::new(
83            Some("snip".to_string()),
84            Vec::new(),
85            Some("Description".to_string()),
86        );
87        hp.snippets.insert("test".to_string(), body);
88        assert_eq!(
89            hp.snippets.get(&"test".to_string()).unwrap(),
90            &expected_body
91        );
92    }
93
94    #[test]
95    fn friendly_description_is_none() {
96        let mut hp: FriendlySnippets = FriendlySnippets {
97            snippets: HashMap::new(),
98        };
99        let body = FriendlySnippetBody::new(Some("snip".to_string()), Vec::new(), None);
100        let expected_body = FriendlySnippetBody::new(Some("snip".to_string()), Vec::new(), None);
101        hp.snippets.insert("test".to_string(), body);
102        assert_eq!(
103            hp.snippets.get(&"test".to_string()).unwrap(),
104            &expected_body
105        );
106    }
107}