mod sys;
use std::fmt;
use std::process::ExitStatus;
type Result<T> = std::result::Result<T, Error>;
#[allow(missing_docs)]
#[derive(Debug, PartialEq, Eq)]
pub enum Error {
SyntaxRegexError,
CommandNotFound,
CommandFailed(ExitStatus, String),
NoMatch,
FailedToParse,
NoValue,
HeaderNotFound(&'static str),
InterfaceError(String),
SocketError(String),
UnknownError,
ScanFailed(String),
}
#[derive(Debug, PartialEq, Eq, Default, Clone)]
pub struct Wifi {
pub mac: String,
pub ssid: String,
pub channel: String,
pub signal_level: String,
pub security: String,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::SyntaxRegexError => write!(f, "An error occured during syntax check"),
Error::CommandNotFound => write!(f, "Couldn't find command"),
Error::CommandFailed(status, reason) => {
write!(f, "Command failed with exit status {}: {}", status, reason)
}
Error::NoMatch => write!(f, "Couldn't match"),
Error::FailedToParse => write!(f, "Failed to parse command"),
Error::NoValue => write!(f, "Value expected but is not present"),
Error::HeaderNotFound(header) => {
write!(f, "Did not find header {} but expected it", header)
}
Error::SocketError(detail) => {
write!(f, "Error while creating socket: {}", detail)
}
Error::InterfaceError(detail) => {
write!(f, "Interface error: {}", detail)
}
Error::UnknownError => {
write!(f, "Unknown error occured")
}
Error::ScanFailed(detail) => {
write!(f, "Scan Failed: {}", detail)
}
}
}
}
impl std::error::Error for Error {}
pub fn scan() -> Result<Vec<Wifi>> {
crate::sys::scan()
}