codama_nodes/shared/
docs.rs1use serde::{Deserialize, Serialize};
2use std::ops::{Deref, DerefMut, Index, IndexMut};
3
4#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
5pub struct Docs(Vec<String>);
6
7impl Docs {
8 pub fn new() -> Self {
9 Self::default()
10 }
11
12 pub fn add_doc<T: Into<String>>(mut self, value: T) -> Self {
13 self.0.push(value.into());
14 self
15 }
16
17 pub fn push<T: Into<String>>(&mut self, value: T) {
18 self.0.push(value.into());
19 }
20
21 pub fn is_empty(&self) -> bool {
22 self.0.is_empty()
23 }
24}
25
26impl From<Vec<String>> for Docs {
27 fn from(vec: Vec<String>) -> Self {
28 Self(vec)
29 }
30}
31
32impl Deref for Docs {
33 type Target = Vec<String>;
34
35 fn deref(&self) -> &Self::Target {
36 &self.0
37 }
38}
39
40impl DerefMut for Docs {
41 fn deref_mut(&mut self) -> &mut Self::Target {
42 &mut self.0
43 }
44}
45
46impl AsRef<[String]> for Docs {
47 fn as_ref(&self) -> &[String] {
48 &self.0
49 }
50}
51
52impl AsMut<[String]> for Docs {
53 fn as_mut(&mut self) -> &mut [String] {
54 &mut self.0
55 }
56}
57
58impl Index<usize> for Docs {
59 type Output = String;
60
61 fn index(&self, index: usize) -> &Self::Output {
62 &self.0[index]
63 }
64}
65
66impl IndexMut<usize> for Docs {
67 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
68 &mut self.0[index]
69 }
70}
71
72#[cfg(test)]
73mod tests {
74 use super::*;
75
76 #[test]
77 fn new() {
78 let docs = Docs::new();
79 assert_eq!(docs.0, Vec::<String>::new());
80 }
81
82 #[test]
83 fn new_and_push() {
84 let mut docs = Docs::new();
85 docs.push("Hello");
86 docs.push(String::from("World"));
87 docs.push('!');
88 assert_eq!(
89 docs.0,
90 vec!["Hello".to_string(), "World".to_string(), "!".to_string()]
91 );
92 }
93
94 #[test]
95 fn from_vec() {
96 let docs: Docs = vec!["Hello".to_string(), "World".to_string()].into();
97 assert_eq!(docs.0, vec!["Hello".to_string(), "World".to_string()]);
98 }
99}