I have the below functions that are the same across several rust struct implementations. I'm quite confused how to refactor these two functions into macros or if I can.
Any pointers would be greatly appreciated. thanks
// convert struct to json
fn to_log(&self) -> String {
match serde_json::to_string(&self) {
Ok(l) => return l,
_ => return "".into()
};
}
// convert struct to json and report it out
pub fn report_log(&self) {
if !ARGS.flag_ip.eq("NONE") {
let socket = format!("{}:{}", ARGS.flag_ip, ARGS.flag_port);
let mut stream = TcpStream::connect(socket)
.expect("Could not connect to server");
stream.write(format!("{}{}", self.to_log(), "\n")
.as_bytes())
.expect("Failed to write to server");
} else {
println!("{}", self.to_log());
}
}