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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
use std::{borrow::Cow, path::PathBuf};

use serde::{de::Visitor, Deserializer};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

pub(crate) mod internal;
pub mod ip;
pub mod utils;

pub use ip::Ip;

pub type Text = Cow<'static, str>;

#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub enum Field {
    #[default]
    Null,
    /// A basic String field
    Text(Text),
    /// IPv4 or IPv6
    Ip(Ip),
    //Domain like contoso.com
    Domain(String),
    User(String),
    ///This is a special field. Uniquely identifies an asset like a system, a
    /// computer or a mobile phone. Reason: the network is dynamic, the IP address
    /// is not fixed certain devices and the hostname of a system can be changed.
    ///
    /// This field should be used with a dataset to recover information about an asset
    /// during the enchance phase:
    /// Getting the IP address, the users logged in the system or another information.
    ///
    /// Can be multiple AssetsID associated with the same event because multiple virtual
    /// machines can be running in the same asset.
    AssetID(String),
    /// unsigned number with 64 bits
    U64(u64),
    /// signed number with 64 bits
    I64(i64),
    /// decimal number with 64 bits
    F64(f64),
    ///A date in a decimal number format with 64 bits
    Date(i64),
    Array(Vec<Text>),
    Path(PathBuf),
}

impl<'a> TryInto<&'a str> for &'a Field {
    type Error = &'static str;

    fn try_into(self) -> Result<&'a str, Self::Error> {
        match self {
            Field::Text(v) => Ok(&v[..]),
            Field::Domain(v) => Ok(&v[..]),
            Field::User(v) => Ok(&v[..]),
            Field::AssetID(v) => Ok(&v[..]),
            _ => Err("Invalid text type"),
        }
    }
}

impl<'a> TryInto<Text> for &'a Field {
    type Error = &'static str;

    fn try_into(self) -> Result<Text, Self::Error> {
        match self {
            Field::Text(v) => Ok(v.clone()),
            Field::Domain(v) => Ok(Text::Owned(v.to_string())),
            Field::User(v) => Ok(Text::Owned(v.to_string())),
            Field::AssetID(v) => Ok(Text::Owned(v.to_string())),
            _ => Err("Invalid type"),
        }
    }
}
impl<'a> TryInto<&'a Text> for &'a Field {
    type Error = &'static str;

    fn try_into(self) -> Result<&'a Text, Self::Error> {
        match self {
            Field::Text(v) => Ok(v),
            _ => Err("Invalid type"),
        }
    }
}

impl<'a> TryInto<&'a Vec<Text>> for &'a Field {
    type Error = &'static str;

    fn try_into(self) -> Result<&'a Vec<Text>, Self::Error> {
        match self {
            Field::Array(v) => Ok(v),
            _ => Err("Invalid type"),
        }
    }
}

impl<'a> TryInto<Vec<Text>> for &'a Field {
    type Error = &'static str;

    fn try_into(self) -> Result<Vec<Text>, Self::Error> {
        let value = match self {
            Field::Array(v) => return Ok(v.clone()),
            Field::AssetID(v) => Text::Owned(v.clone()),
            Field::Text(v) => v.clone(),
            Field::Domain(v) => Text::Owned(v.clone()),
            Field::User(v) => Text::Owned(v.clone()),
            Field::I64(v) => Text::Owned(v.to_string()),
            Field::F64(v) => Text::Owned(v.to_string()),
            Field::U64(v) => Text::Owned(v.to_string()),
            Field::Date(v) => Text::Owned(v.to_string()),
            Field::Ip(v) => Text::Owned(v.to_string()),
            Field::Null => Text::Borrowed(""),
            Field::Path(v) => Text::Owned(v.to_string_lossy().to_string()),
        };
        Ok(vec![value])
    }
}

impl<'a> TryInto<u64> for &'a Field {
    type Error = &'static str;

    fn try_into(self) -> Result<u64, Self::Error> {
        Ok(match self {
            Field::F64(v) => *v as u64,
            Field::I64(v) => *v as u64,
            Field::U64(v) => *v,
            Field::Date(v) => *v as u64,
            _ => return Err("Invalid type"),
        })
    }
}
impl<'a> TryInto<i64> for &'a Field {
    type Error = &'static str;

    fn try_into(self) -> Result<i64, Self::Error> {
        Ok(match self {
            Field::F64(v) => *v as i64,
            Field::I64(v) => *v as i64,
            Field::U64(v) => *v as i64,
            Field::Date(v) => *v as i64,
            _ => return Err("Invalid type"),
        })
    }
}
impl<'a> TryInto<f64> for &'a Field {
    type Error = &'static str;

    fn try_into(self) -> Result<f64, Self::Error> {
        Ok(match self {
            Field::F64(v) => *v as f64,
            Field::I64(v) => *v as f64,
            Field::U64(v) => *v as f64,
            Field::Date(v) => *v as f64,
            _ => return Err("Invalid type"),
        })
    }
}

