Skip to content

Commit 6b2c66d

Browse files
committed
host Settings::title now app_name; Vfs knows about user data directories now; cfg now uses vfs for all file io
1 parent 038d575 commit 6b2c66d

File tree

7 files changed

+228
-175
lines changed

7 files changed

+228
-175
lines changed

toybox-cfg/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ edition.workspace = true
77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10-
dirs = "5.0.1"
1110
toml = "0.8.10"
1211
anyhow.workspace = true
1312
tracing.workspace = true
14-
log.workspace = true
13+
log.workspace = true
14+
15+
toybox-vfs.workspace = true

toybox-cfg/src/lib.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ use table::{Table, Value};
77
use std::path::{PathBuf};
88
use tracing::instrument;
99

10+
use toybox_vfs::{Vfs, PathKind};
11+
12+
// TODO(pat.m): this should maybe become a _system_ rather than a normal object
13+
1014

1115
/// Runtime representation of hierarchical key-value storage, intended for settings, command line config, etc.
1216
#[derive(Debug, Clone, Default)]
@@ -28,16 +32,17 @@ pub struct Config {
2832
}
2933

3034
impl Config {
31-
#[instrument(skip_all, name="cfg Config::for_app_name")]
32-
pub fn for_app_name(app_name: &str) -> anyhow::Result<Self> {
35+
// TODO(pat.m): this whole function is jank as shit
36+
#[instrument(skip_all, name="cfg Config::from_vfs")]
37+
pub fn from_vfs(vfs: &Vfs) -> anyhow::Result<Self> {
3338
let mut config = Self::default();
3439

35-
config.save_path = config_path(app_name);
36-
if config.save_path.exists() {
37-
config.base = Table::from_file(&config.save_path)?;
40+
if vfs.path_exists(PathKind::Config, "config.toml") {
41+
config.base = Table::from_file(vfs, PathKind::Config, "config.toml")?;
3842
} else {
43+
log::info!("Couldn't load config - writing defaults to {}", config.save_path.display());
3944
// TODO(pat.m): defaults?
40-
config.base.save_to_file(&config.save_path)?;
45+
config.base.save_to_file(vfs, PathKind::Config, "config.toml")?;
4146
}
4247

4348
config.arguments = Table::from_cli()?;
@@ -50,9 +55,9 @@ impl Config {
5055
}
5156

5257
#[instrument(skip_all, name="cfg Config::save")]
53-
pub fn save(&self) -> anyhow::Result<()> {
58+
pub fn save(&self, vfs: &Vfs) -> anyhow::Result<()> {
5459
// TODO(pat.m): extra resolve?
55-
self.base.save_to_file(&self.save_path)
60+
self.base.save_to_file(vfs, PathKind::Config, "config.toml")
5661
}
5762

5863
#[instrument(skip_all, name="cfg Config::commit")]
@@ -100,15 +105,3 @@ impl Config {
100105
// }
101106
}
102107

103-
104-
#[instrument]
105-
pub fn config_path(app_name: &str) -> PathBuf {
106-
let mut dir = dirs::preference_dir()
107-
.expect("Couldn't get preferences dir");
108-
109-
dir.push("toybox");
110-
dir.push(app_name);
111-
dir.push("config.toml");
112-
113-
dir
114-
}

toybox-cfg/src/table.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::collections::HashMap;
22
use std::path::Path;
33

4+
use toybox_vfs::{Vfs, PathKind};
45

56

67
#[derive(Debug, Clone, Default)]
@@ -14,8 +15,8 @@ impl Table {
1415
Table::default()
1516
}
1617

17-
pub fn from_file(path: impl AsRef<Path>) -> anyhow::Result<Table> {
18-
let data = std::fs::read_to_string(path)?;
18+
pub fn from_file(vfs: &Vfs, kind: PathKind, path: impl AsRef<Path>) -> anyhow::Result<Table> {
19+
let data = vfs.load_string(kind, path)?;
1920
let raw: toml::Table = toml::from_str(&data)?;
2021
let _ = dbg!(raw);
2122

@@ -31,12 +32,10 @@ impl Table {
3132
Ok(Table::default())
3233
}
3334

34-
pub fn save_to_file(&self, path: impl AsRef<Path>) -> anyhow::Result<()> {
35+
pub fn save_to_file(&self, vfs: &Vfs, kind: PathKind, path: impl AsRef<Path>) -> anyhow::Result<()> {
3536
let toml = self.to_toml();
3637
let string = toml::to_string_pretty(&toml)?;
37-
std::fs::create_dir_all(path.as_ref().parent().unwrap())?;
38-
std::fs::write(path, string)?;
39-
Ok(())
38+
vfs.save_data(kind, path, &string)
4039
}
4140

4241
/// Copy or replace values present in `other`

toybox-host/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub fn start<F, H>(settings: Settings<'_>, start_hostee: F) -> anyhow::Result<()
5252
let event_loop = EventLoop::new()?;
5353

5454
let window_attributes = Window::default_attributes()
55-
.with_title(settings.initial_title)
55+
.with_title(settings.app_name)
5656
.with_transparent(settings.transparent)
5757
.with_decorations(!settings.no_decorations)
5858
.with_resizable(true)
@@ -202,15 +202,15 @@ pub trait HostedApp {
202202

203203

204204
pub struct Settings<'title> {
205-
pub initial_title: &'title str,
205+
pub app_name: &'title str,
206206
pub transparent: bool,
207207
pub no_decorations: bool,
208208
}
209209

210210
impl<'title> Settings<'title> {
211-
pub fn new(initial_title: &'title str) -> Self {
211+
pub fn new(app_name: &'title str) -> Self {
212212
Settings {
213-
initial_title,
213+
app_name,
214214
transparent: false,
215215
no_decorations: false,
216216
}

toybox-vfs/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition.workspace = true
55
authors.workspace = true
66

77
[dependencies]
8+
dirs = "5.0.1"
89
log.workspace = true
910
tracing.workspace = true
1011
anyhow.workspace = true

0 commit comments

Comments
 (0)