Compare commits
2 Commits
1b945d09b1
...
cc393ce2c0
Author | SHA1 | Date | |
---|---|---|---|
cc393ce2c0 | |||
7a8d6e0e74 |
@ -7,3 +7,4 @@ edition = "2024"
|
||||
chrono = { version = "0.4.41", features = ["serde"] }
|
||||
serde = { version = "1.0.219", features = ["derive"] }
|
||||
serde_json = "1.0.141"
|
||||
uuid = { version = "1.17.0", features = ["serde", "v4"] }
|
||||
|
@ -19,9 +19,14 @@ impl App {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn add(&mut self, item: Item) {
|
||||
pub fn add(&mut self, item: Item) -> Result<()> {
|
||||
self.logs.push(item);
|
||||
let _ = self.save();
|
||||
self.save()
|
||||
}
|
||||
|
||||
pub fn remove<T: AsRef<str>>(&mut self, id: T) -> Result<()> {
|
||||
self.logs.retain(|i| i.id() != id.as_ref());
|
||||
self.save()
|
||||
}
|
||||
|
||||
pub fn save(&self) -> Result<()> {
|
||||
|
@ -1,8 +1,10 @@
|
||||
use chrono::{DateTime, Local};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct Item {
|
||||
id: Uuid,
|
||||
content: String,
|
||||
created: DateTime<Local>,
|
||||
modified: DateTime<Local>,
|
||||
@ -17,12 +19,17 @@ impl Item {
|
||||
self.content = content;
|
||||
self.modified = Local::now();
|
||||
}
|
||||
|
||||
pub fn id(&self) -> String {
|
||||
self.id.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Item {
|
||||
fn default() -> Self {
|
||||
let now = Local::now();
|
||||
Self {
|
||||
id: Uuid::new_v4(),
|
||||
content: String::new(),
|
||||
created: now,
|
||||
modified: now,
|
||||
|
15
src/main.rs
15
src/main.rs
@ -1,8 +1,15 @@
|
||||
use lw::App;
|
||||
use std::error::Error;
|
||||
|
||||
fn main() {
|
||||
use lw::{App, log::Item};
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let mut app = App::default();
|
||||
app.add("hello_world".into());
|
||||
app.save().unwrap();
|
||||
let item = Item::from("hello_world");
|
||||
let id = item.id();
|
||||
app.add(item)?;
|
||||
println!("{app:?}");
|
||||
app.remove(id)?;
|
||||
app.save()?;
|
||||
println!("{app:?}");
|
||||
Ok(())
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user