kamiya_database/
lib.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Serialize, Deserialize, Default, Clone)]
4pub struct Note {
5    pub name: String,
6    pub content: String,
7    #[serde(default)]
8    pub description: String,
9}
10
11#[derive(Serialize, Deserialize, Default)]
12pub struct Database {
13    notes: Vec<Note>,
14}
15
16#[derive(Debug)]
17pub enum DatabaseError {
18    NoteNotFound,
19    BadTemplate,
20}
21
22impl Database {
23    pub fn note_exists(&self, name: &str) -> bool {
24        self.notes.iter().any(|item| item.name == *name.to_owned())
25    }
26
27    pub fn remove_note(&mut self, name: &str) -> Result<(), DatabaseError> {
28        match self.get_note_index(name) {
29            Ok(index) => {
30                self.notes.remove(index);
31                Ok(())
32            }
33            Err(e) => Err(e),
34        }
35    }
36
37    pub fn get_notes(&self) -> Vec<Note> {
38        self.notes.clone()
39    }
40
41    pub fn add_note(&mut self, new_note: Note) {
42        self.notes.push(new_note);
43    }
44
45    pub fn notes_count(&self) -> usize {
46        self.notes.len()
47    }
48
49    pub fn set_note_name(&mut self, note_name: &str, new_name: &str) -> Result<(), DatabaseError> {
50        match self.get_note_index(note_name) {
51            Ok(index) => {
52                self.notes[index].name = new_name.to_string();
53                Ok(())
54            }
55            Err(e) => Err(e),
56        }
57    }
58
59    pub fn set_note_content(
60        &mut self,
61        note_name: &str,
62        new_content: &str,
63    ) -> Result<(), DatabaseError> {
64        match self.get_note_index(note_name) {
65            Ok(index) => {
66                self.notes[index].content = new_content.to_string();
67                Ok(())
68            }
69            Err(e) => Err(e),
70        }
71    }
72
73    pub fn set_note_description(
74        &mut self,
75        note_name: &str,
76        new_desc: &str,
77    ) -> Result<(), DatabaseError> {
78        match self.get_note_index(note_name) {
79            Ok(index) => {
80                self.notes[index].description = new_desc.to_string();
81                Ok(())
82            }
83            Err(e) => Err(e),
84        }
85    }
86
87    pub fn get_note_index(&self, name: &str) -> Result<usize, DatabaseError> {
88        match self
89            .notes
90            .iter()
91            .position(|item| item.name == *name.to_owned())
92        {
93            Some(index) => Ok(index),
94            None => Err(DatabaseError::NoteNotFound),
95        }
96    }
97
98    pub fn get_note(&self, name: &str) -> Result<Note, DatabaseError> {
99        match self.get_note_index(name) {
100            Ok(index) => match self.notes.get(index) {
101                Some(i) => Ok(i.clone()),
102                None => Err(DatabaseError::NoteNotFound),
103            },
104            Err(e) => Err(e),
105        }
106    }
107
108    pub fn generate_name(&self, template: &str) -> Result<String, DatabaseError> {
109        if !template.contains("&i") {
110            return Err(DatabaseError::BadTemplate);
111        }
112        let note_number = self.notes.len() + 1;
113        let new_name: String = template.replace("&i", &note_number.to_string());
114        Ok(new_name)
115    }
116}