Skip to main content

commit_wizard/engine/fs/
mod.rs

1use std::fs;
2use std::path::Path;
3
4use serde::Serialize;
5use serde::de::DeserializeOwned;
6
7use crate::engine::{ErrorCode, Result};
8
9pub fn read_text(path: &Path) -> Result<String> {
10    fs::read_to_string(path).map_err(|err| {
11        ErrorCode::IoFailure
12            .error()
13            .with_context("operation", "load_file")
14            .with_context("path", path.display().to_string())
15            .with_context("error", err.to_string())
16    })
17}
18
19pub fn write_text(path: &Path, content: &str) -> Result<()> {
20    if let Some(parent) = path.parent() {
21        create_dir_all(parent)?;
22    }
23
24    fs::write(path, content).map_err(|err| {
25        ErrorCode::IoFailure
26            .error()
27            .with_context("operation", "save_file")
28            .with_context("path", path.display().to_string())
29            .with_context("error", err.to_string())
30    })
31}
32
33pub fn create_dir_all(path: &Path) -> Result<()> {
34    fs::create_dir_all(path).map_err(|err| {
35        ErrorCode::IoFailure
36            .error()
37            .with_context("operation", "create_dir_all")
38            .with_context("path", path.display().to_string())
39            .with_context("error", err.to_string())
40    })
41}
42
43pub fn current_dir() -> Result<std::path::PathBuf> {
44    std::env::current_dir().map_err(|err| {
45        ErrorCode::IoFailure
46            .error()
47            .with_context("operation", "current_dir")
48            .with_context("error", err.to_string())
49    })
50}
51pub fn load_json<T>(path: &Path) -> Result<T>
52where
53    T: DeserializeOwned,
54{
55    let raw = read_text(path)?;
56
57    serde_json::from_str(&raw).map_err(|err| {
58        ErrorCode::SerializationFailure
59            .error()
60            .with_context("operation", "load_json")
61            .with_context("path", path.display().to_string())
62            .with_context("error", err.to_string())
63    })
64}
65
66pub fn save_json<T>(path: &Path, value: &T) -> Result<()>
67where
68    T: Serialize,
69{
70    let raw = serde_json::to_string_pretty(value).map_err(|err| {
71        ErrorCode::SerializationFailure
72            .error()
73            .with_context("operation", "save_json")
74            .with_context("path", path.display().to_string())
75            .with_context("error", err.to_string())
76    })?;
77
78    write_text(path, &raw)
79}
80
81pub fn load_toml<T>(path: &Path) -> Result<T>
82where
83    T: DeserializeOwned,
84{
85    let raw = read_text(path)?;
86
87    toml::from_str(&raw).map_err(|err| {
88        ErrorCode::SerializationFailure
89            .error()
90            .with_context("operation", "load_toml")
91            .with_context("path", path.display().to_string())
92            .with_context("error", err.to_string())
93    })
94}
95
96pub fn save_toml<T>(path: &Path, value: &T) -> Result<()>
97where
98    T: Serialize,
99{
100    let raw = toml::to_string_pretty(value).map_err(|err| {
101        ErrorCode::SerializationFailure
102            .error()
103            .with_context("operation", "save_toml")
104            .with_context("path", path.display().to_string())
105            .with_context("error", err.to_string())
106    })?;
107
108    write_text(path, &raw)
109}