/* * Copyright © 2023 Collabora Ltd. * Copyright © 2024 Valve Software * * SPDX-License-Identifier: MIT */ use anyhow::Result; use serde::Deserialize; use std::path::PathBuf; use tokio::fs::read_to_string; #[cfg(not(test))] use tokio::sync::OnceCell; #[cfg(not(test))] use crate::hardware::is_deck; #[cfg(not(test))] static CONFIG: OnceCell> = OnceCell::const_new(); #[derive(Clone, Default, Deserialize, Debug)] #[serde(default)] pub(crate) struct PlatformConfig { pub factory_reset: Option, pub update_bios: Option, pub update_dock: Option, pub storage: Option, } #[derive(Clone, Default, Deserialize, Debug)] pub(crate) struct ScriptConfig { pub script: PathBuf, #[serde(default)] pub script_args: Vec, } #[derive(Clone, Default, Deserialize, Debug)] pub(crate) struct StorageConfig { pub trim_devices: ScriptConfig, pub format_device: FormatDeviceConfig, } #[derive(Clone, Default, Deserialize, Debug)] pub(crate) struct FormatDeviceConfig { pub script: PathBuf, #[serde(default)] pub script_args: Vec, pub label_flag: String, #[serde(default)] pub device_flag: Option, #[serde(default)] pub validate_flag: Option, #[serde(default)] pub no_validate_flag: Option, } impl PlatformConfig { #[cfg(not(test))] async fn load() -> Result> { if !is_deck().await? { // Non-Steam Deck platforms are not yet supported return Ok(None); } let config = read_to_string("/usr/share/steamos-manager/platforms/jupiter.toml").await?; Ok(Some(toml::from_str(config.as_ref())?)) } } #[cfg(not(test))] pub(crate) async fn platform_config() -> Result<&'static Option> { CONFIG.get_or_try_init(PlatformConfig::load).await } #[cfg(test)] pub(crate) async fn platform_config() -> Result> { let test = crate::testing::current(); let config = test.platform_config.borrow().clone(); Ok(config) } #[cfg(test)] mod test { use super::*; #[tokio::test] async fn jupiter_valid() { let config = read_to_string("data/platforms/jupiter.toml") .await .expect("read_to_string"); let res = toml::from_str::(config.as_ref()); assert!(res.is_ok(), "{res:?}"); } }