|
| 1 | +use serde::{Deserialize, Serialize}; |
| 2 | + |
| 3 | +/// Public surface for the config. |
| 4 | +#[derive(Debug, Clone, PartialEq)] |
| 5 | +pub struct Config { |
| 6 | + pub appearance: Appearance, |
| 7 | +} |
| 8 | + |
| 9 | +impl Config { |
| 10 | + pub fn new() -> Self { |
| 11 | + Self { |
| 12 | + appearance: Appearance { |
| 13 | + theme: iced::Theme::GruvboxLight, |
| 14 | + scale: 1.0, |
| 15 | + }, |
| 16 | + } |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +impl Default for Config { |
| 21 | + fn default() -> Self { |
| 22 | + Self::new() |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +/// Fills in any missing fields from `inner` using `Config::default()`. |
| 27 | +impl From<InnerConfig> for Config { |
| 28 | + fn from(inner: InnerConfig) -> Self { |
| 29 | + let defaults = Config::default(); |
| 30 | + Self { |
| 31 | + appearance: inner |
| 32 | + .appearance |
| 33 | + .map(|a| Appearance { |
| 34 | + theme: a |
| 35 | + .theme |
| 36 | + .map(iced::Theme::from) |
| 37 | + .unwrap_or(defaults.appearance.theme.clone()), |
| 38 | + scale: a.scale.unwrap_or(defaults.appearance.scale.clone()), |
| 39 | + }) |
| 40 | + .unwrap_or(defaults.appearance.clone()), |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/// For writing config back out to disk — only include what differs |
| 46 | +/// from defaults if you want a "sparse" file, or just mirror everything. |
| 47 | +impl From<&Config> for InnerConfig { |
| 48 | + fn from(config: &Config) -> Self { |
| 49 | + Self { |
| 50 | + appearance: Some(InnerAppearance { |
| 51 | + theme: crate::theme::ThemeSetting::try_from(&config.appearance.theme).ok(), |
| 52 | + scale: Some(config.appearance.scale), |
| 53 | + }), |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/// Reader to config file |
| 59 | +#[derive(Deserialize, Serialize, Default)] |
| 60 | +pub(crate) struct InnerConfig { |
| 61 | + appearance: Option<InnerAppearance>, |
| 62 | +} |
| 63 | + |
| 64 | +#[derive(Debug, Clone, PartialEq)] |
| 65 | +pub struct Appearance { |
| 66 | + /// Inner to `iced::Theme` until I feel like adding custom themes |
| 67 | + pub theme: iced::Theme, |
| 68 | + pub scale: f32, |
| 69 | +} |
| 70 | + |
| 71 | +#[derive(Deserialize, Serialize)] |
| 72 | +pub(crate) struct InnerAppearance { |
| 73 | + theme: Option<crate::theme::ThemeSetting>, |
| 74 | + scale: Option<f32>, |
| 75 | +} |
0 commit comments