Skip to main content

app_json_settings/
core.rs

1use serde::{de::DeserializeOwned, Serialize};
2
3use std::fs;
4use std::io;
5use std::marker::PhantomData;
6use std::path::PathBuf;
7
8use crate::core::constant::DEFAULT_FILE_NAME;
9use crate::core::dir::default_config_dir;
10use crate::core::json::JsonFormat;
11use crate::ConfigError;
12use crate::Result;
13
14pub mod constant;
15mod dir;
16pub mod error;
17mod json;
18
19pub struct ConfigManager<T> {
20    //     app_name: String,
21    folder_path: PathBuf,
22    file_name: String,
23    json_format: JsonFormat,
24    _marker: PhantomData<T>,
25}
26
27impl<T> ConfigManager<T>
28where
29    T: Serialize + DeserializeOwned,
30{
31    /// デフォルト初期化
32    /// フォルダ: OS 標準 config ディレクトリ / app_name
33    pub fn new() -> Self {
34        let folder_path = default_config_dir();
35
36        // let app_name = std::env::current_exe().unwrap_or_default();
37
38        Self {
39            // app_name,
40            folder_path,
41            file_name: DEFAULT_FILE_NAME.to_string(),
42            json_format: JsonFormat::Pretty,
43            _marker: PhantomData,
44        }
45    }
46
47    /// カレントディレクトリに保存
48    pub fn at_current_dir(mut self) -> Self {
49        self.folder_path = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
50        self
51    }
52
53    /// 任意パスに保存
54    pub fn at_custom_dir<P: Into<PathBuf>>(mut self, path: P) -> Self {
55        self.folder_path = path.into();
56        self
57    }
58
59    /// ファイル名変更
60    pub fn with_filename(mut self, name: &str) -> Self {
61        self.file_name = name.to_string();
62        self
63    }
64
65    /// JSON を pretty 形式で保存
66    pub fn disable_pretty_json(mut self) -> Self {
67        self.json_format = JsonFormat::Compact;
68        self
69    }
70
71    /// フルパス取得
72    pub fn path(&self) -> PathBuf {
73        self.folder_path.join(&self.file_name)
74    }
75
76    /// 完全保存(置換書き込み)
77    pub fn save(&self, config: &T) -> Result<()> {
78        if !self.folder_path.exists() {
79            fs::create_dir_all(&self.folder_path)?;
80        }
81
82        let content = match self.json_format {
83            JsonFormat::Compact => serde_json::to_string(config)?,
84            JsonFormat::Pretty => serde_json::to_string_pretty(config)?,
85        };
86
87        fs::write(self.path(), content)?;
88        Ok(())
89    }
90
91    /// ファイルが存在する前提のロード
92    pub fn load(&self) -> Result<T> {
93        let content = fs::read_to_string(self.path())?;
94        Ok(serde_json::from_str(&content)?)
95    }
96}
97
98//
99// Default 対応 API
100//
101
102impl<T> ConfigManager<T>
103where
104    T: Serialize + DeserializeOwned + Default,
105{
106    /// 存在しなければ default を生成して保存
107    pub fn load_or_default(&self) -> Result<T> {
108        let path = self.path();
109
110        match fs::read_to_string(&path) {
111            Ok(content) => Ok(serde_json::from_str(&content)?),
112
113            Err(e) if e.kind() == io::ErrorKind::NotFound => {
114                let default_config = T::default();
115                self.save(&default_config)?;
116                Ok(default_config)
117            }
118
119            Err(e) => Err(ConfigError::Io(e)),
120        }
121    }
122
123    /// 安全な read-modify-write
124    /// save() と並ぶ主要 API
125    pub fn update<F>(&self, f: F) -> Result<T>
126    where
127        F: FnOnce(&mut T),
128    {
129        let mut cfg = self.load_or_default()?;
130        f(&mut cfg);
131        self.save(&cfg)?;
132        Ok(cfg)
133    }
134}