node: handle [write!] error

This commit is contained in:
hinto-janaiyo 2022-11-20 14:46:43 -05:00
parent f2852549c2
commit 42c89fcdd2
No known key found for this signature in database
GPG key ID: B1C5A64B80691E45
2 changed files with 15 additions and 5 deletions

View file

@ -314,7 +314,7 @@ impl Node {
// Convert [Vec<(String, Self)>] into [String] // Convert [Vec<(String, Self)>] into [String]
// that can be written as a proper TOML file // that can be written as a proper TOML file
pub fn to_string(vec: &Vec<(String, Self)>) -> String { pub fn to_string(vec: &Vec<(String, Self)>) -> Result<String, TomlError> {
let mut toml = String::new(); let mut toml = String::new();
for (key, value) in vec.iter() { for (key, value) in vec.iter() {
write!( write!(
@ -324,9 +324,9 @@ impl Node {
value.ip, value.ip,
value.rpc, value.rpc,
value.zmq, value.zmq,
); )?;
} }
toml Ok(toml)
} }
// Combination of multiple functions: // Combination of multiple functions:
@ -354,7 +354,7 @@ impl Node {
pub fn create_new(path: &PathBuf) -> Result<Vec<(String, Self)>, TomlError> { pub fn create_new(path: &PathBuf) -> Result<Vec<(String, Self)>, TomlError> {
info!("Node | Creating new default..."); info!("Node | Creating new default...");
let new = Self::new_vec(); let new = Self::new_vec();
let string = Self::to_string(&Self::new_vec()); let string = Self::to_string(&Self::new_vec())?;
fs::write(&path, &string)?; fs::write(&path, &string)?;
info!("Node | Write ... OK"); info!("Node | Write ... OK");
Ok(new) Ok(new)
@ -363,7 +363,7 @@ impl Node {
// Save [Node] onto disk file [node.toml] // Save [Node] onto disk file [node.toml]
pub fn save(vec: &Vec<(String, Self)>, path: &PathBuf) -> Result<(), TomlError> { pub fn save(vec: &Vec<(String, Self)>, path: &PathBuf) -> Result<(), TomlError> {
info!("Node | Saving to disk..."); info!("Node | Saving to disk...");
let string = Self::to_string(vec); let string = Self::to_string(vec)?;
match fs::write(path, string) { match fs::write(path, string) {
Ok(_) => { info!("TOML save ... OK"); Ok(()) }, Ok(_) => { info!("TOML save ... OK"); Ok(()) },
Err(err) => { error!("Couldn't overwrite TOML file"); Err(TomlError::Io(err)) }, Err(err) => { error!("Couldn't overwrite TOML file"); Err(TomlError::Io(err)) },
@ -394,6 +394,7 @@ pub enum TomlError {
Serialize(toml::ser::Error), Serialize(toml::ser::Error),
Deserialize(toml::de::Error), Deserialize(toml::de::Error),
Merge(figment::Error), Merge(figment::Error),
Format(std::fmt::Error),
} }
impl Display for TomlError { impl Display for TomlError {
@ -405,6 +406,7 @@ impl Display for TomlError {
Serialize(err) => write!(f, "{}: Serialize | {}", ERROR, err), Serialize(err) => write!(f, "{}: Serialize | {}", ERROR, err),
Deserialize(err) => write!(f, "{}: Deserialize | {}", ERROR, err), Deserialize(err) => write!(f, "{}: Deserialize | {}", ERROR, err),
Merge(err) => write!(f, "{}: Merge | {}", ERROR, err), Merge(err) => write!(f, "{}: Merge | {}", ERROR, err),
Format(err) => write!(f, "{}: Format | {}", ERROR, err),
} }
} }
} }
@ -415,6 +417,12 @@ impl From<std::io::Error> for TomlError {
} }
} }
impl From<std::fmt::Error> for TomlError {
fn from(err: std::fmt::Error) -> Self {
TomlError::Format(err)
}
}
//---------------------------------------------------------------------------------------------------- Const //---------------------------------------------------------------------------------------------------- Const
// State file // State file
const ERROR: &'static str = "Disk error"; const ERROR: &'static str = "Disk error";

View file

@ -184,6 +184,7 @@ impl App {
Path(e) => app.error_state.set(format!("State file: {}", e), ErrorFerris::Panic, ErrorButtons::Quit), Path(e) => app.error_state.set(format!("State file: {}", e), ErrorFerris::Panic, ErrorButtons::Quit),
Serialize(e) => app.error_state.set(format!("State file: {}", e), ErrorFerris::Panic, ErrorButtons::Quit), Serialize(e) => app.error_state.set(format!("State file: {}", e), ErrorFerris::Panic, ErrorButtons::Quit),
Deserialize(e) => app.error_state.set(format!("State file: {}", e), ErrorFerris::Panic, ErrorButtons::Quit), Deserialize(e) => app.error_state.set(format!("State file: {}", e), ErrorFerris::Panic, ErrorButtons::Quit),
Format(e) => app.error_state.set(format!("State file: {}", e), ErrorFerris::Panic, ErrorButtons::Quit),
Merge(e) => app.error_state.set(format!("State file: {}", e), ErrorFerris::Error, ErrorButtons::ResetState), Merge(e) => app.error_state.set(format!("State file: {}", e), ErrorFerris::Error, ErrorButtons::ResetState),
}; };
State::new() State::new()
@ -200,6 +201,7 @@ impl App {
Path(e) => app.error_state.set(format!("Node list: {}", e), ErrorFerris::Panic, ErrorButtons::Quit), Path(e) => app.error_state.set(format!("Node list: {}", e), ErrorFerris::Panic, ErrorButtons::Quit),
Serialize(e) => app.error_state.set(format!("Node list: {}", e), ErrorFerris::Panic, ErrorButtons::Quit), Serialize(e) => app.error_state.set(format!("Node list: {}", e), ErrorFerris::Panic, ErrorButtons::Quit),
Deserialize(e) => app.error_state.set(format!("Node list: {}", e), ErrorFerris::Panic, ErrorButtons::Quit), Deserialize(e) => app.error_state.set(format!("Node list: {}", e), ErrorFerris::Panic, ErrorButtons::Quit),
Format(e) => app.error_state.set(format!("State file: {}", e), ErrorFerris::Panic, ErrorButtons::Quit),
Merge(e) => app.error_state.set(format!("Node list: {}", e), ErrorFerris::Error, ErrorButtons::ResetState), Merge(e) => app.error_state.set(format!("Node list: {}", e), ErrorFerris::Error, ErrorButtons::ResetState),
}; };
Node::new_vec() Node::new_vec()