effindom_native_packaging/
fui_config.rs1use crate::{Error, Result};
2use serde::Deserialize;
3use std::fs;
4use std::path::{Path, PathBuf};
5
6pub const FUI_CONFIG_SCHEMA_VERSION: u32 = 1;
7pub const FUI_CONFIG_FILE: &str = "fui-config.json";
8pub const FUI_CONFIG_SCHEMA: &str = include_str!("../fui-config.schema.json");
9
10#[derive(Clone, Debug, Deserialize, PartialEq)]
11#[serde(deny_unknown_fields)]
12pub struct FuiConfig {
13 #[serde(rename = "$schema")]
14 pub schema: Option<String>,
15 pub version: u32,
16 pub application: Option<FuiApplicationConfig>,
17 pub web: Option<FuiWebConfig>,
18}
19
20#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
21#[serde(deny_unknown_fields, rename_all = "camelCase")]
22pub struct FuiApplicationConfig {
23 pub page_zoom: Option<FuiPageZoomMode>,
24}
25
26#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Eq)]
27#[serde(rename_all = "kebab-case")]
28pub enum FuiPageZoomMode {
29 Disabled,
30 Enabled,
31}
32
33#[derive(Clone, Debug, Deserialize, PartialEq)]
34#[serde(deny_unknown_fields, rename_all = "camelCase")]
35pub struct FuiWebConfig {
36 pub loading: Option<FuiWebLoadingConfig>,
37 pub dev_tools: Option<FuiWebDevToolsConfig>,
38}
39
40#[derive(Clone, Debug, Deserialize, PartialEq)]
41#[serde(deny_unknown_fields, rename_all = "camelCase")]
42pub struct FuiWebLoadingConfig {
43 pub delay_ms: Option<f64>,
44 pub minimum_visible_ms: Option<f64>,
45}
46
47#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
48#[serde(deny_unknown_fields, rename_all = "camelCase")]
49pub struct FuiWebDevToolsConfig {
50 pub dom_mirror: Option<FuiDevToolsDomMirrorMode>,
51}
52
53#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Eq)]
54#[serde(rename_all = "kebab-case")]
55pub enum FuiDevToolsDomMirrorMode {
56 Disabled,
57 Enabled,
58 OnRequested,
59}
60
61pub fn decode_fui_config(bytes: &[u8]) -> Result<FuiConfig> {
62 let config: FuiConfig = serde_json::from_slice(bytes).map_err(Error::ParseFuiConfig)?;
63 validate_fui_config(config)
64}
65
66pub fn load_fui_config(path: impl AsRef<Path>) -> Result<FuiConfig> {
67 let path = path.as_ref();
68 let bytes = fs::read(path).map_err(|source| Error::ReadFile {
69 path: PathBuf::from(path),
70 source,
71 })?;
72 decode_fui_config(&bytes)
73}
74
75fn validate_fui_config(config: FuiConfig) -> Result<FuiConfig> {
76 if config.version != FUI_CONFIG_SCHEMA_VERSION {
77 return Err(Error::InvalidFuiConfig(format!(
78 "version must be {FUI_CONFIG_SCHEMA_VERSION}, got {}",
79 config.version
80 )));
81 }
82 if let Some(loading) = config.web.as_ref().and_then(|web| web.loading.as_ref()) {
83 validate_duration(loading.delay_ms, "web.loading.delayMs")?;
84 validate_duration(loading.minimum_visible_ms, "web.loading.minimumVisibleMs")?;
85 }
86 Ok(config)
87}
88
89fn validate_duration(value: Option<f64>, field: &'static str) -> Result<()> {
90 if value.is_some_and(|value| !value.is_finite() || value < 0.0) {
91 return Err(Error::InvalidFuiConfig(format!(
92 "{field} must be a finite non-negative number"
93 )));
94 }
95 Ok(())
96}