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
use lexpr::Value;

use crate::{Error, Type};

#[derive(Debug, PartialEq)]
pub struct Field {
    pub name: String,
    pub r#type: Type,
}

impl Default for Field {
    fn default() -> Self {
        Self {
            name: Default::default(),
            r#type: Type::Other(Default::default()),
        }
    }
}

impl Field {
    /// Creates a new instance of field.
    ///
    /// Arguments:
    ///
    /// * `name`: A string representing the name of the field.
    /// * `r#type`: Type of the field.
    pub fn new(name: &str, r#type: Type) -> Self {
        Self {
            name: name.to_string(),
            r#type,
        }
    }
}

impl TryFrom<&Value> for Field {
    type Error = Error;

    /// Try to parse a field from a lexpr::Value
    fn try_from(value: &Value) -> Result<Self, Self::Error> {
        let name = value[0]["SimpleLocal"][0].to_string();
        let r#type = value[1].to_string().parse()?;

        Ok(Field { name, r#type })
    }
}

#[derive(Debug, PartialEq, Default)]
pub struct FieldList(pub Vec<Field>);

impl std::ops::Deref for FieldList {
    type Target = Vec<Field>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl TryFrom<&Value> for FieldList {
    type Error = Error;

    /// Try to parse a list of fields from a lexpr::Value
    fn try_from(value: &Value) -> Result<Self, Self::Error> {
        if !value.is_list() {
            return Ok(FieldList::default());
        }

        let fields: Result<Vec<Field>, Error> = value
            .list_iter()
            .unwrap()
            .map(|elem| elem.try_into())
            .collect();

        Ok(FieldList(fields?))
    }
}