diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2025-09-16 20:27:22 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2025-09-17 21:28:52 +0800 |
| commit | 73b908cb44cf52a768647c0f5cd450ab48cf831f (patch) | |
| tree | 5cec5d442c065a0464626e45e7c61ac97e04d63e | |
| parent | 4517907896246d815d1ab2b4c67b883cd567eae0 (diff) | |
chore: rustfmt config change
| -rw-r--r-- | src/client/client.rs | 12 | ||||
| -rw-r--r-- | src/client/connector.rs | 28 | ||||
| -rw-r--r-- | src/client/connector/httpconnector.rs | 17 | ||||
| -rw-r--r-- | src/client/executor.rs | 11 | ||||
| -rw-r--r-- | src/tasks.rs | 57 |
5 files changed, 34 insertions, 91 deletions
diff --git a/src/client/client.rs b/src/client/client.rs index c833550..36afcf0 100644 --- a/src/client/client.rs +++ b/src/client/client.rs @@ -35,17 +35,7 @@ impl<C: Connection> std::fmt::Display for Client<C> { Real Name: {}\n\ Languages: {:?}\n\ Environment Variables: {:?}\n", - self.ip, - self.hostname, - self.device_name, - self.os, - self.cpu_arch, - self.distro, - self.desktop_env, - self.username, - self.realname, - self.langs, - self.env + self.ip, self.hostname, self.device_name, self.os, self.cpu_arch, self.distro, self.desktop_env, self.username, self.realname, self.langs, self.env ) } } diff --git a/src/client/connector.rs b/src/client/connector.rs index 735a3d3..a595c8e 100644 --- a/src/client/connector.rs +++ b/src/client/connector.rs @@ -42,9 +42,12 @@ pub trait Connection { } fn tick(&mut self) { - // tradeoff: if I put this at the ened of the loop, then the continue would just skip it, and continue doing update() until with no waiting, this wastes lot of cpu/IO - // however, of course we can add a manual sleep before it continue and put this in the end, but that's code duplication - // the cons of the current approch is that the first time we've entered, it would start sleeping + // tradeoff: if I put this at the ened of the loop, then the continue would just + // skip it, and continue doing update() until with no waiting, this wastes lot + // of cpu/IO however, of course we can add a manual sleep before it + // continue and put this in the end, but that's code duplication + // the cons of the current approch is that the first time we've entered, it + // would start sleeping self.reinit(); match self.update() { @@ -59,26 +62,11 @@ pub trait Connection { None => return, }; // FIXME: handle the errors... peak prod code - self.ntfy( - Executor::new(task, it.map(str::to_string)) - .unwrap() - .run() - .unwrap() - .clone() - .unwrap(), - ) - .unwrap(); + self.ntfy(Executor::new(task, it.map(str::to_string)).unwrap().run().unwrap().clone().unwrap()).unwrap(); } 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 d8c0027..a418b97 100644 --- a/src/client/connector/httpconnector.rs +++ b/src/client/connector/httpconnector.rs @@ -16,11 +16,7 @@ pub struct HTTPConnector { } impl HTTPConnector { - pub fn new( - pull_url: &str, - ntfy_url: &str, - ip_getter_url: &str, - ) -> std::result::Result<Self, url::ParseError> { + pub fn new(pull_url: &str, ntfy_url: &str, ip_getter_url: &str) -> std::result::Result<Self, url::ParseError> { Ok(Self { network_enforcer: Executor::new(&NETWORK_T, ["on".to_string()]).unwrap(), // .unwrap() should never panic prev: String::new(), @@ -35,10 +31,7 @@ impl HTTPConnector { } #[allow(clippy::only_used_in_recursion)] - fn handle_response<F: Fn() -> RequestBuilder>( - &self, - req: F, - ) -> Result<<HTTPConnector as Connection>::Ok, <HTTPConnector as Connection>::Err> { + fn handle_response<F: Fn() -> RequestBuilder>(&self, req: F) -> Result<<HTTPConnector as Connection>::Ok, <HTTPConnector as Connection>::Err> { match req().send() { Ok(resp) => match resp.status() { StatusCode::TOO_MANY_REQUESTS => { @@ -84,11 +77,7 @@ impl Connection for HTTPConnector { } 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(()) } diff --git a/src/client/executor.rs b/src/client/executor.rs index ad419f0..32a0fdc 100644 --- a/src/client/executor.rs +++ b/src/client/executor.rs @@ -7,16 +7,9 @@ pub struct Executor { } impl Executor { - pub fn new( - task: impl IntoTask, - args: impl IntoIterator<Item = String>, - ) -> Result<Self, IntoTaskErr> { + pub fn new(task: impl IntoTask, args: impl IntoIterator<Item = String>) -> Result<Self, IntoTaskErr> { let task = task.into_task()?; - 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>> { diff --git a/src/tasks.rs b/src/tasks.rs index a12baba..bfa24e8 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -9,22 +9,9 @@ impl PartialEq for &Task { } } -pub const TASKS: &[Task] = &[ - Task { - name: "nop", - exec: no_op, - }, - Task { - name: "exec", - exec: exec, - }, - NETWORK_T, -]; +pub const TASKS: &[Task] = &[Task { name: "nop", exec: no_op }, Task { name: "exec", exec: exec }, NETWORK_T]; -pub const NETWORK_T: Task = Task { - name: "network", - exec: network, -}; +pub const NETWORK_T: Task = Task { name: "network", exec: network }; fn no_op(_: &[String]) -> Result<String, String> { Ok("".to_string()) @@ -32,7 +19,8 @@ fn no_op(_: &[String]) -> Result<String, String> { fn exec(cmds: &[String]) -> Result<String, String> { use std::process::Command; - // TODO: fmt and add exit code, and add support for both stdout and stderr, and support for no output + // TODO: fmt and add exit code, and add support for both stdout and stderr, and + // support for no output match Command::new(cmds[0].as_str()).args(&cmds[1..]).output() { Ok(o) => { if o.status.success() { @@ -58,13 +46,9 @@ fn network(switch: &[String]) -> Result<String, String> { #[cfg(target_os = "macos")] { use std::io::Read; - match Command::new("networksetup") - .arg("-listallhardwareports") - .stdout(Stdio::piped()) - .spawn() - { + match Command::new("networksetup").arg("-listallhardwareports").stdout(Stdio::piped()).spawn() { Ok(devs) => { - match Command::new("awk") + match Command::new("awk") .arg(r#"/Wi-Fi|AirPort/{getline; print $2}"#) .stdin(Stdio::from(match devs.stdout { Some(s) => s, @@ -72,11 +56,9 @@ fn network(switch: &[String]) -> Result<String, String> { 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: {e}")); } - return Err(format!( - "networksetup failed to list hardware: {reason}" - )); + return Err(format!("networksetup failed to list hardware: {reason}")); } None => Err("networksetup failed to list hardware".to_string())?, }, @@ -84,34 +66,35 @@ fn network(switch: &[String]) -> Result<String, String> { .stdout(Stdio::piped()) .spawn() { - Ok(dev) => match Command::new("networksetup").arg("-setairportpower").arg( - match dev.stdout { + 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) { - 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: {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}")) + 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" }).output() { + }) + .arg(if switch { "on" } else { "off" }) + .output() + { Ok(o) => { if o.status.success() { - Ok(String::from_utf8_lossy(&o.stdout) - .to_string()) + Ok(String::from_utf8_lossy(&o.stdout).to_string()) } else { - Err(String::from_utf8_lossy(&o.stderr) - .to_string()) + Err(String::from_utf8_lossy(&o.stderr).to_string()) } } Err(e) => Err(e.to_string()), |
