aboutsummaryrefslogtreecommitdiff
path: root/p2p/src/utils/version.rs
blob: 4986495ab0609fd66866b2f22152ea2c1fff867d (plain)
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
use std::str::FromStr;

use bincode::{Decode, Encode};
use semver::VersionReq;

use crate::{Error, Result};

/// Represents the network version and protocol version used in Karyons p2p.
///
/// # Example
///
/// ```
/// use karyons_p2p::Version;
///
/// let version: Version = "0.2.0, >0.1.0".parse().unwrap();
///
/// let version: Version = "0.2.0".parse().unwrap();
///
/// ```
#[derive(Debug, Clone)]
pub struct Version {
    pub v: VersionInt,
    pub req: VersionReq,
}

impl Version {
    /// Creates a new Version
    pub fn new(v: VersionInt, req: VersionReq) -> Self {
        Self { v, req }
    }
}

#[derive(Debug, Decode, Encode, Clone)]
pub struct VersionInt {
    major: u64,
    minor: u64,
    patch: u64,
}

impl FromStr for Version {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self> {
        let v: Vec<&str> = s.split(", ").collect();
        if v.is_empty() || v.len() > 2 {
            return Err(Error::ParseError(format!("Invalid version{s}")));
        }

        let version: VersionInt = v[0].parse()?;
        let req: VersionReq = if v.len() > 1 { v[1] } else { v[0] }.parse()?;

        Ok(Self { v: version, req })
    }
}

impl FromStr for VersionInt {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self> {
        let v: Vec<&str> = s.split('.').collect();
        if v.len() < 2 || v.len() > 3 {
            return Err(Error::ParseError(format!("Invalid version{s}")));
        }

        let major = v[0].parse::<u64>()?;
        let minor = v[1].parse::<u64>()?;
        let patch = v.get(2).unwrap_or(&"0").parse::<u64>()?;

        Ok(Self {
            major,
            minor,
            patch,
        })
    }
}

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

impl From<VersionInt> for semver::Version {
    fn from(v: VersionInt) -> Self {
        semver::Version::new(v.major, v.minor, v.patch)
    }
}

/// Check if a version satisfies a version request.
pub fn version_match(version_req: &VersionReq, version: &VersionInt) -> bool {
    let version: semver::Version = version.clone().into();
    version_req.matches(&version)
}