refactor: tv no longer needs to write the default cable channel recipes to the user's configuration directory (#482)

The cable channel prototypes are now compiled into the binary itself,
and users can override them with a custom cable file.
This commit is contained in:
Alexandre Pasmantier 2025-04-28 00:04:38 +02:00 committed by GitHub
parent 67677fb917
commit c2f4cc258f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -26,8 +26,6 @@ const DEFAULT_CABLE_CHANNELS: &str =
const DEFAULT_CABLE_CHANNELS: &str = const DEFAULT_CABLE_CHANNELS: &str =
include_str!("../cable/windows-channels.toml"); include_str!("../cable/windows-channels.toml");
const DEFAULT_CABLE_CHANNELS_FILE_NAME: &str = "default_channels.toml";
/// Load the cable configuration from the config directory. /// Load the cable configuration from the config directory.
/// ///
/// Cable is loaded by compiling all files that match the following /// Cable is loaded by compiling all files that match the following
@ -47,24 +45,20 @@ pub fn load_cable_channels() -> Result<CableChannels> {
let files = std::fs::read_dir(&config_dir)?; let files = std::fs::read_dir(&config_dir)?;
// filter the files that match the pattern // filter the files that match the pattern
let mut file_paths: Vec<PathBuf> = files let file_paths: Vec<PathBuf> = files
.filter_map(|f| f.ok().map(|f| f.path())) .filter_map(|f| f.ok().map(|f| f.path()))
.filter(|p| is_cable_file_format(p) && p.is_file()) .filter(|p| is_cable_file_format(p) && p.is_file())
.collect(); .collect();
debug!("Found cable channel files: {:?}", file_paths); 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() { if file_paths.is_empty() {
debug!("No user defined cable channels found"); 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::<ChannelPrototypes>(DEFAULT_CABLE_CHANNELS)
.expect("Failed to parse default cable channels");
let prototypes = file_paths.iter().fold( let prototypes = file_paths.iter().fold(
Vec::<CableChannelPrototype>::new(), Vec::<CableChannelPrototype>::new(),
|mut acc, p| { |mut acc, p| {
@ -84,14 +78,19 @@ pub fn load_cable_channels() -> Result<CableChannels> {
}, },
); );
debug!("Loaded cable channels: {:?}", prototypes); debug!(
if prototypes.is_empty() { "Loaded {} default and {} custom prototypes",
error!("No cable channels found"); default_prototypes.prototypes.len(),
return Err(anyhow::anyhow!("No cable channels found")); prototypes.len()
} );
let mut cable_channels = FxHashMap::default(); 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); cable_channels.insert(prototype.name.clone(), prototype);
} }
Ok(CableChannels(cable_channels)) Ok(CableChannels(cable_channels))
@ -118,8 +117,5 @@ mod tests {
fn test_is_cable_file() { fn test_is_cable_file() {
let path = std::path::Path::new("cable_channels.toml"); let path = std::path::Path::new("cable_channels.toml");
assert!(is_cable_file_format(path)); assert!(is_cable_file_format(path));
let path = std::path::Path::new(DEFAULT_CABLE_CHANNELS_FILE_NAME);
assert!(is_cable_file_format(path));
} }
} }