Skip to content

Commit

Permalink
Use custom unescape code and remove dep
Browse files Browse the repository at this point in the history
  • Loading branch information
nbdd0121 committed Apr 20, 2024
1 parent 57afd66 commit cf9277c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
8 changes: 1 addition & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ clap = { version = "4", features = ["default", "derive"] }
clap-verbosity-flag = "2"
humantime = "2"
bytes = "1"
thiserror = "1"
tokio = { version = "1", features = ["full"] }
tokio-stream = "0.1"
tokio-util = { version = "0.7", features = ["full"] }
async-stream = "0.3"
unescape = "0.1"
udev = "0.8"
bollard = "0.16"
futures = "0.3"
Expand Down
4 changes: 2 additions & 2 deletions src/dev/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl Device {
})
.or_else(|| {
let vendor = self.device.property_value("ID_VENDOR_ENC")?.to_str()?;
let vendor = unescape::unescape(vendor)?;
let vendor = crate::util::escape::unescape_devnode(vendor).ok()?;
Some(vendor)
})?;

Expand All @@ -57,7 +57,7 @@ impl Device {
})
.or_else(|| {
let model = self.device.property_value("ID_MODEL_ENC")?.to_str()?;
let model = unescape::unescape(model)?;
let model = crate::util::escape::unescape_devnode(model).ok()?;
Some(model)
})?;

Expand Down
32 changes: 32 additions & 0 deletions src/util/escape.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#[derive(thiserror::Error, Debug)]
#[error("Invalid escape sequence")]
pub struct InvalidEscapeError;

/// Unescape a udev-escaped devnode name.
pub fn unescape_devnode(escaped: &str) -> Result<String, InvalidEscapeError> {
let mut result = String::with_capacity(escaped.len());
let mut iter = escaped.chars();

while let Some(c) = iter.next() {
if c != '\\' {
result.push(c);
continue;
}

// Udev escaped devnode names only use hexadecimal escapes, so we only need to handle those.
if iter.next() != Some('x') {
return Err(InvalidEscapeError);
}

let hex = iter.as_str().get(..2).ok_or(InvalidEscapeError)?;
result.push(
u8::from_str_radix(hex, 16)
.map_err(|_| InvalidEscapeError)?
.into(),
);
iter.next();
iter.next();
}

Ok(result)
}
1 change: 1 addition & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod escape;
pub mod tty_mode_guard;

0 comments on commit cf9277c

Please sign in to comment.