common/types: add type for ChannelName

This commit is contained in:
2025-11-25 04:13:32 +09:00
parent c833143496
commit 1a2ea5327b
7 changed files with 69 additions and 9 deletions

View File

@@ -1,6 +1,7 @@
mod absolute_relative_song_position;
mod audio;
mod bool_or_oneshot;
mod channel_name;
mod group_type;
mod one_or_range;
mod replay_gain_mode_mode;
@@ -16,6 +17,7 @@ mod window_range;
pub use absolute_relative_song_position::AbsouluteRelativeSongPosition;
pub use audio::Audio;
pub use bool_or_oneshot::BoolOrOneshot;
pub use channel_name::ChannelName;
pub use group_type::GroupType;
pub use one_or_range::OneOrRange;
pub use replay_gain_mode_mode::ReplayGainModeMode;
@@ -39,7 +41,6 @@ pub type TimeWithFractions = f64;
// TODO: use a proper types
pub type AudioOutputId = String;
pub type ChannelName = String;
pub type Feature = String;
pub type PartitionName = String;
pub type Path = String;

View File

@@ -0,0 +1,39 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ChannelName(String);
impl ChannelName {
fn new(name: String) -> Self {
ChannelName(name)
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn into_inner(self) -> String {
self.0
}
}
impl std::fmt::Display for ChannelName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::str::FromStr for ChannelName {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if !s
.chars()
.all(|c| c.is_ascii_alphanumeric() || "-_.:".contains(c))
{
Err("Invalid channel name")
} else {
Ok(ChannelName::new(s.to_string()))
}
}
}