srctrait_note/
config.rs

1use std::path::{Path, PathBuf};
2use derive_builder as builder;
3use tomlx::{FromToml, ToStarterToml, ToToml};
4use crate::*;
5
6pub const NOTES_DIR_NAME: &'static str = "notes";
7
8const EDITOR_CMD: &'static str ="$EDITOR";
9
10#[derive(Debug, builder::Builder, Clone, serde::Serialize, serde::Deserialize)]
11pub struct NoteConfig {
12    /// should default to ~/Documents/notes equivalent on OS
13    notes_dir: PathBuf,
14    editor: String,
15}
16
17#[derive(Default, Debug, Clone, serde::Serialize, serde::Deserialize, builder::Builder)]
18#[builder(default)]
19pub struct NoteConfigInput {
20    /// defaults to ~/Documents/notes equivalent on OS
21    notes_dir: Option<PathBuf>,
22    /// defaults to $EDITOR
23    editor: Option<String>,
24}
25
26impl NoteConfigInput {
27    pub fn builder() -> NoteConfigInputBuilder {
28        NoteConfigInputBuilder::default()
29    }
30}
31
32impl FromToml for NoteConfigInput {}
33
34impl NoteConfig {
35    pub fn builder() -> NoteConfigBuilder {
36        NoteConfigBuilder::create_empty()
37    }
38
39    pub fn from_input(
40        input: NoteConfigInput,
41        user_documents_dir: PathBuf
42    ) -> Self
43    {
44        Self {
45            notes_dir: input.notes_dir
46                .unwrap_or_else(|| user_documents_dir.join(NOTES_DIR_NAME)),
47            editor: input.editor
48                .unwrap_or_else(|| EDITOR_CMD.to_string()),
49        }
50    }
51
52    pub fn notes_dir(&self) -> &Path {
53        &self.notes_dir
54    }
55
56    pub fn editor(&self) -> &str {
57        &self.editor
58    }
59}
60
61impl FromToml for NoteConfig {}
62impl ToToml for NoteConfig {}
63impl ToStarterToml for NoteConfig {}