use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Entry {
pub id: Uuid,
pub title: String,
pub body: String,
pub date: NaiveDateTime,
pub tags: Vec<String>,
pub starred: bool,
}
impl Entry {
pub fn new(input: &str, date: NaiveDateTime) -> Self {
let id = Uuid::new_v4();
let is_starred = input.trim_start().starts_with("*");
let cleaned_input = input.trim_start_matches("*").trim();
let mut title = cleaned_input;
let mut body = "";
if let Some((t, b)) = cleaned_input.split_once(".") {
title = t.trim();
body = b.trim();
}
let tags = extract_tags(cleaned_input);
Entry {
id: id,
title: title.to_string(),
body: body.to_string(),
starred: is_starred,
tags: tags,
date: date,
}
}
}
fn extract_tags(cleaned_input: &str) -> Vec<String> {
println!("{}", cleaned_input);
cleaned_input
.split_whitespace()
.filter_map(|word| {
if word.starts_with("#") {
Some(word.trim_start_matches("#").to_string())
} else {
None
}
})
.collect()
}