Skip to main content

simploxide_core/
lib.rs

1//! Starting from `0.4.0` this crate is repurposed to supply types shared between
2//! [simploxide-ws-core](https://docs.rs/simploxide_ws_core/) and
3//! [simploxide-ffi-core](https://docs.rs/simploxide_ffi_core/). Check the
4//! documentation of the corresponding crates for actual functionality
5
6use std::str::FromStr;
7
8use serde::Deserialize;
9
10pub const MIN_SUPPORTED_VERSION: SimplexVersion = SimplexVersion::new(6, 5, 2, 0);
11pub const MAX_SUPPORTED_VERSION: SimplexVersion = SimplexVersion::new(6, 5, 2, 99);
12
13/// Parses SimpleX version numbers in the form `MAJOR.MINOR.PATCH.HOTFIX`.
14#[derive(Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
15pub struct SimplexVersion {
16    major: u8,
17    minor: u8,
18    patch: u8,
19    hotfix: u8,
20}
21
22impl SimplexVersion {
23    pub const fn new(major: u8, minor: u8, patch: u8, hotfix: u8) -> Self {
24        Self {
25            major,
26            minor,
27            patch,
28            hotfix,
29        }
30    }
31
32    pub fn major(&self) -> u8 {
33        self.major
34    }
35
36    pub fn minor(&self) -> u8 {
37        self.minor
38    }
39
40    pub fn patch(&self) -> u8 {
41        self.patch
42    }
43
44    pub fn hotfix(&self) -> u8 {
45        self.hotfix
46    }
47
48    pub fn is_supported(self) -> bool {
49        self >= MIN_SUPPORTED_VERSION && self <= MAX_SUPPORTED_VERSION
50    }
51}
52
53impl FromStr for SimplexVersion {
54    type Err = ();
55
56    fn from_str(s: &str) -> Result<Self, Self::Err> {
57        let mut num_iter = s.split('.');
58
59        fn get_num<'a, 'b>(iter: &'a mut impl Iterator<Item = &'b str>) -> Result<u8, ()> {
60            iter.next()
61                .ok_or(())
62                .and_then(|s| s.parse().map_err(|_| ()))
63        }
64
65        Ok(Self {
66            major: get_num(&mut num_iter)?,
67            minor: get_num(&mut num_iter)?,
68            patch: get_num(&mut num_iter)?,
69            hotfix: get_num(&mut num_iter)?,
70        })
71    }
72}
73
74impl std::fmt::Display for SimplexVersion {
75    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76        write!(
77            f,
78            "v{}.{}.{}.{}",
79            self.major, self.minor, self.patch, self.hotfix
80        )
81    }
82}
83
84impl std::fmt::Debug for SimplexVersion {
85    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
86        write!(f, "SimplexVersion(")?;
87        write!(f, "{self}")?;
88        write!(f, ")")
89    }
90}
91
92/// A helper to parse version from SimpleX response
93#[derive(Deserialize)]
94pub struct VersionInfo<'a> {
95    #[serde(borrow, rename = "versionInfo")]
96    pub version_info: VersionData<'a>,
97}
98
99#[derive(Deserialize)]
100pub struct VersionData<'a> {
101    #[serde(borrow)]
102    pub version: &'a str,
103}
104
105#[cfg(test)]
106mod tests {
107    use super::SimplexVersion;
108
109    #[test]
110    fn simplex_version_parse() {
111        let current: SimplexVersion = "6.4.9.0".parse().unwrap();
112        let old: SimplexVersion = "6.3.2.8".parse().unwrap();
113
114        let min_supported = SimplexVersion::new(6, 4, 5, 2);
115        let max_supported = SimplexVersion::new(6, 4, 10, 0);
116
117        assert!(current >= min_supported && current <= max_supported);
118        assert!(!(old >= min_supported && old <= max_supported));
119    }
120}