From a5ffda7d3f854557df1a009a08a71496df43791c Mon Sep 17 00:00:00 2001 From: dheijl Date: Wed, 28 Feb 2024 14:35:07 +0100 Subject: [PATCH] prepare for configurable streamsize See #120 --- src/enums/streaming.rs | 22 +++++++++++++++++++ src/utils/configuration.rs | 43 +++++++++++++++++++++++++++++++------- 2 files changed, 57 insertions(+), 8 deletions(-) diff --git a/src/enums/streaming.rs b/src/enums/streaming.rs index bd6e94d..3e128c1 100644 --- a/src/enums/streaming.rs +++ b/src/enums/streaming.rs @@ -26,3 +26,25 @@ impl fmt::Display for StreamingFormat { } } } + +#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)] +#[serde(untagged)] +pub enum StreamSize { + NoneChunked, + U32maxChunked, + U32maxNotChunked, + U64maxChunked, + U64maxNotChunked, +} + +impl fmt::Display for StreamSize { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + StreamSize::NoneChunked => write!(f, "NoneChunked"), + StreamSize::U32maxChunked => write!(f, "U32maxChunked"), + StreamSize::U32maxNotChunked => write!(f, "U32maxNotChunked"), + StreamSize::U64maxChunked => write!(f, "U64maxChunked"), + StreamSize::U64maxNotChunked => write!(f, "U64maxNotChunked"), + } + } +} diff --git a/src/utils/configuration.rs b/src/utils/configuration.rs index 9db9b3a..f30ab51 100644 --- a/src/utils/configuration.rs +++ b/src/utils/configuration.rs @@ -1,4 +1,6 @@ -use crate::{enums::streaming::StreamingFormat, globals::statics::SERVER_PORT}; +use crate::{ + enums::streaming::StreamSize, enums::streaming::StreamingFormat, globals::statics::SERVER_PORT, +}; use lexopt::{prelude::*, Parser}; use log::LevelFilter; use serde::{Deserialize, Serialize}; @@ -13,6 +15,24 @@ use toml::from_str; const CONFIGFILE: &str = "config{}.toml"; const PKGNAME: &str = env!("CARGO_PKG_NAME"); +// default values for Serde +struct CfgDefaults {} + +impl CfgDefaults { + fn disable_chunked() -> bool { + true + } + pub fn log_level() -> LevelFilter { + LevelFilter::Info + } + pub fn ssdp_interval_mins() -> f64 { + 10.0 + } + pub fn stream_size() -> StreamSize { + StreamSize::U64maxNotChunked + } +} + // the configuration struct, read from and saved in config.ini #[derive(Deserialize, Serialize, Clone, Debug)] struct Config { @@ -20,10 +40,6 @@ struct Config { pub configuration: Configuration, } -fn disable_chunked() -> bool { - true -} - #[derive(Deserialize, Serialize, Clone, Debug)] pub struct Configuration { #[serde(alias = "ServerPort")] @@ -34,15 +50,25 @@ pub struct Configuration { pub sound_source: String, #[serde(alias = "SoundCardIndex")] pub sound_source_index: Option, - #[serde(alias = "LogLevel")] + #[serde(alias = "LogLevel", default = "CfgDefaults::log_level")] pub log_level: LevelFilter, - #[serde(alias = "SSDPIntervalMins")] + #[serde( + alias = "SSDPIntervalMins", + default = "CfgDefaults::ssdp_interval_mins" + )] pub ssdp_interval_mins: f64, #[serde(alias = "AutoReconnect")] pub auto_reconnect: bool, // removed in 1.8.5 - #[serde(alias = "DisableChunked", skip, default = "disable_chunked")] + #[serde( + alias = "DisableChunked", + skip, + default = "CfgDefaults::disable_chunked" + )] _disable_chunked: bool, + // added in 1.9.9 + #[serde(alias = "StreamSize", default = "CfgDefaults::stream_size")] + pub stream_size: StreamSize, #[serde(alias = "UseWaveFormat")] pub use_wave_format: bool, #[serde(alias = "BitsPerSample")] @@ -83,6 +109,7 @@ impl Configuration { ssdp_interval_mins: 10.0, auto_reconnect: false, _disable_chunked: true, + stream_size: StreamSize::U64maxNotChunked, use_wave_format: false, bits_per_sample: Some(16), streaming_format: Some(StreamingFormat::Lpcm),