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
// Copyright (c) 2022 MASSA LABS <info@massa.net>

use crate::error::ModelsError;
use massa_serialization::{Deserializer, Serializer, U32VarIntDeserializer, U32VarIntSerializer};
use nom::bytes::complete::take;
use nom::error::context;
use nom::sequence::tuple;
use nom::Parser;
use nom::{
    error::{ContextError, ParseError},
    IResult,
};
use serde::de::Unexpected;
use std::ops::Bound::Included;
use std::{convert::TryInto, fmt, str::FromStr};

const INSTANCE_LEN: usize = 4;

/// Application version, checked during handshakes
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Version {
    /// ASCII uppercase alpha
    instance: [char; INSTANCE_LEN],
    major: u32,
    minor: u32,
}

struct VersionVisitor;

impl<'de> serde::de::Visitor<'de> for VersionVisitor {
    type Value = Version;

    fn visit_str<E>(self, value: &str) -> Result<Version, E>
    where
        E: serde::de::Error,
    {
        Version::from_str(value).map_err(|_| E::invalid_value(Unexpected::Str(value), &self))
    }

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        write!(
            formatter,
            "an Version type representing a version identifier"
        )
    }
}

impl<'de> serde::Deserialize<'de> for Version {
    fn deserialize<D>(deserializer: D) -> Result<Version, D::Error>
    where
        D: serde::de::Deserializer<'de>,
    {
        deserializer.deserialize_str(VersionVisitor)
    }
}

impl serde::Serialize for Version {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(&self.to_string())
    }
}

/// Serializer for `Version`
#[derive(Clone)]
pub struct VersionSerializer {
    u32_serializer: U32VarIntSerializer,
}

impl VersionSerializer {
    /// Creates a `VersionSerializer`
    pub const fn new() -> Self {
        Self {
            u32_serializer: U32VarIntSerializer::new(),
        }
    }
}

impl Default for VersionSerializer {
    fn default() -> Self {
        Self::new()
    }
}

impl Serializer<Version> for VersionSerializer {
    /// ```
    /// use std::ops::Bound::Included;
    /// use std::str::FromStr;
    /// use massa_serialization::Serializer;
    /// use massa_models::version::{Version, VersionSerializer};
    ///
    /// let version: Version = Version::from_str("TEST.1.10").unwrap();
    /// let serializer = VersionSerializer::new();
    /// let mut buffer = Vec::new();
    /// serializer.serialize(&version, &mut buffer).unwrap();
    /// ```
    fn serialize(
        &self,
        value: &Version,
        buffer: &mut Vec<u8>,
    ) -> Result<(), massa_serialization::SerializeError> {
        buffer.extend(value.instance.iter().map(|&c| c as u8));
        self.u32_serializer.serialize(&value.major, buffer)?;
        self.u32_serializer.serialize(&value.minor, buffer)?;
        Ok(())
    }
}

/// Serializer for `Version`
#[derive(Clone)]
pub struct VersionDeserializer {
    u32_deserializer: U32VarIntDeserializer,
}

impl VersionDeserializer {
    /// Creates a `VersionSerializer`
    pub const fn new() -> Self {
        Self {
            u32_deserializer: U32VarIntDeserializer::new(Included(0), Included(u32::MAX)),
        }
    }
}

impl Default for VersionDeserializer {
    fn default() -> Self {
        Self::new()
    }
}

impl Deserializer<Version> for VersionDeserializer {
    /// ```
    /// use std::ops::Bound::Included;
    /// use std::str::FromStr;
    /// use massa_serialization::{Serializer, Deserializer, DeserializeError};
    /// use massa_models::version::{Version, VersionSerializer, VersionDeserializer};
    ///
    /// let version: Version = Version::from_str("TEST.1.10").unwrap();
    /// let mut serialized = Vec::new();
    /// let serializer = VersionSerializer::new();
    /// let deserializer = VersionDeserializer::new();
    /// serializer.serialize(&version, &mut serialized).unwrap();
    /// let (rest, version_deser) = deserializer.deserialize::<DeserializeError>(&serialized).unwrap();
    /// assert_eq!(rest.len(), 0);
    /// assert_eq!(version, version_deser);
    /// ```
    fn deserialize<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(
        &self,
        buffer: &'a [u8],
    ) -> IResult<&'a [u8], Version, E> {
        context(
            "Failed Version deserialization",
            tuple((
                context("Failed instance deserialization", |input: &'a [u8]| {
                    let (rest, instance) = take(INSTANCE_LEN)(input)?;
                    if instance.iter().any(|c| {
                        !c.is_ascii() || !c.is_ascii_alphabetic() || !c.is_ascii_uppercase()
                    }) {
                        return Err(nom::Err::Error(ParseError::from_error_kind(
                            input,
                            nom::error::ErrorKind::Char,
                        )));
                    }
                    // Safe: because `take` fail if there is not enough data
                    let instance: [char; INSTANCE_LEN] = instance
                        .iter()
                        .map(|&c| char::from(c))
                        .collect::<Vec<char>>()
                        .try_into()
                        .unwrap();
                    Ok((rest, instance))
                }),
                context("Failed major deserialization", |input: &'a [u8]| {
                    self.u32_deserializer.deserialize(input)
                }),
                context("Failed minor deserialization", |input: &'a [u8]| {
                    self.u32_deserializer.deserialize(input)
                }),
            )),
        )
        .map(|(instance, major, minor)| Version {
            instance,
            major,
            minor,
        })
        .parse(buffer)
    }
}

impl Version {
    /// true if instance and major are the same
    pub fn is_compatible(&self, other: &Version) -> bool {
        self.instance == other.instance && self.major == other.major
    }
}

impl fmt::Display for Version {
    /// ```rust
    /// # use massa_models::*;
    /// # use std::str::FromStr;
    /// let v: version::Version = version::Version::from_str("TEST.1.10").unwrap();
    /// assert_eq!(v.to_string(), "TEST.1.10");
    /// ```
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let network_str: String = self.instance.iter().cloned().collect();
        write!(f, "{}.{}.{}", network_str, self.major, self.minor)
    }
}

impl FromStr for Version {
    type Err = ModelsError;

    fn from_str(str_version: &str) -> Result<Self, Self::Err> {
        let parts: Vec<String> = str_version.split('.').map(|v| v.to_string()).collect();
        if parts.len() != 3 {
            return Err(ModelsError::InvalidVersionError(
                "version identifier is not in 3 parts separates by a dot".into(),
            ));
        }
        if parts[0].len() != 4
            || !parts[0].is_ascii()
            || parts[0]
                .chars()
                .any(|c| !c.is_ascii_alphabetic() || !c.is_ascii_uppercase())
        {
            return Err(ModelsError::InvalidVersionError(
                "version identifier instance part is not 4-char uppercase alphabetic ASCII".into(),
            ));
        }
        let instance: [char; 4] = parts[0].chars().collect::<Vec<char>>().try_into().unwrap(); // will not panic as the length and type were checked above
        let major: u32 = u32::from_str(&parts[1]).map_err(|_| {
            ModelsError::InvalidVersionError("version identifier major number invalid".into())
        })?;
        let minor: u32 = u32::from_str(&parts[2]).map_err(|_| {
            ModelsError::InvalidVersionError("version identifier minor number invalid".into())
        })?;
        Ok(Version {
            instance,
            major,
            minor,
        })
    }
}