From 575489e3f7335a6c903b6f1d98832eaa27f244ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakob=20R=C3=B6nnb=C3=A4ck?= Date: Thu, 5 Feb 2026 11:17:32 +0100 Subject: [PATCH] Error Handling with Result and Option (page 32) --- src/main.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/main.rs b/src/main.rs index f159129..a150264 100644 --- a/src/main.rs +++ b/src/main.rs @@ -77,7 +77,26 @@ impl TodoList { fs::write(filename, json)?; 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() { let cli = Cli::parse(); match cli.command {