current state before improvement pass
This commit is contained in:
+81
-4
@@ -3,6 +3,14 @@ use std::path::{Path, PathBuf};
|
||||
use anyhow::Result;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Theme {
|
||||
#[default]
|
||||
Dark,
|
||||
Light,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Config {
|
||||
#[serde(default)]
|
||||
@@ -15,6 +23,8 @@ pub struct Config {
|
||||
pub updates: UpdatesConfig,
|
||||
#[serde(default)]
|
||||
pub output_dir: Option<PathBuf>,
|
||||
#[serde(default)]
|
||||
pub theme: Theme,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
@@ -44,32 +54,36 @@ pub struct UpdatesConfig {
|
||||
pub check_on_launch: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Format {
|
||||
#[default]
|
||||
Gif,
|
||||
Webm,
|
||||
Mp4,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum GifDither {
|
||||
#[default]
|
||||
FloydSteinberg,
|
||||
Bayer,
|
||||
None,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum WebmCodec {
|
||||
#[default]
|
||||
Vp9,
|
||||
Av1,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Mp4Codec {
|
||||
#[default]
|
||||
H264,
|
||||
H265,
|
||||
}
|
||||
@@ -82,6 +96,7 @@ impl Default for Config {
|
||||
hotkeys: HotkeyConfig::default(),
|
||||
updates: UpdatesConfig::default(),
|
||||
output_dir: None,
|
||||
theme: Theme::Dark,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -143,3 +158,65 @@ impl Config {
|
||||
pub fn config_path(app_dir: &Path) -> PathBuf {
|
||||
app_dir.join("config.toml")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn config_roundtrip() {
|
||||
let cfg = Config {
|
||||
export: ExportConfig {
|
||||
default_format: Format::Webm,
|
||||
gif_dither: GifDither::Bayer,
|
||||
webm_codec: WebmCodec::Av1,
|
||||
mp4_codec: Mp4Codec::H265,
|
||||
},
|
||||
capture: CaptureConfig {
|
||||
default_fps: 60,
|
||||
capture_cursor: false,
|
||||
capture_audio: true,
|
||||
},
|
||||
hotkeys: HotkeyConfig {
|
||||
start: Some("F9".to_string()),
|
||||
stop: Some("F10".to_string()),
|
||||
pause: Some("F11".to_string()),
|
||||
},
|
||||
updates: UpdatesConfig { check_on_launch: false },
|
||||
output_dir: Some(PathBuf::from("./custom")),
|
||||
theme: Theme::Light,
|
||||
};
|
||||
|
||||
let dir = std::env::temp_dir().join("bytesize-test-config");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("config.toml");
|
||||
cfg.save(&path).unwrap();
|
||||
let loaded = Config::load_or_create(&path).unwrap();
|
||||
assert_eq!(loaded.export.default_format, Format::Webm);
|
||||
assert_eq!(loaded.capture.default_fps, 60);
|
||||
assert_eq!(loaded.hotkeys.start, Some("F9".to_string()));
|
||||
assert_eq!(loaded.output_dir, Some(PathBuf::from("./custom")));
|
||||
assert_eq!(loaded.theme, Theme::Light);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn needs_first_run_when_hotkeys_missing() {
|
||||
let cfg = Config::default();
|
||||
assert!(cfg.needs_first_run());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_first_run_when_hotkeys_set() {
|
||||
let cfg = Config {
|
||||
hotkeys: HotkeyConfig {
|
||||
start: Some("F9".to_string()),
|
||||
stop: Some("F10".to_string()),
|
||||
pause: Some("F11".to_string()),
|
||||
},
|
||||
..Config::default()
|
||||
};
|
||||
assert!(!cfg.needs_first_run());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user