Skip to content

Commit

Permalink
impl dbg
Browse files Browse the repository at this point in the history
  • Loading branch information
splurf committed Jan 16, 2024
1 parent 0c79c57 commit a93261e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ mod util;
use {cfg::*, err::*, util::*};

fn main() -> Result<()> {
let mut client = BaseClient::setup()?;
let mut client = dbg("Initializing client", || BaseClient::setup())?;

loop {
match routine(&mut client) {
match dbg("Doing routine", || routine(&mut client)) {
Ok(patch) => {
if let Some((old, new)) = patch {
println!("PATCH: {} => {}", old, new)
}
}
Err(e) => eprintln!("{}", e),
}
delay()
dbg("Delaying", || Ok(client.delay()))?;
}
}
23 changes: 18 additions & 5 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use {
crate::{BaseClient, Result},
std::{net::IpAddr, thread::sleep, time::Duration},
std::{
io::{stdout, Write},
net::IpAddr,
},
};

pub const DELAY: Duration = Duration::from_secs(300);

pub fn routine(client: &mut BaseClient) -> Result<Option<(IpAddr, IpAddr)>> {
let current = client.public_ip()?;
let record = client.get()?;
Expand All @@ -17,6 +18,18 @@ pub fn routine(client: &mut BaseClient) -> Result<Option<(IpAddr, IpAddr)>> {
})
}

pub fn delay() {
sleep(DELAY)
pub fn dbg<T, F: FnOnce() -> Result<T>>(msg: &'static str, f: F) -> Result<T> {
let mut stdout = stdout();
stdout.write_fmt(format_args!("{}...", msg))?;
stdout.flush()?;

let result = f();

stdout.write_fmt(format_args!(
" {}\n",
if result.is_ok() { "Done" } else { "Failed" }
))?;

stdout.flush()?;
result
}

0 comments on commit a93261e

Please sign in to comment.