Skip to content

Commit

Permalink
Merge pull request #54 from rien/feature/unknown-model-handling
Browse files Browse the repository at this point in the history
Replace Model::Unknown with UnknownVersion error
  • Loading branch information
LinusCDE authored Oct 21, 2021
2 parents c5ffd9c + b47a53f commit b5a0ea1
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@ use once_cell::sync::Lazy;
pub enum Model {
Gen1,
Gen2,
Unknown,
}

impl std::fmt::Display for Model {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match *self {
Model::Gen1 => write!(f, "reMarkable 1"),
Model::Gen2 => write!(f, "reMarkable 2"),
Model::Unknown => write!(f, "Unknown reMarkable"),
}
}
}

impl Model {
fn current_model() -> std::io::Result<Model> {
pub fn current_model() -> Result<Model, ErrorKind> {
let content = std::fs::read_to_string("/sys/devices/soc0/machine")?;
let machine_name = content.trim();
// "reMarkable Prototype 1" was also seen for reMarkable 1 owners (and it didn't mean they preordered it).
Expand All @@ -30,13 +28,39 @@ impl Model {
} else if machine_name == "reMarkable 2.0" {
Ok(Model::Gen2)
} else {
Ok(Model::Unknown)
Err(ErrorKind::UnknownVersion(machine_name.to_owned()))
}
}
}

pub static CURRENT_DEVICE: Lazy<Device> = Lazy::new(Device::new);

/// Differentiate between the reasons why the determination of the current device model can fail.
#[derive(Debug)]
pub enum ErrorKind {
/// An IO error occured when reading /sys/devices/soc0/machine
IOError(std::io::Error),
/// The version string in /sys/devices/soc0/machine does not match any of the known versions.
UnknownVersion(String),
}

impl From<std::io::Error> for ErrorKind {
fn from(err: std::io::Error) -> Self {
ErrorKind::IOError(err)
}
}

impl std::fmt::Display for ErrorKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ErrorKind::IOError(err) => err.fmt(f),
ErrorKind::UnknownVersion(version) => {
write!(f, "Unknown reMarkable version '{}'", version)
}
}
}
}

/// Mainly information regarding both models
pub struct Device {
pub model: Model,
Expand All @@ -58,8 +82,7 @@ pub struct InputDevicePlacement {
impl Device {
fn new() -> Self {
let model = Model::current_model()
.unwrap_or_else(|e| panic!("Got IO Error when determining model: {}", e));
assert!(model != Model::Unknown, "Failed to determine model!");
.unwrap_or_else(|e| panic!("Got an error when determining model: {}", e));

Self { model }
}
Expand All @@ -76,7 +99,6 @@ impl Device {
invert_x: true,
invert_y: false,
},
Model::Unknown => unreachable!(),
}
}

Expand All @@ -94,7 +116,6 @@ impl Device {
match self.model {
Model::Gen1 => "bq27441-0",
Model::Gen2 => "max77818_battery",
Model::Unknown => unreachable!(),
}
}
}

0 comments on commit b5a0ea1

Please sign in to comment.