simplify main loop

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin 2024-07-19 11:13:02 -07:00
parent 399f238865
commit 471a70bbfa

View file

@ -7,7 +7,7 @@ use noteguard::{Action, InputMessage, NoteFilter, OutputMessage};
use serde::de::DeserializeOwned;
use serde::Deserialize;
use std::collections::HashMap;
use std::io::{self, BufRead, Read, Write};
use std::io::{self, Read};
use log::info;
#[derive(Deserialize)]
@ -113,6 +113,11 @@ fn main() {
noteguard();
}
fn serialize_output_message(msg: &OutputMessage) -> String {
serde_json::to_string(msg).expect("OutputMessage should always serialize correctly")
}
fn noteguard() {
env_logger::init();
info!("running noteguard");
@ -133,14 +138,17 @@ fn noteguard() {
.expect("Expected filter config to be loaded ok");
let stdin = io::stdin();
let stdout = io::stdout();
let handle = stdout.lock();
let mut writer = io::BufWriter::new(handle);
for line in stdin.lock().lines() {
match line {
Ok(input) => {
let input_message: InputMessage = match serde_json::from_str(&input) {
for line in stdin.lines() {
let line = match line {
Ok(line) => line,
Err(e) => {
eprintln!("Failed to get line: {}", e);
continue;
}
};
let input_message: InputMessage = match serde_json::from_str(&line) {
Ok(msg) => msg,
Err(e) => {
eprintln!("Failed to parse input: {}", e);
@ -149,25 +157,21 @@ fn noteguard() {
};
if input_message.message_type != "new" {
eprintln!("Unexpected request type");
let out = OutputMessage::new(input_message.event.id.clone(), Action::Reject, Some("invalid strfry write policy input".to_string()));
println!("{}", serialize_output_message(&out));
continue;
}
let output_message = noteguard.run(input_message);
let out = noteguard.run(input_message);
let json = serialize_output_message(&out);
println!("{}", json);
}
}
match serde_json::to_string(&output_message) {
Ok(json) => {
writeln!(writer, "{}", json).unwrap();
writer.flush().unwrap();
}
Err(e) => {
eprintln!("Failed to serialize output: {}", e);
}
}
}
Err(e) => {
eprintln!("Failed to read line: {}", e);
}
}
}
}