From f6ad6b118e47335734fa887aaf9abc98d6c5c987 Mon Sep 17 00:00:00 2001 From: Alexandre Pasmantier Date: Mon, 28 Apr 2025 00:01:20 +0200 Subject: [PATCH] refactor!: tv no longer writes its default cable channels TOML file to configuration folder The cable channel prototypes are compiled into the binary itself, and users can override them with a custom cable file. --- television/cable.rs | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/television/cable.rs b/television/cable.rs index 71d35ff..c2c98a0 100644 --- a/television/cable.rs +++ b/television/cable.rs @@ -26,8 +26,6 @@ const DEFAULT_CABLE_CHANNELS: &str = const DEFAULT_CABLE_CHANNELS: &str = include_str!("../cable/windows-channels.toml"); -const DEFAULT_CABLE_CHANNELS_FILE_NAME: &str = "default_channels.toml"; - /// Load the cable configuration from the config directory. /// /// Cable is loaded by compiling all files that match the following @@ -47,24 +45,20 @@ pub fn load_cable_channels() -> Result { let files = std::fs::read_dir(&config_dir)?; // filter the files that match the pattern - let mut file_paths: Vec = files + let file_paths: Vec = files .filter_map(|f| f.ok().map(|f| f.path())) .filter(|p| is_cable_file_format(p) && p.is_file()) .collect(); debug!("Found cable channel files: {:?}", file_paths); - - // If no cable provider files are found, write the default provider for the current - // platform to the config directory if file_paths.is_empty() { debug!("No user defined cable channels found"); - // write the default cable channels to the config directory - let default_channels_path = - config_dir.join(DEFAULT_CABLE_CHANNELS_FILE_NAME); - std::fs::write(&default_channels_path, DEFAULT_CABLE_CHANNELS)?; - file_paths.push(default_channels_path); } + let default_prototypes = + toml::from_str::(DEFAULT_CABLE_CHANNELS) + .expect("Failed to parse default cable channels"); + let prototypes = file_paths.iter().fold( Vec::::new(), |mut acc, p| { @@ -84,14 +78,19 @@ pub fn load_cable_channels() -> Result { }, ); - debug!("Loaded cable channels: {:?}", prototypes); - if prototypes.is_empty() { - error!("No cable channels found"); - return Err(anyhow::anyhow!("No cable channels found")); - } + debug!( + "Loaded {} default and {} custom prototypes", + default_prototypes.prototypes.len(), + prototypes.len() + ); let mut cable_channels = FxHashMap::default(); - for prototype in prototypes { + // custom prototypes take precedence over default ones + for prototype in default_prototypes + .prototypes + .into_iter() + .chain(prototypes.into_iter()) + { cable_channels.insert(prototype.name.clone(), prototype); } Ok(CableChannels(cable_channels)) @@ -118,8 +117,5 @@ mod tests { fn test_is_cable_file() { let path = std::path::Path::new("cable_channels.toml"); assert!(is_cable_file_format(path)); - - let path = std::path::Path::new(DEFAULT_CABLE_CHANNELS_FILE_NAME); - assert!(is_cable_file_format(path)); } }