tekton/models/
friendly.rs1use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6pub type Table = HashMap<String, FriendlySnippetBody>;
8
9#[derive(Debug, Serialize, Deserialize)]
11pub struct FriendlySnippets {
12 #[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#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
33pub struct FriendlySnippetBody {
34 #[serde(skip_serializing_if = "Option::is_none")]
36 pub prefix: Option<String>,
37 pub body: Vec<String>,
39 #[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}