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
//! Contains [Storable] trait that marks a type suitable to use in [crate::XvcStore].
use serde::{Deserialize, Serialize};

/// Marks the traits as storable in XvcStore
///
/// It requires to implement an fn to describe the store names.
/// It also requires the implementer to be (serde) serializable, and to implement Clone, Debug, Ord
/// and PartialEq.
///
/// These are to use JSON as storage format, and to be able to use BTreeMaps as data structures.
pub trait Storable:
    Serialize + for<'lt> Deserialize<'lt> + Clone + std::fmt::Debug + Ord + PartialEq
{
    /// A string representation for the type.
    ///
    /// By convention this should be in kebab-case, like `xvc-path` for [xvc-core::XvcPath].
    /// Xvc uses this to create filenames in serialization.
    ///
    /// See [crate::persist] macro to specify this representation conveniently.
    fn type_description() -> String;
}

impl Storable for String {
    fn type_description() -> String {
        "string".to_string()
    }
}

impl<T: Storable> Storable for Option<T> {
    fn type_description() -> String {
        format!("option-{}", <T as Storable>::type_description())
    }
}

impl<T: Storable, U: Storable> Storable for (T, U) {
    fn type_description() -> String {
        format!(
            "tuple-{}-{}",
            <T as Storable>::type_description(),
            <U as Storable>::type_description()
        )
    }
}
impl Storable for i32 {
    fn type_description() -> String {
        "i32".to_string()
    }
}

impl Storable for usize {
    fn type_description() -> String {
        "usize".to_string()
    }
}