(&mut self, os: &Os)
| 381 | } |
| 382 | |
| 383 | pub async fn validate(&mut self, os: &Os) -> Result<()> { |
| 384 | // Rather than throwing an error, let invoke() handle this case |
| 385 | if let Some(id) = self.get_id() { |
| 386 | if !os.fs.exists(id_to_path(os, &id)?) { |
| 387 | return Ok(()); |
| 388 | } |
| 389 | } |
| 390 | match self { |
| 391 | TodoList::Create { |
| 392 | tasks, |
| 393 | todo_list_description: task_description, |
| 394 | } => { |
| 395 | if tasks.is_empty() { |
| 396 | bail!("No tasks were provided"); |
| 397 | } else if tasks.iter().any(|task| task.trim().is_empty()) { |
| 398 | bail!("Tasks cannot be empty"); |
| 399 | } else if task_description.is_empty() { |
| 400 | bail!("No task description was provided"); |
| 401 | } |
| 402 | }, |
| 403 | TodoList::Complete { |
| 404 | completed_indices, |
| 405 | context_update, |
| 406 | current_id, |
| 407 | .. |
| 408 | } => { |
| 409 | let state = TodoListState::load(os, current_id).await?; |
| 410 | if completed_indices.is_empty() { |
| 411 | bail!("At least one completed index must be provided"); |
| 412 | } else if context_update.is_empty() { |
| 413 | bail!("No context update was provided"); |
| 414 | } |
| 415 | for i in completed_indices.iter() { |
| 416 | if *i >= state.tasks.len() { |
| 417 | bail!("Index {i} is out of bounds for length {}, ", state.tasks.len()); |
| 418 | } |
| 419 | } |
| 420 | }, |
| 421 | TodoList::Add { |
| 422 | new_tasks, |
| 423 | insert_indices, |
| 424 | new_description, |
| 425 | current_id: id, |
| 426 | } => { |
| 427 | let state = TodoListState::load(os, id).await?; |
| 428 | if new_tasks.iter().any(|task| task.trim().is_empty()) { |
| 429 | bail!("New tasks cannot be empty"); |
| 430 | } else if has_duplicates(insert_indices) { |
| 431 | bail!("Insertion indices must be unique") |
| 432 | } else if new_tasks.len() != insert_indices.len() { |
| 433 | bail!("Must provide an index for every new task"); |
| 434 | } else if new_description.is_some() && new_description.as_ref().unwrap().trim().is_empty() { |
| 435 | bail!("New description cannot be empty"); |
| 436 | } |
| 437 | for i in insert_indices.iter() { |
| 438 | if *i > state.tasks.len() { |
| 439 | bail!("Index {i} is out of bounds for length {}, ", state.tasks.len()); |
| 440 | } |
nothing calls this directly
no test coverage detected