Step 4: page 29
This commit is contained in:
20
src/main.rs
20
src/main.rs
@@ -1,5 +1,7 @@
|
|||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::fs;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[command(name = "todo")]
|
#[command(name = "todo")]
|
||||||
@@ -30,6 +32,24 @@ struct TodoList {
|
|||||||
next_id: usize,
|
next_id: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TodoList {
|
||||||
|
fn new() -> Self {
|
||||||
|
TodoList {
|
||||||
|
tasks: Vec::new(),
|
||||||
|
next_id: 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load_from_file(filename: &str) -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
|
if Path::new(filename).exists() {
|
||||||
|
let contents = fs::read_to_string(filename)?;
|
||||||
|
let todo_list = serde_json::from_str(&contents)?;
|
||||||
|
Ok(todo_list)
|
||||||
|
} else {
|
||||||
|
Ok(TodoList::new())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
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