simplify main loop
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
parent
399f238865
commit
471a70bbfa
1 changed files with 37 additions and 33 deletions
48
src/main.rs
48
src/main.rs
|
|
@ -7,7 +7,7 @@ use noteguard::{Action, InputMessage, NoteFilter, OutputMessage};
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::io::{self, BufRead, Read, Write};
|
use std::io::{self, Read};
|
||||||
use log::info;
|
use log::info;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
|
|
@ -113,6 +113,11 @@ fn main() {
|
||||||
noteguard();
|
noteguard();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn serialize_output_message(msg: &OutputMessage) -> String {
|
||||||
|
serde_json::to_string(msg).expect("OutputMessage should always serialize correctly")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
fn noteguard() {
|
fn noteguard() {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
info!("running noteguard");
|
info!("running noteguard");
|
||||||
|
|
@ -133,14 +138,17 @@ fn noteguard() {
|
||||||
.expect("Expected filter config to be loaded ok");
|
.expect("Expected filter config to be loaded ok");
|
||||||
|
|
||||||
let stdin = io::stdin();
|
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() {
|
for line in stdin.lines() {
|
||||||
match line {
|
let line = match line {
|
||||||
Ok(input) => {
|
Ok(line) => line,
|
||||||
let input_message: InputMessage = match serde_json::from_str(&input) {
|
Err(e) => {
|
||||||
|
eprintln!("Failed to get line: {}", e);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let input_message: InputMessage = match serde_json::from_str(&line) {
|
||||||
Ok(msg) => msg,
|
Ok(msg) => msg,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("Failed to parse input: {}", e);
|
eprintln!("Failed to parse input: {}", e);
|
||||||
|
|
@ -149,25 +157,21 @@ fn noteguard() {
|
||||||
};
|
};
|
||||||
|
|
||||||
if input_message.message_type != "new" {
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let output_message = noteguard.run(input_message);
|
let out = noteguard.run(input_message);
|
||||||
|
let json = serialize_output_message(&out);
|
||||||
|
|
||||||
match serde_json::to_string(&output_message) {
|
println!("{}", json);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue