Skip to content

refactor: remove all regexes #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions adb_client/src/emulator_device/adb_emulator_device.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
use std::{
net::{Ipv4Addr, SocketAddrV4},
sync::LazyLock,
};
use std::net::{Ipv4Addr, SocketAddrV4};

use crate::{ADBServerDevice, ADBTransport, Result, RustADBError, TCPEmulatorTransport};
use regex::Regex;

static EMULATOR_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new("^emulator-(?P<port>\\d+)$").expect("wrong syntax for emulator regex")
});
#[must_use]
fn get_emulator_port(id: &str) -> Option<&str> {
const PREFIX: &str = "emulator-";
if id.len() <= PREFIX.len() || !id.starts_with(PREFIX) {
return None;
}
// bounds-check already done above
let suffix = &id[PREFIX.len()..];
if suffix.bytes().all(|c| c.is_ascii_digit()) {
return Some(suffix);
}
None
}

/// Represents an emulator connected to the ADB server.
#[derive(Debug)]
Expand All @@ -24,20 +30,23 @@ impl ADBEmulatorDevice {
pub fn new(identifier: String, ip_address: Option<Ipv4Addr>) -> Result<Self> {
let ip_address = match ip_address {
Some(ip_address) => ip_address,
None => Ipv4Addr::new(127, 0, 0, 1),
None => Ipv4Addr::LOCALHOST,
};

let groups = EMULATOR_REGEX
.captures(&identifier)
let port = identifier
.lines()
.find_map(|id| {
const PREFIX: &str = "emulator-";
if id.len() <= PREFIX.len() || !id.starts_with(PREFIX) {
return None;
}
// bounds-check already done above
Some(&id[PREFIX.len()..])
})
.ok_or(RustADBError::DeviceNotFound(format!(
"Device {} is likely not an emulator",
identifier
)))?;

let port = groups
.name("port")
.ok_or(RustADBError::RegexParsingError)?
.as_str()
)))?
.parse::<u16>()?;

let socket_addr = SocketAddrV4::new(ip_address, port);
Expand Down
111 changes: 60 additions & 51 deletions adb_client/src/server/models/device_long.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
use std::str::FromStr;
use std::sync::LazyLock;
use std::{fmt::Display, str};

use crate::{DeviceState, RustADBError};
use regex::bytes::Regex;

static DEVICES_LONG_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^(?P<identifier>\S+)\s+(?P<state>\w+)\s+(usb:(?P<usb1>\S+)|(?P<usb2>\S+))?\s*(product:(?P<product>\S+)\s+model:(?P<model>\w+)\s+device:(?P<device>\S+)\s+)?transport_id:(?P<transport_id>\d+)$").expect("cannot build devices long regex")
});

/// Represents a new device with more informations.
#[derive(Debug)]
Expand Down Expand Up @@ -48,53 +42,68 @@ impl TryFrom<&[u8]> for DeviceLong {
type Error = RustADBError;

fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
let groups = DEVICES_LONG_REGEX
.captures(value)
.ok_or(RustADBError::RegexParsingError)?;
let value = str::from_utf8(value)?;
let mut it = value.split_whitespace();

let id = it.next().ok_or(Self::Error::RegexParsingError)?;
let stat = DeviceState::from_str(
it.next()
.ok_or(Self::Error::UnknownDeviceState(String::new()))?,
)?;

let mut comp = it.next().ok_or(Self::Error::RegexParsingError)?;

let mut usb = match comp.strip_prefix("usb:") {
Some(usb) => {
comp = it.next().ok_or(Self::Error::RegexParsingError)?;
Some(usb)
}
_ => None,
};

let prod = match comp.strip_prefix("product:") {
Some(prod) => {
comp = it.next().ok_or(Self::Error::RegexParsingError)?;
Some(prod)
}
_ => {
usb = Some(comp);
comp = it.next().ok_or(Self::Error::RegexParsingError)?;
comp.strip_prefix("product:")
}
};
if prod.is_some() {
comp = it.next().ok_or(Self::Error::RegexParsingError)?
}

let model = comp.strip_prefix("model:");
if model.is_some() {
comp = it.next().ok_or(Self::Error::RegexParsingError)?
}

let dev = comp.strip_prefix("device:");
if dev.is_some() {
comp = it.next().ok_or(Self::Error::RegexParsingError)?
}

if it.next().is_some() {
return Err(Self::Error::RegexParsingError);
}

let trans = comp
.strip_prefix("transport_id:")
.ok_or(Self::Error::UnknownTransport(String::new()))?;

Ok(DeviceLong {
identifier: String::from_utf8(
groups
.name("identifier")
.ok_or(RustADBError::RegexParsingError)?
.as_bytes()
.to_vec(),
)?,
state: DeviceState::from_str(&String::from_utf8(
groups
.name("state")
.ok_or(RustADBError::RegexParsingError)?
.as_bytes()
.to_vec(),
)?)?,
usb: match groups.name("usb1") {
None => match groups.name("usb2") {
None => "Unk".to_string(),
Some(usb) => String::from_utf8(usb.as_bytes().to_vec())?,
},
Some(usb) => String::from_utf8(usb.as_bytes().to_vec())?,
},
product: match groups.name("product") {
None => "Unk".to_string(),
Some(product) => String::from_utf8(product.as_bytes().to_vec())?,
},
model: match groups.name("model") {
None => "Unk".to_string(),
Some(model) => String::from_utf8(model.as_bytes().to_vec())?,
},
device: match groups.name("device") {
None => "Unk".to_string(),
Some(device) => String::from_utf8(device.as_bytes().to_vec())?,
},
transport_id: u32::from_str_radix(
str::from_utf8(
groups
.name("transport_id")
.ok_or(RustADBError::RegexParsingError)?
.as_bytes(),
)?,
16,
)?,
identifier: id.to_string(),
state: stat,
usb: usb.unwrap_or("Unk").to_string(),
product: prod.unwrap_or("Unk").to_string(),
model: model.unwrap_or("Unk").to_string(),
device: dev.unwrap_or("Unk").to_string(),
transport_id: trans
.parse()
.map_err(|_| Self::Error::UnknownTransport(trans.to_string()))?,
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion adb_client/src/transports/tcp_server_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::models::{AdbRequestStatus, SyncCommand};
use crate::{ADBTransport, models::AdbServerCommand};
use crate::{Result, RustADBError};

const DEFAULT_SERVER_IP: Ipv4Addr = Ipv4Addr::new(127, 0, 0, 1);
const DEFAULT_SERVER_IP: Ipv4Addr = Ipv4Addr::LOCALHOST;
const DEFAULT_SERVER_PORT: u16 = 5037;

/// Server transport running on top on TCP
Expand Down