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
use crate::prelude::*;

/// The package version parser
#[derive(Debug, Clone)]
pub struct Version {
    value: String,
}

impl Version {
    /// Get as string slice
    pub fn as_str(&self) -> &str {
        &self.value
    }

    /// Converts to string
    pub fn to_string(&self) -> String {
        self.value.clone()
    }
}

impl std::str::FromStr for Version {
    type Err = Error;
    
    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        let re = Regex::new(r"^\^?\d+(\.\d+)*$").unwrap();
        if re.is_match(s) {
            Ok(Self {
                value: s.to_owned(),
            })
        } else {
            Err(Error::IncorrectVersionFormat)
        }
    }
}

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

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