trs_dataframe/dataframe/
key.rs

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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//use flexstr::SharedStr as SharedString;
#[cfg(feature = "python")]
use pyo3::prelude::*;
#[cfg(feature = "utoipa")]
use utoipa::ToSchema;

use smartstring::alias::String as SString;

use crate::DataType;

/// [`Key`] holds information about key. It's used to identify the feature
/// Mostly is represented as a string but inside is stored as a hash and the [`DataType`]
/// of the feature/value which is stored in [`crate::DataFrame`]
#[derive(Clone, Eq, serde::Deserialize, serde::Serialize, Default)]
#[cfg_attr(feature = "python", pyo3::pyclass)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
pub struct Key {
    pub key: u32,
    #[cfg_attr(feature = "utoipa", schema(schema_with = smart_string_schema))]
    pub name: SString,
    pub ctype: DataType,
}

#[cfg(feature = "utoipa")]
fn smart_string_schema() -> utoipa::openapi::Object {
    utoipa::openapi::ObjectBuilder::new()
        .schema_type(utoipa::openapi::schema::Type::String)
        .build()
}

impl Key {
    pub fn name(&self) -> &str {
        self.name.as_str()
    }

    pub fn id(&self) -> u32 {
        self.key
    }
    pub fn key(&self) -> crate::Key {
        self.clone()
    }
}
impl PartialEq for Key {
    fn eq(&self, other: &Self) -> bool {
        self.key == other.key
    }
}
impl PartialOrd for Key {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.key.cmp(&other.key))
    }
}
impl Ord for Key {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.key.cmp(&other.key)
    }
}

#[cfg(feature = "python")]
#[pymethods]
impl Key {
    #[new]
    #[pyo3(signature = (name, ctype=None))]
    pub fn init(name: String, ctype: Option<DataType>) -> Self {
        Self::new(name.as_str(), ctype.unwrap_or(DataType::Unknown))
    }

    #[pyo3(name = "name")]
    pub fn py_name(&self) -> &str {
        self.name()
    }

    #[pyo3(name = "id")]
    pub fn py_id(&self) -> u32 {
        self.key
    }
}

impl std::fmt::Display for Key {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.name)
    }
}
impl std::fmt::Debug for Key {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self)
    }
}

impl From<&str> for Key {
    fn from(name: &str) -> Self {
        Self {
            key: crate::utils::fnv1a_hash_str_32(name),
            name: name.into(),
            ctype: DataType::Unknown,
        }
    }
}
impl From<SString> for Key {
    fn from(name: SString) -> Self {
        Self::from(name.as_str())
    }
}
impl From<String> for Key {
    fn from(name: String) -> Self {
        Self::from(name.as_str())
    }
}

impl From<&String> for Key {
    fn from(name: &String) -> Self {
        Self::from(name.as_str())
    }
}

impl Key {
    pub fn new(name: &str, ctype: DataType) -> Self {
        Self {
            key: crate::utils::fnv1a_hash_str_32(name),
            name: name.into(),
            ctype,
        }
    }
}

impl std::hash::Hash for Key {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        state.write_u32(self.key);
    }
}

#[cfg(test)]
mod test {
    use super::*;
    #[test]
    fn dummy_test() {
        let key = Key::from("test");
        assert_eq!(key.key, 2949673445);
        assert_eq!(key.id(), 2949673445);
        assert_eq!(key.key(), key);
        assert_eq!(key.name, "test");
        let s = String::from("test");
        let key1 = Key::from(s);
        assert_eq!(key1.key, 2949673445);
        assert_eq!(key1.name, "test");
        assert_eq!(key1, key);
        assert!(key1.cmp(&key) == std::cmp::Ordering::Equal);
        let s = SString::from("test");
        let key = Key::from(s);
        assert_eq!(key.key, 2949673445);
        assert_eq!(key.name, "test");
        let key = Key::new("test", DataType::Unknown);
        assert_eq!(key.key, 2949673445);
        assert_eq!(key.name, "test");
        assert_eq!(key.ctype, DataType::Unknown);
        assert_eq!(format!("{}", key), "test");
        let s = format!("{:?}", key);
        println!("{}", s);
        let key = Key::from(&("test".to_string()));
        assert_eq!(key.key, 2949673445);
        assert_eq!(key.name, "test");
        let key_serialized = serde_json::to_string(&key).expect("BUG: Cannot serialize");
        let key_deserialized: Key =
            serde_json::from_str(&key_serialized).expect("BUG: Cannot deserialize");
        assert_eq!(key, key_deserialized);
    }

    #[cfg(feature = "python")]
    #[test]
    fn py_test() {
        let key = Key::init("test".into(), Some(DataType::String));
        assert_eq!(key.key, 2949673445);
        assert_eq!(key.name, "test");
        assert_eq!(key.ctype, DataType::String);
        assert_eq!(key.py_name(), "test");
        assert_eq!(key.py_id(), 2949673445);
    }
}