From: Scott Worley Date: Wed, 11 Oct 2023 00:08:19 +0000 (-0700) Subject: server: Extract merge_message() X-Git-Tag: v0.1.0~6 X-Git-Url: http://git.scottworley.com/tattlekey/commitdiff_plain/c54403e6dc4829379e3d933f33da344c26fb7c1d?ds=sidebyside server: Extract merge_message() --- diff --git a/server/src/main.rs b/server/src/main.rs index e7351db..bf4f4fc 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -80,6 +80,16 @@ impl Range { } } +fn merge_message(presses: &mut HashMap, message: Message) { + if let Some(r) = presses.get_mut(&message.key) { + if !r.contains(&message.t) { + r.extend(&message.t); + } + } else { + presses.insert(message.key, Range::new(&message.t)); + } +} + fn open_log_for_writing() -> csv::Writer { let log_file_exists = std::path::Path::new(LOGFILENAME).exists(); let logfile = std::fs::OpenOptions::new() @@ -110,13 +120,7 @@ fn main() { log.serialize((&message.key, message.t)) .expect("Couldn't write log"); log.flush().expect("Couldn't flush log"); - if let Some(r) = presses.get_mut(&message.key) { - if !r.contains(&message.t) { - r.extend(&message.t); - } - } else { - presses.insert(message.key, Range::new(&message.t)); - } + merge_message(&mut presses, message); } } }