1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// SPDX-FileCopyrightText: 2022 - 2023 Robin Vobruba <hoijui.quaero@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-or-later

use std::{
    borrow::Cow,
    collections::{HashMap, HashSet},
};

pub trait Codify {
    fn init_code(&self) -> Cow<'static, str>;
}

impl<T: Codify> Codify for Option<T> {
    fn init_code(&self) -> Cow<'static, str> {
        self.as_ref().map_or(Cow::Borrowed("None"), |val| {
            Cow::Owned(format!("Some({})", val.init_code()))
        })
    }
}

impl<'a, T> Codify for Cow<'a, T>
where
    T: Clone,
    for<'b> &'b T: Codify,
{
    fn init_code(&self) -> Cow<'static, str> {
        self.as_ref().init_code()
    }
}

impl<T> Codify for Box<T>
where
    for<'b> &'b T: Codify,
{
    fn init_code(&self) -> Cow<'static, str> {
        self.as_ref().init_code()
    }
}

// impl<T: AsRef<str>> Codify for T {
//     fn init_code(&self) -> Cow<'static, str> {
//         Cow::Owned(format!(r##""{}""##, self.as_ref()))
//     }
// }

impl Codify for String {
    fn init_code(&self) -> Cow<'static, str> {
        // NOTE: This codifies the String into a &'static str!
        Cow::Owned(format!(r##"String::from("{self}")"##))
    }
}

impl Codify for &str {
    fn init_code(&self) -> Cow<'static, str> {
        Cow::Owned(format!(r##""{self}""##))
    }
}

impl<T> Codify for Vec<T>
where
    T: Codify,
{
    fn init_code(&self) -> Cow<'static, str> {
        let mut parts = vec!["vec![\n".to_string()];
        for entry in self {
            parts.push(format!("{},\n", entry.init_code()));
        }
        parts.push("]".to_string());

        Cow::Owned(parts.concat())
    }
}

impl<K, V, S: ::std::hash::BuildHasher> Codify for HashMap<K, V, S>
where
    K: Codify,
    V: Codify,
{
    fn init_code(&self) -> Cow<'static, str> {
        let mut parts = vec!["{{\nlet mut map = HashMap::new();\n".to_string()];
        for (key, val) in self {
            parts.push(format!(
                r##"map.insert({}, {});
"##,
                key.init_code(),
                val.init_code()
            ));
        }
        parts.push("map\n}}".to_string());

        Cow::Owned(parts.concat())
    }
}

impl<E, S: ::std::hash::BuildHasher> Codify for HashSet<E, S>
where
    E: Codify,
{
    fn init_code(&self) -> Cow<'static, str> {
        let mut parts = vec!["{{\nlet mut set = HashSet::new();\n".to_string()];
        for entry in self {
            parts.push(format!(
                r##"set.insert({});
"##,
                entry.init_code(),
            ));
        }
        parts.push("set\n}}".to_string());

        Cow::Owned(parts.concat())
    }
}

// This tests rust code in the README with doc-tests.
// Though, It will not appear in the generated documentaton.
#[doc = include_str!("../README.md")]
#[cfg(doctest)]
pub struct ReadmeDoctests;