feat: Add user-friendly default_channel configuration

Ajoute la possibilité de configurer le channel par défaut de Radio Paradise
de manière persistante et user-friendly.

Fonctionnalités :
- get_paradise_default_channel() : récupère le channel configuré (défaut: 0/main)
- set_paradise_default_channel(u8) : définit le channel par défaut
- Accepte DEUX formats dans le fichier YAML :
  * Noms conviviaux : "main", "mellow", "rock", "eclectic"
  * IDs numériques : 0, 1, 2, 3
- Stocke les valeurs comme chaînes conviviales pour la lisibilité
- Validation automatique avec fallback sur "main" si invalide
- Persistence automatique de la valeur par défaut lors du premier accès

Exemple de configuration YAML :
```yaml
sources:
  radio_paradise:
    enabled: true
    default_channel: mellow  # ou 1
```

Cette amélioration rend la configuration plus accessible aux utilisateurs
qui préfèrent un channel autre que Main Mix par défaut.
This commit is contained in:
Claude
2025-11-05 11:21:33 +00:00
parent bac4a94cad
commit 98bf45cd27

View File

@@ -21,6 +21,7 @@
//! }
//! ```
use crate::{channels::ParadiseChannelKind, client::DEFAULT_CHANNEL};
use anyhow::Result;
use pmoconfig::Config;
use serde_yaml::Value;
@@ -81,6 +82,60 @@ pub trait RadioParadiseConfigExt {
/// config.set_paradise_enabled(false)?;
/// ```
fn set_paradise_enabled(&self, enabled: bool) -> Result<()>;
/// Récupère le channel par défaut
///
/// # Returns
///
/// Le channel par défaut (0 = Main Mix par défaut).
///
/// Si la valeur n'existe pas dans la configuration, elle est automatiquement
/// définie à "main" et persistée.
///
/// # Channels disponibles
///
/// Peut être configuré comme chaîne de caractères ou nombre :
/// - "main" ou 0 = Main Mix (eclectic, diverse mix)
/// - "mellow" ou 1 = Mellow Mix (smooth, chilled music)
/// - "rock" ou 2 = Rock Mix (classic & modern rock)
/// - "eclectic" ou 3 = Eclectic Mix (global sounds)
///
/// # Exemple de configuration YAML
///
/// ```yaml
/// sources:
/// radio_paradise:
/// default_channel: mellow # or 1
/// ```
///
/// # Exemple d'utilisation
///
/// ```rust,ignore
/// let channel = config.get_paradise_default_channel()?;
/// let client = RadioParadiseClient::builder().channel(channel).build().await?;
/// ```
fn get_paradise_default_channel(&self) -> Result<u8>;
/// Définit le channel par défaut
///
/// # Arguments
///
/// * `channel` - Le channel (0-3)
///
/// La valeur est stockée sous forme de nom convivial ("main", "mellow", etc.)
/// dans le fichier de configuration.
///
/// # Exemple
///
/// ```rust,ignore
/// use pmoparadise::channels::ParadiseChannelKind;
///
/// // Use Mellow Mix by default
/// config.set_paradise_default_channel(ParadiseChannelKind::Mellow.id())?;
/// // Or simply:
/// config.set_paradise_default_channel(1)?;
/// ```
fn set_paradise_default_channel(&self, channel: u8) -> Result<()>;
}
impl RadioParadiseConfigExt for Config {
@@ -101,6 +156,62 @@ impl RadioParadiseConfigExt for Config {
Value::Bool(enabled),
)
}
fn get_paradise_default_channel(&self) -> Result<u8> {
match self.get_value(&["sources", "radio_paradise", "default_channel"]) {
Ok(Value::String(s)) => {
// Try to parse as channel name (e.g., "main", "mellow", etc.)
match s.parse::<ParadiseChannelKind>() {
Ok(kind) => Ok(kind.id()),
Err(_) => {
// Invalid channel name, use default
self.set_paradise_default_channel(DEFAULT_CHANNEL)?;
Ok(DEFAULT_CHANNEL)
}
}
}
Ok(Value::Number(n)) => {
// Accept numeric channel ID (0-3)
if let Some(ch) = n.as_u64() {
if ch <= 3 {
Ok(ch as u8)
} else {
// Invalid channel number, use default
self.set_paradise_default_channel(DEFAULT_CHANNEL)?;
Ok(DEFAULT_CHANNEL)
}
} else {
// Not a valid number, use default
self.set_paradise_default_channel(DEFAULT_CHANNEL)?;
Ok(DEFAULT_CHANNEL)
}
}
_ => {
// Use default and persist it as "main" (user-friendly)
self.set_value(
&["sources", "radio_paradise", "default_channel"],
Value::String("main".to_string()),
)?;
Ok(DEFAULT_CHANNEL)
}
}
}
fn set_paradise_default_channel(&self, channel: u8) -> Result<()> {
// Convert channel ID to user-friendly string name
let channel_name = match channel {
0 => "main",
1 => "mellow",
2 => "rock",
3 => "eclectic",
_ => return Err(anyhow::anyhow!("Invalid channel ID: {}", channel)),
};
self.set_value(
&["sources", "radio_paradise", "default_channel"],
Value::String(channel_name.to_string()),
)
}
}
#[cfg(test)]