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
use std::borrow::Cow;
use std::fmt::Display;
use std::str::FromStr;

use super::*;

impl CharacterData {
    pub(crate) fn check_value(value: &CharacterData, spec: &CharacterDataSpec, file_version: AutosarVersion) -> bool {
        match spec {
            // the specification must call for an enum
            CharacterDataSpec::Enum { items } => {
                // the actual value must be an enum item
                if let CharacterData::Enum(enumitem) = &value {
                    // find the given enum item in the specified list of values
                    if let Some((_, version_mask)) = items.iter().find(|(name, _)| *name == *enumitem) {
                        // and finally check that this value is allowed in the active autosar version
                        if *version_mask & (file_version as u32) != 0 {
                            return true;
                        }
                    }
                }
            }
            CharacterDataSpec::Pattern {
                check_fn, max_length, ..
            } => {
                if let CharacterData::String(stringval) = &value {
                    if (max_length.is_none() || stringval.len() < max_length.unwrap()) && check_fn(stringval.as_bytes())
                    {
                        return true;
                    }
                }
            }
            CharacterDataSpec::String { max_length, .. } => {
                if let CharacterData::String(stringval) = &value {
                    if max_length.is_none() || stringval.len() < max_length.unwrap() {
                        return true;
                    }
                }
            }
            CharacterDataSpec::UnsignedInteger => {
                if let CharacterData::UnsignedInteger(_) = &value {
                    return true;
                }
            }
            CharacterDataSpec::Double => {
                if let CharacterData::Double(_) = &value {
                    return true;
                }
            }
        }
        false
    }

    pub(crate) fn check_version_compatibility(
        &self,
        data_spec: &CharacterDataSpec,
        target_version: AutosarVersion,
    ) -> (bool, u32) {
        if let CharacterDataSpec::Enum { items } = data_spec {
            if let CharacterData::Enum(attrval) = self {
                if let Some((_, enumitem_version_mask)) = items.iter().find(|(item, _)| *item == *attrval) {
                    if target_version.compatible(*enumitem_version_mask) {
                        return (true, *enumitem_version_mask);
                    } else {
                        return (false, *enumitem_version_mask);
                    }
                } else {
                    // no spec for this item -> not allowed in any version
                    return (false, 0);
                }
            } else {
                // content is not an enum item, but an enum item is expected? shouldn't be possible, maybe panic?
                return (false, u32::MAX);
            }
        }
        (true, u32::MAX)
    }

    pub(crate) fn parse(input: &str, character_data_spec: &CharacterDataSpec, version: AutosarVersion) -> Option<Self> {
        match character_data_spec {
            CharacterDataSpec::Enum { items } => {
                if let Ok(enumitem) = EnumItem::from_str(input) {
                    if let Some((_, version_mask)) = items.iter().find(|(item, _)| *item == enumitem) {
                        if version as u32 & version_mask != 0 {
                            return Some(CharacterData::Enum(enumitem));
                        }
                    }
                }
            }
            CharacterDataSpec::Pattern {
                check_fn, max_length, ..
            } => {
                if (max_length.is_none() || input.len() < max_length.unwrap()) && check_fn(input.as_bytes()) {
                    return Some(CharacterData::String(input.to_owned()));
                }
            }
            CharacterDataSpec::String { max_length, .. } => {
                if max_length.is_none() || input.len() < max_length.unwrap() {
                    return Some(CharacterData::String(input.to_owned()));
                }
            }
            CharacterDataSpec::UnsignedInteger => {
                if let Ok(value) = input.parse() {
                    return Some(CharacterData::UnsignedInteger(value));
                }
            }
            CharacterDataSpec::Double => {
                if let Ok(value) = input.parse() {
                    return Some(CharacterData::Double(value));
                }
            }
        }
        None
    }

    pub(crate) fn serialize_internal(&self, outstring: &mut String) {
        match self {
            CharacterData::Enum(enumval) => outstring.push_str(enumval.to_str()),
            CharacterData::String(strval) => outstring.push_str(&escape_text(strval)),
            CharacterData::UnsignedInteger(intval) => outstring.push_str(&intval.to_string()),
            CharacterData::Double(doubleval) => outstring.push_str(&doubleval.to_string()),
        }
    }

    /// Get the contained enum value
    ///
    /// Returns the enum value if the content is an enum, or None otherwise
    pub fn enum_value(&self) -> Option<EnumItem> {
        if let CharacterData::Enum(item) = self {
            Some(*item)
        } else {
            None
        }
    }

    /// Get the contained string
    ///
    /// Returns the string if the content is a string, or None otherwise
    pub fn string_value(&self) -> Option<String> {
        if let CharacterData::String(value) = self {
            Some(value.to_owned())
        } else {
            None
        }
    }

    /// Get the contained unsigned integer
    ///
    /// Returns the string if the content is a string, or None otherwise
    pub fn unsigned_integer_value(&self) -> Option<u64> {
        if let CharacterData::UnsignedInteger(uintval) = self {
            Some(*uintval)
        } else {
            None
        }
    }

    /// Get the contained double
    ///
    /// Returns the value content is a double, or None otherwise
    pub fn double_value(&self) -> Option<f64> {
        if let CharacterData::Double(value) = self {
            Some(*value)
        } else {
            None
        }
    }
}

impl Display for CharacterData {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            CharacterData::Enum(enumitem) => f.write_str(enumitem.to_str()),
            CharacterData::String(stringval) => f.write_str(stringval),
            CharacterData::UnsignedInteger(uintval) => f.write_str(&uintval.to_string()),
            CharacterData::Double(f64val) => f.write_str(&f64val.to_string()),
        }
    }
}

fn escape_text(input: &str) -> Cow<str> {
    if input.contains(&['&', '>', '<', '\'', '"']) {
        let mut escaped = String::with_capacity(input.len() + 6);

        for c in input.chars() {
            match c {
                '<' => escaped.push_str("&lt;"),
                '>' => escaped.push_str("&gt;"),
                '&' => escaped.push_str("&amp;"),
                '"' => escaped.push_str("&quot;"),
                '\'' => escaped.push_str("&apos;"),
                other => escaped.push(other),
            }
        }

        Cow::Owned(escaped)
    } else {
        Cow::Borrowed(input)
    }
}