diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2025-09-24 00:58:09 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2025-09-24 00:58:09 +0800 |
| commit | 12068a53657ddad6a4f1a277dd03e095400f690d (patch) | |
| tree | 675d7043832319643699d7a737844479621b729d /src | |
| parent | 0cfc9d0f4acac43ce20b445ac883b7cf0ca1dc79 (diff) | |
formatmain
Diffstat (limited to 'src')
| -rw-r--r-- | src/client.rs | 5 | ||||
| -rw-r--r-- | src/client/connector.rs | 22 | ||||
| -rw-r--r-- | src/client/connector/httpconnector.rs | 104 | ||||
| -rw-r--r-- | src/client/executor.rs | 24 | ||||
| -rw-r--r-- | src/client/main.rs | 70 | ||||
| -rw-r--r-- | src/tasks.rs | 82 |
6 files changed, 127 insertions, 180 deletions
diff --git a/src/client.rs b/src/client.rs index d5dbd4f..2377549 100644 --- a/src/client.rs +++ b/src/client.rs @@ -12,8 +12,5 @@ pub struct Client<'a> { pub username: String, pub realname: String, pub langs: Option<Vec<Language>>, - pub env: Vec<( - String, - String, - )>, + pub env: Vec<(String, String)>, } diff --git a/src/client/connector.rs b/src/client/connector.rs index 25f8b5c..00d9f3f 100644 --- a/src/client/connector.rs +++ b/src/client/connector.rs @@ -27,23 +27,18 @@ pub trait Connection { fn get_ip(&self) -> Option<IpAddr>; fn force_network(&mut self) { - _ = Executor::new( - &NETWORK_T, - ["true"], - ); + _ = Executor::new(&NETWORK_T, ["true"]); } fn init<'a>(&self, c: &Client<'a, Self>) -> std::result::Result<(), Self::Err> where Self: std::marker::Sized, { - self.ntfy( - format!( - r#"New client: + self.ntfy(format!( + r#"New client: {c} "#, - ), - ) + )) } fn reinit(&mut self) { @@ -89,14 +84,7 @@ pub trait Connection { } Result::Err(e) => { #[cfg(debug_assertions)] - std::eprintln!( - "[{}:{}:{}] {} = {:#?}", - std::file!(), - std::line!(), - std::column!(), - std::stringify!(e), - &&e as &dyn std::fmt::Debug, - ); + std::eprintln!("[{}:{}:{}] {} = {:#?}", std::file!(), std::line!(), std::column!(), std::stringify!(e), &&e as &dyn std::fmt::Debug,); } } } diff --git a/src/client/connector/httpconnector.rs b/src/client/connector/httpconnector.rs index 5973410..59ffc9c 100644 --- a/src/client/connector/httpconnector.rs +++ b/src/client/connector/httpconnector.rs @@ -35,35 +35,29 @@ pub struct HTTPConnector { impl HTTPConnector { pub fn new(pull_url: &str, ntfy_url: &str, ip_getter_url: &str) -> result::Result<Self, HTTPConnectorError> { - Ok( - Self { - network_enforcer: Executor::new( - &NETWORK_T, - ["true"], - ) - .unwrap(), // .unwrap() should never panic - prev: String::new(), - pull_url: Url::parse(pull_url)?, - ntfy_url: Url::parse(ntfy_url)?, - ip_getter_url: match Url::parse(ip_getter_url) { - Ok(url) => url, - Err(_) => Url::parse((pull_url.to_string() + ip_getter_url).as_str())?, - }, - client: match Client::builder().build() { + Ok(Self { + network_enforcer: Executor::new(&NETWORK_T, ["true"]).unwrap(), // .unwrap() should never panic + prev: String::new(), + pull_url: Url::parse(pull_url)?, + ntfy_url: Url::parse(ntfy_url)?, + ip_getter_url: match Url::parse(ip_getter_url) { + Ok(url) => url, + Err(_) => Url::parse((pull_url.to_string() + ip_getter_url).as_str())?, + }, + client: match Client::builder().build() { + Ok(c) => c, + Err(_) => match Client::builder() + .danger_accept_invalid_certs(true) + .build() + { Ok(c) => c, - Err(_) => match Client::builder() + Err(_) => Client::builder() .danger_accept_invalid_certs(true) - .build() - { - Ok(c) => c, - Err(_) => Client::builder() - .danger_accept_invalid_certs(true) - .danger_accept_invalid_hostnames(true) - .build()?, - }, + .danger_accept_invalid_hostnames(true) + .build()?, }, }, - ) + }) } #[allow(clippy::only_used_in_recursion)] @@ -109,28 +103,24 @@ impl Connection for HTTPConnector { // consider StatusCode::NOT_MODIFIED fn update(&self) -> Result<Self::Ok, Self::Err> { - self.handle_response( - || { - self.client - .get( - self.pull_url - .as_str(), - ) - }, - ) + self.handle_response(|| { + self.client + .get( + self.pull_url + .as_str(), + ) + }) } fn ntfy(&self, data: String) -> std::result::Result<(), Self::Err> { - self.handle_response( - || { - self.client - .post( - self.ntfy_url - .as_str(), - ) - .body(data.to_owned()) - }, - )?; + self.handle_response(|| { + self.client + .post( + self.ntfy_url + .as_str(), + ) + .body(data.to_owned()) + })?; Ok(()) } @@ -150,25 +140,21 @@ impl Connection for HTTPConnector { } fn get_ip(&self) -> Option<IpAddr> { - match self.handle_response( - || { - self.client - .get( - self.ip_getter_url - .as_str(), - ) - }, - ) { + match self.handle_response(|| { + self.client + .get( + self.ip_getter_url + .as_str(), + ) + }) { Result::Ok(ip) => Some( ip.parse() .ok()?, ), - Result::Err(_) => match self.handle_response( - || { - self.client - .get("ifconfig.me") - }, - ) { + Result::Err(_) => match self.handle_response(|| { + self.client + .get("ifconfig.me") + }) { Ok(ip) => Some( ip.parse() .ok()?, diff --git a/src/client/executor.rs b/src/client/executor.rs index 8fef869..1e6968c 100644 --- a/src/client/executor.rs +++ b/src/client/executor.rs @@ -8,23 +8,19 @@ pub struct Executor<'a, 't> { impl<'a, 't> Executor<'a, 't> { pub fn new(task: &'t Task, args: impl IntoIterator<Item = &'a str>) -> Result<Self, IntoTaskErr> { - Ok( - Self { - task: task, - args: args - .into_iter() - .collect(), - output: None, - }, - ) + Ok(Self { + task: task, + args: args + .into_iter() + .collect(), + output: None, + }) } pub fn run(&mut self) -> Option<&Result<String, String>> { - self.output = Some( - (self - .task - .exec)(&self.args), - ); + self.output = Some((self + .task + .exec)(&self.args)); self.output .as_ref() } diff --git a/src/client/main.rs b/src/client/main.rs index 68afaa1..0fae222 100644 --- a/src/client/main.rs +++ b/src/client/main.rs @@ -22,47 +22,39 @@ pub(crate) mod executor; fn main() { #[cfg(not(debug_assertions))] { - _ = Signals::new( - [ - SIGHUP, - SIGINT, - SIGQUIT, - SIGTRAP, - SIGABRT, - SIGPIPE, - SIGALRM, - SIGTERM, - SIGUSR1, - SIGUSR2, - SIGCHLD, - SIGCONT, - SIGTSTP, - SIGTTIN, - SIGTTOU, - SIGURG, - SIGXCPU, - SIGXFSZ, - SIGVTALRM, - SIGPROF, - SIGWINCH, - #[cfg( - any( - target_os = "linux", - target_os = "android" - ) - )] - SIGSYS, - ], - ); // discard err + _ = Signals::new([ + SIGHUP, + SIGINT, + SIGQUIT, + SIGTRAP, + SIGABRT, + SIGPIPE, + SIGALRM, + SIGTERM, + SIGUSR1, + SIGUSR2, + SIGCHLD, + SIGCONT, + SIGTSTP, + SIGTTIN, + SIGTTOU, + SIGURG, + SIGXCPU, + SIGXFSZ, + SIGVTALRM, + SIGPROF, + SIGWINCH, + #[cfg( + any( + target_os = "linux", + target_os = "android" + ) + )] + SIGSYS, + ]); // discard err } let ip = "http://127.0.0.1:8081"; - let client = Client::new( - HTTPConnector::new( - ip, ip, "/", - ) - .unwrap(), - ) - .unwrap(); + let client = Client::new(HTTPConnector::new(ip, ip, "/").unwrap()).unwrap(); client.enter_loop(); } diff --git a/src/tasks.rs b/src/tasks.rs index a4cab8b..3fbe694 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -14,11 +14,7 @@ pub enum Name { impl Display for Name { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - write!( - f, - "{:?}", - self - ) + write!(f, "{:?}", self) } } @@ -84,56 +80,48 @@ fn network(switch: &[&str]) -> Result<String, String> { Ok(devs) => { match Command::new("awk") .arg(r#"/Wi-Fi|AirPort/{getline; print $2}"#) - .stdin( - Stdio::from( - match devs.stdout { - Some(s) => s, - None => match devs.stderr { - Some(mut e) => { - let mut reason = String::new(); - if let Err(e) = e.read_to_string(&mut reason) { - return Err(format!("networksetup failed to list hardware: {e}")); - } - return Err(format!("networksetup failed to list hardware: {reason}")); - } - None => Err("networksetup failed to list hardware".to_string())?, - }, - }, - ), - ) + .stdin(Stdio::from(match devs.stdout { + Some(s) => s, + None => match devs.stderr { + Some(mut e) => { + let mut reason = String::new(); + if let Err(e) = e.read_to_string(&mut reason) { + return Err(format!("networksetup failed to list hardware: {e}")); + } + return Err(format!("networksetup failed to list hardware: {reason}")); + } + None => Err("networksetup failed to list hardware".to_string())?, + }, + })) .stdout(Stdio::piped()) .spawn() { Ok(dev) => match Command::new("networksetup") .arg("-setairportpower") - .arg( - match dev.stdout { - Some(mut o) => { - let mut output = String::new(); - if let Err(e) = o.read_to_string(&mut output) { + .arg(match dev.stdout { + Some(mut o) => { + let mut output = String::new(); + if let Err(e) = o.read_to_string(&mut output) { + return Err(format!("awk failed to get device from networksetup list hardware: {e}")); + }; + output + } + None => match dev.stderr { + Some(mut e) => { + let mut reason = String::new(); + if let Err(e) = e.read_to_string(&mut reason) { return Err(format!("awk failed to get device from networksetup list hardware: {e}")); - }; - output - } - None => match dev.stderr { - Some(mut e) => { - let mut reason = String::new(); - if let Err(e) = e.read_to_string(&mut reason) { - return Err(format!("awk failed to get device from networksetup list hardware: {e}")); - } - return Err(format!("awk failed to get device from networksetup list hardware: {reason}")); } - None => Err("awk failed to get device from networksetup list hardware".to_string())?, - }, - }, - ) - .arg( - if switch { - "on" - } else { - "off" + return Err(format!("awk failed to get device from networksetup list hardware: {reason}")); + } + None => Err("awk failed to get device from networksetup list hardware".to_string())?, }, - ) + }) + .arg(if switch { + "on" + } else { + "off" + }) .output() { Ok(o) => { |
