243 lines
6.3 KiB
Rust
243 lines
6.3 KiB
Rust
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)]
|
|
pub export: ExportConfig,
|
|
#[serde(default)]
|
|
pub capture: CaptureConfig,
|
|
#[serde(default)]
|
|
pub hotkeys: HotkeyConfig,
|
|
#[serde(default)]
|
|
pub updates: UpdatesConfig,
|
|
#[serde(default)]
|
|
pub output_dir: Option<PathBuf>,
|
|
#[serde(default)]
|
|
pub theme: Theme,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct ExportConfig {
|
|
pub default_format: Format,
|
|
pub gif_dither: GifDither,
|
|
pub webm_codec: WebmCodec,
|
|
pub mp4_codec: Mp4Codec,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct CaptureConfig {
|
|
pub default_fps: u32,
|
|
pub capture_cursor: bool,
|
|
pub capture_audio: bool,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
|
pub struct HotkeyConfig {
|
|
pub start: Option<String>,
|
|
pub stop: Option<String>,
|
|
pub pause: Option<String>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct UpdatesConfig {
|
|
pub check_on_launch: bool,
|
|
}
|
|
|
|
#[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, Default)]
|
|
#[serde(rename_all = "lowercase")]
|
|
pub enum GifBudgetMode {
|
|
#[default]
|
|
Balanced,
|
|
Resolution,
|
|
Framerate,
|
|
}
|
|
|
|
#[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, Default)]
|
|
#[serde(rename_all = "lowercase")]
|
|
pub enum WebmCodec {
|
|
#[default]
|
|
Vp9,
|
|
Av1,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
|
|
#[serde(rename_all = "lowercase")]
|
|
pub enum Mp4Codec {
|
|
#[default]
|
|
H264,
|
|
H265,
|
|
}
|
|
|
|
impl Default for Config {
|
|
fn default() -> Self {
|
|
Self {
|
|
export: ExportConfig::default(),
|
|
capture: CaptureConfig::default(),
|
|
hotkeys: HotkeyConfig::default(),
|
|
updates: UpdatesConfig::default(),
|
|
output_dir: None,
|
|
theme: Theme::Dark,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Default for ExportConfig {
|
|
fn default() -> Self {
|
|
Self {
|
|
default_format: Format::Gif,
|
|
gif_dither: GifDither::FloydSteinberg,
|
|
webm_codec: WebmCodec::Vp9,
|
|
mp4_codec: Mp4Codec::H264,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Default for CaptureConfig {
|
|
fn default() -> Self {
|
|
Self {
|
|
default_fps: 30,
|
|
capture_cursor: true,
|
|
capture_audio: false,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Default for UpdatesConfig {
|
|
fn default() -> Self {
|
|
Self { check_on_launch: true }
|
|
}
|
|
}
|
|
|
|
impl Config {
|
|
pub fn load_or_create(path: &Path) -> Result<Self> {
|
|
if !path.exists() {
|
|
let cfg = Self::default();
|
|
cfg.save(path)?;
|
|
return Ok(cfg);
|
|
}
|
|
let raw = std::fs::read_to_string(path)?;
|
|
match toml::from_str::<Self>(&raw) {
|
|
Ok(cfg) => Ok(cfg),
|
|
Err(e) => {
|
|
// Config is corrupt - back it up and return defaults
|
|
tracing::warn!("config.toml is corrupt ({e}), backing up and using defaults");
|
|
let backup = path.with_extension("toml.bak");
|
|
let _ = std::fs::copy(path, &backup);
|
|
let cfg = Self::default();
|
|
cfg.save(path)?;
|
|
Ok(cfg)
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn save(&self, path: &Path) -> Result<()> {
|
|
let serialized = toml::to_string_pretty(self)?;
|
|
if let Some(parent) = path.parent() {
|
|
std::fs::create_dir_all(parent)?;
|
|
}
|
|
std::fs::write(path, serialized)?;
|
|
Ok(())
|
|
}
|
|
|
|
pub fn needs_first_run(&self) -> bool {
|
|
self.hotkeys.start.is_none()
|
|
|| self.hotkeys.stop.is_none()
|
|
|| self.hotkeys.pause.is_none()
|
|
}
|
|
}
|
|
|
|
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());
|
|
}
|
|
}
|