impl<'a> TryInto<Ip> for &'a Field {
    type Error = &'static str;
    fn try_into(self) -> Result<Ip, Self::Error> {
        Ok(match self {
            Field::Text(v) => Ip::from_ip_str(&v).map_err(|_e| "Invalud ip format")?,
            Field::Ip(v) => v.clone(),
            _ => return Err("Type cannot be converted to Ip"),
        })
    }
}

impl From<&'static str> for Field {
    fn from(v: &'static str) -> Field {
        Field::Text(Text::Borrowed(v))
    }
}
impl From<&String> for Field {
    fn from(v: &String) -> Field {
        Field::Text(Text::Owned(v.to_string()))
    }
}
impl From<String> for Field {
    fn from(v: String) -> Field {
        Field::Text(Text::Owned(v))
    }
}
impl From<Text> for Field {
    fn from(v: Text) -> Field {
        Field::Text(v)
    }
}
impl From<&Text> for Field {
    fn from(v: &Text) -> Field {
        Field::Text(v.clone())
    }
}

impl From<&u64> for Field {
    fn from(v: &u64) -> Field {
        Field::U64(*v)
    }
}
impl From<u64> for Field {
    fn from(v: u64) -> Field {
        Field::U64(v)
    }
}
impl From<&i64> for Field {
    fn from(v: &i64) -> Field {
        Field::I64(*v)
    }
}
impl From<i64> for Field {
    fn from(v: i64) -> Field {
        Field::I64(v)
    }
}

impl From<&f64> for Field {
    fn from(v: &f64) -> Field {
        Field::F64(*v)
    }
}
impl From<f64> for Field {
    fn from(v: f64) -> Field {
        Field::F64(v)
    }
}
impl From<Ip> for Field {
    fn from(v: Ip) -> Field {
        Field::Ip(v)
    }
}
impl From<&Ip> for Field {
    fn from(v: &Ip) -> Field {
        Field::Ip(*v)
    }
}
impl From<Vec<Text>> for Field {
    fn from(v: Vec<Text>) -> Field {
        Field::Array(v)
    }
}
impl From<&Vec<Text>> for Field {
    fn from(v: &Vec<Text>) -> Field {
        Field::Array(v.clone())
    }
}

#[cfg(feature = "serde")]
impl Serialize for Field {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        match self {
            Field::Null => serializer.serialize_none(),
            Field::Text(v) => serializer.serialize_str(&v[..]),
            Field::Ip(v) => v.serialize(serializer),
            Field::Domain(v) => serializer.serialize_str(&v[..]),
            Field::User(v) => serializer.serialize_str(&v[..]),
            Field::AssetID(v) => serializer.serialize_str(&v[..]),
            Field::U64(v) => serializer.serialize_u64(*v),
            Field::I64(v) => serializer.serialize_i64(*v),
            Field::F64(v) => serializer.serialize_f64(*v),
            Field::Date(v) => serializer.serialize_i64(*v),
            Field::Array(v) => v.serialize(serializer),
            Field::Path(v) => serializer.serialize_str(&v.to_string_lossy()[..]),
        }
    }
}
#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for Field {
    fn deserialize<D>(deserializer: D) -> Result<Field, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_any(FieldVisitor)
    }
}
#[cfg(feature = "serde")]
struct FieldVisitor;
#[cfg(feature = "serde")]
impl<'de> Visitor<'de> for FieldVisitor {
    type Value = Field;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        formatter.write_str("a valid forensic data")
    }

    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(Field::Text(Cow::Owned(v.to_string())))
    }
    fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(Field::Text(Cow::Owned(v)))
    }
    fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(Field::U64(if v { 1 } else { 0 }))
    }

    fn visit_i8<E>(self, v: i8) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(Field::I64(v as _))
    }
    fn visit_i16<E>(self, v: i16) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(Field::I64(v as _))
    }
    fn visit_i32<E>(self, v: i32) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(Field::I64(v as _))
    }
    fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(Field::I64(v))
    }
    fn visit_f32<E>(self, v: f32) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(Field::F64(v as _))
    }

    fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(Field::F64(v))
    }

    fn visit_u32<E>(self, v: u32) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(Field::U64(v as _))
    }
    fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(Field::U64(v))
    }

    fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(Field::U64(v as _))
    }
    fn visit_u16<E>(self, v: u16) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(Field::U64(v as _))
    }
    fn visit_unit<E>(self) -> Result<Self::Value, E>
        where
            E: serde::de::Error, {
        Ok(Field::Null)
    }
    fn visit_none<E>(self) -> Result<Self::Value, E>
        where
            E: serde::de::Error, {
        Ok(Field::Null)
    }

    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
        where
            A: serde::de::SeqAccess<'de>, {
        let mut vc = Vec::with_capacity(32);
        while let Some(value) = seq.next_element()? {
            vc.push(value);
        }
        Ok(Field::Array(vc))
    }
}