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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//  ------------------------------------------------------------------
//  Airone
//  is a Rust library which provides a simple in-memory,
//  write-on-update database that is persisted
//  to an append-only transaction file.
//
//  Copyright © 2022 Massimo Gismondi
//
//  This file is part of Airone.
//  Airone is free software: you can redistribute it and/or
//  modify it under the terms of the GNU Affero General Public License
//  as published by the Free Software Foundation, either version 3
//  of the License, or (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU Affero General Public License
//  along with this program. If not, see <https://www.gnu.org/licenses/>.
//  ------------------------------------------------------------------

/// A value that can be converted to a String
/// properly escaping characters that are not
/// allowed in airone's CSV format
///
/// Sometimes it's enough to format!() the value,
/// sometimes it needs custom conversions.
pub trait PersistableValue
{
    fn to_persistable_string(&self) -> String;
}

/// A value that can be converted from the String
/// to its actual type.
///
/// Sometimes it's enough to parse the value,
/// sometimes it needs custom conversions.
pub trait LoadableValue
{
    fn from_persistable_string(s: &String) -> Self;
}

macro_rules! impl_persistable_value_with_format {
    ($($t:ty),*) => {
        $(
            impl PersistableValue for $t
            {

                fn to_persistable_string(&self) -> String
                {
                    format!("{}", &self)
                }
            }
        )*
    };
}
impl_persistable_value_with_format!(
    bool, i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, f32, f64
);

macro_rules! impl_loadable_value_with_parse {
    ($($t:ty),*) => {
        $(
            impl LoadableValue for $t
            {
                fn from_persistable_string(s: &String) -> $t
                {
                    return (s.parse::<$t>().unwrap()).clone()
                }
            }
        )*
    };
}
impl_loadable_value_with_parse!(
    bool, i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, f32, f64
);

impl PersistableValue for String
{
    fn to_persistable_string(&self) -> String
    {
        let a = &self.clone();
        return a
            .replace("\n", "\\n")
            .replace("\r", "\\r")
            .replace("\t", "\\t")
            .replace("\"", "\\\"");
    }
}
impl LoadableValue for String
{
    fn from_persistable_string(s: &String) -> String
    {
        let a = s.clone();
        return a
            .replace("\\n", "\n")
            .replace("\\r", "\r")
            .replace("\\t", "\t")
            .replace("\\\"", "\"");
    }
}

impl<T> PersistableValue for Option<T>
where
    T: PersistableValue
{
    fn to_persistable_string(&self) -> String
    {
        if let Some(el) = &self
        {
            return el.to_persistable_string();
        }
        else
        {
            return String::new();
        }
    }
}

impl<A: LoadableValue> LoadableValue for Option<A>
{
    fn from_persistable_string(s: &String) -> Option<A>
    {
        if s.len() > 0
        {
            let b: A = LoadableValue::from_persistable_string(s);
            return Some(b);
        }
        else
        {
            return None;
        }
    }
}