rtcom_config/lib.rs
1//! Profile persistence for rtcom.
2#![forbid(unsafe_code)]
3
4pub mod paths;
5pub mod profile;
6
7use std::path::Path;
8
9pub use paths::default_profile_path;
10pub use profile::{ModalStyle, Profile};
11
12/// Errors produced by profile IO.
13#[derive(Debug, thiserror::Error)]
14pub enum Error {
15 /// Underlying filesystem error.
16 #[error("io error: {0}")]
17 Io(#[from] std::io::Error),
18 /// TOML parse error.
19 #[error("parse error: {0}")]
20 Parse(#[from] toml::de::Error),
21 /// TOML serialize error.
22 #[error("serialize error: {0}")]
23 Serialize(#[from] toml::ser::Error),
24}
25
26/// Read a profile from a TOML file at `path`.
27///
28/// # Errors
29///
30/// Returns [`Error::Io`] if the file cannot be opened (including
31/// `NotFound`) and [`Error::Parse`] if the TOML is malformed.
32pub fn read(path: &Path) -> Result<Profile, Error> {
33 let text = std::fs::read_to_string(path)?;
34 Ok(toml::from_str(&text)?)
35}
36
37/// Serialize `profile` to TOML and write it to `path`, creating any missing
38/// parent directories.
39///
40/// # Errors
41///
42/// Returns [`Error::Io`] on filesystem failures and [`Error::Serialize`] if
43/// the profile cannot be serialized (currently unreachable, but kept for
44/// schema additions that may later hit serde's serialize path).
45pub fn write(path: &Path, profile: &Profile) -> Result<(), Error> {
46 if let Some(parent) = path.parent() {
47 std::fs::create_dir_all(parent)?;
48 }
49 let text = toml::to_string_pretty(profile)?;
50 std::fs::write(path, text)?;
51 Ok(())
52}