Error Handling with Result and Option (page 32)

This commit is contained in:
2026-02-05 11:17:32 +01:00
parent 4c77d74985
commit 575489e3f7

View File

@@ -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 {