Error Handling with Result and Option (page 32)
This commit is contained in:
19
src/main.rs
19
src/main.rs
@@ -77,7 +77,26 @@ impl TodoList {
|
|||||||
fs::write(filename, json)?;
|
fs::write(filename, json)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn add_task(&mut self, description: String) -> Result<(), TodoError> {
|
||||||
|
if description.trim().is_empty() {
|
||||||
|
return Err(TodoError::InvalidInput(
|
||||||
|
"Task description cannot be empty".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let task = Task {
|
||||||
|
id: self.next_id,
|
||||||
|
description,
|
||||||
|
completed: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
self.tasks.push(task);
|
||||||
|
self.next_id += 1;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let cli = Cli::parse();
|
let cli = Cli::parse();
|
||||||
match cli.command {
|
match cli.command {
|
||||||
|
|||||||
Reference in New Issue
Block a user