error handling

This commit is contained in:
Candifloss 2024-09-27 15:29:14 +05:30
parent 4dfa6e4d3d
commit f568ac111d
2 changed files with 8 additions and 3 deletions

View File

@ -1,9 +1,10 @@
// This module deals with converting the notification object into rson format, which can be used instead of json if preferred
use crate::notification::Notification;
use rson_rs::ser::to_string as rson_string;
use rson_rs::de::Error as RsonError;
impl Notification {
pub fn rson(&self) -> Result<String, String> {
rson_string(self).map_err(|e| format!("Failed to serialize to RSON: {}", e))
pub fn rson(&self) -> Result<String, RsonError> {
rson_string(self).map_err(|e| RsonError::Message(format!("RSON serialization error: {}", e)))
}
}

View File

@ -132,7 +132,11 @@ async fn main() -> Result<()> {
println!("{}", &notif.json()); // Print the json version
}
"r" => {
println!("{}", &notif.rson()); // Print the rson version
// Print the rson version
match notif.rson() {
Ok(rson_string) => println!("{}", rson_string),
Err(e) => eprintln!("Failed to convert to RSON: {}", e),
}
}
"p" => {
println!("{}\n", &notif.plain()); // Print the plain version