zod_core/
docs.rs

1//! Generate the type-table.md
2
3#[derive(Debug, Clone)]
4struct T;
5
6#[derive(Debug, Clone)]
7struct U;
8
9impl crate::ZodType for T {
10    fn schema() -> String {
11        String::from("T")
12    }
13
14    fn type_def() -> crate::TsTypeDef {
15        crate::TsTypeDef::Type(String::from("T"))
16    }
17}
18
19impl crate::ZodType for U {
20    fn schema() -> String {
21        String::from("U")
22    }
23
24    fn type_def() -> crate::TsTypeDef {
25        crate::TsTypeDef::Type(String::from("U"))
26    }
27}
28
29type E = U;
30
31pub fn generate() -> String {
32    let lines = [
33        Line::with_link::<()>("()", "https://doc.rust-lang.org/std/primitive.unit.html"),
34        Line::new::<bool>(),
35        Line::new::<char>(),
36        Line::with_link::<Option<T>>("Option<T>", "Option"),
37        Line::with_link::<Result<T, E>>("Result<T, E>", "Result"),
38        Line::with_link::<Vec<T>>("Vec<T>", "Vec"),
39        Line::new::<&'static str>(),
40        Line::with_link::<String>("String", None),
41        Line::new::<usize>(),
42        Line::new::<u8>(),
43        Line::new::<u16>(),
44        Line::new::<u32>(),
45        Line::new::<u64>(),
46        Line::new::<u128>(),
47        Line::new::<i8>(),
48        Line::new::<i16>(),
49        Line::new::<i32>(),
50        Line::new::<i64>(),
51        Line::new::<i128>(),
52        Line::new::<usize>(),
53        Line::new::<isize>(),
54        Line::new::<f32>(),
55        Line::new::<f64>(),
56        Line::with_link::<Box<T>>("Box<T>", "Box"),
57        Line::with_link::<std::sync::Arc<T>>("Arc<T>", "std::sync::Arc"),
58        Line::with_link::<std::rc::Rc<T>>("Rc<T>", "std::rc::Rc"),
59        Line::with_link::<std::borrow::Cow<'static, T>>("Cow<'static, T>", "std::borrow::Cow"),
60        Line::with_link::<std::cell::Cell<T>>("Cell<T>", "std::cell::Cell"),
61        Line::with_link::<std::cell::RefCell<T>>("RefCell<T>", "std::cell::RefCell"),
62        Line::with_link::<std::sync::Mutex<T>>("Mutex<T>", "std::sync::Mutex"),
63        Line::with_link::<std::sync::Weak<T>>("Weak<T>", "std::sync::Weak"),
64        Line::with_link::<std::marker::PhantomData<T>>(
65            "PhantomData<T>",
66            "std::marker::PhantomData",
67        ),
68        Line::with_link::<std::collections::HashSet<T>>("HashSet<T>", "std::collections::HashSet"),
69        Line::with_link::<std::collections::HashMap<T, U>>(
70            "HashMap<T1, U2>",
71            "std::collections::HashMap",
72        ),
73        Line::with_link::<std::collections::BTreeSet<T>>(
74            "BTreeSet<T>",
75            "std::collections::BTreeSet",
76        ),
77        Line::with_link::<std::collections::BTreeMap<T, U>>(
78            "BTreeMap<T1, U2>",
79            "std::collections::BTreeMap",
80        ),
81    ];
82
83    let s: String = lines.into_iter().map(|l| l.to_string()).collect();
84
85    format!("## Type Overview\n\n\n\n{s}")
86}
87
88struct Line {
89    rust: String,
90    schema: String,
91}
92
93impl Line {
94    fn new<T: crate::ZodType>() -> Self {
95        let name = std::any::type_name::<T>();
96        Self::with_link::<T>(name, name)
97    }
98
99    fn with_link<'a, T: crate::ZodType>(name: &'a str, link: impl Into<Option<&'a str>>) -> Self {
100        let link = link.into().map(escape);
101        let name = escape(name);
102
103        Self {
104            rust: match link {
105                Some(link) => format!("[{name}]({link})"),
106                None => format!("[{name}]"),
107            },
108
109            schema: T::schema(),
110        }
111    }
112}
113
114impl std::fmt::Display for Line {
115    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
116        writeln!(
117            f,
118            "{}\n```ignore// schema\n{}\n```",
119            &self.rust, &self.schema,
120        )
121    }
122}
123
124fn escape(input: &str) -> String {
125    // input.to_string()
126    input
127        .replace('<', "&lt;")
128        .replace('>', "&gt;")
129        .replace('|', "&#124;")
130}