Skip to main content

device_tests/
model.rs

1#[cfg(test)]
2mod tests;
3
4use core::fmt;
5
6/// Path of the file with the firmware revision.
7pub const PROJECT_VERSION: &str = "/etc/quectel-project-version";
8
9/// Module variant.
10#[derive(Clone, Copy, Debug, PartialEq, Eq)]
11pub enum Model {
12    /// AG35 EVA.
13    Eva,
14    /// AG35 EVD.
15    Evd,
16}
17
18/// The `Rev` line of the project version file.
19#[derive(Clone, Debug, PartialEq, Eq)]
20pub struct Revision {
21    /// The firmware revision, for example `AG35EVDR08A13T4G_OCPU_01.001.02`.
22    pub revision: String,
23    /// The model that the revision names.
24    pub model: Option<Model>,
25}
26
27impl Model {
28    const fn as_str(self) -> &'static str {
29        match self {
30            Self::Eva => "AG35EVA",
31            Self::Evd => "AG35EVD",
32        }
33    }
34
35    fn from_revision(revision: &str) -> Option<Self> {
36        [Self::Eva, Self::Evd]
37            .into_iter()
38            .find(|model| revision.contains(model.as_str()))
39    }
40}
41
42impl fmt::Display for Model {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        f.write_str(self.as_str())
45    }
46}
47
48impl Revision {
49    /// Reads the `Rev` line, for example
50    /// `Project Rev : AG35EVDR08A13T4G_OCPU_01.001.02`.
51    #[must_use]
52    pub fn parse(content: &str) -> Option<Self> {
53        content.lines().find_map(|line| {
54            let (key, value) = line.split_once(':')?;
55            key.trim().ends_with("Rev").then(|| {
56                let revision = value.trim().to_owned();
57                Self {
58                    model: Model::from_revision(&revision),
59                    revision,
60                }
61            })
62        })
63    }
64}