use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Default, PartialEq, Deserialize, Serialize)]
#[serde(default)]
pub struct Station {
#[serde(deserialize_with = "coordinate")]
pub geo_lat: Option<f64>,
#[serde(deserialize_with = "coordinate")]
pub geo_long: Option<f64>,
pub stationuuid: String,
pub name: String,
pub url: String,
pub url_resolved: String,
pub homepage: String,
pub favicon: String,
pub country: String,
pub countrycode: String,
pub state: String,
pub language: String,
pub languagecodes: String,
pub tags: String,
pub codec: String,
pub bitrate: u32,
pub hls: u8,
pub lastcheckok: u8,
pub votes: u64,
pub clickcount: u64,
pub custom: bool,
pub active: bool,
pub favorite: bool,
}
impl Station {
pub fn primary_url(&self) -> &str {
if self.url_resolved.is_empty() {
&self.url
} else {
&self.url_resolved
}
}
pub fn stream_candidates(&self, backups: &[String]) -> Vec<String> {
let mut result = Vec::new();
for url in std::iter::once(self.primary_url())
.chain(std::iter::once(self.url.as_str()))
.chain(backups.iter().map(String::as_str))
{
if crate::config::validate_url(url).is_ok() && !result.iter().any(|s| s == url) {
result.push(url.to_owned());
}
}
result
}
pub fn search_text(&self) -> String {
format!(
"{} {} {} {} {} {} {}",
self.name,
self.country,
self.countrycode,
self.state,
self.language,
self.languagecodes,
self.tags
)
.to_lowercase()
}
}
#[derive(clap::Args, Debug, Clone)]
pub struct Page {
#[arg(long, default_value_t=100, value_parser=clap::value_parser!(u16).range(1..=1000))]
pub limit: u16,
#[arg(long, default_value_t = 0)]
pub offset: usize,
#[arg(long)]
pub all: bool,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Filter {
pub geo: Option<crate::geo::GeoFilter>,
pub sort: Sort,
pub query: String,
pub country: Option<String>,
pub language: Option<String>,
pub tag: Option<String>,
pub min_bitrate: u32,
pub favorites: bool,
pub history: bool,
pub custom: bool,
pub limit: usize,
pub recordings: bool,
pub offset: usize,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, clap::ValueEnum)]
pub enum Sort {
Name,
Bitrate,
Votes,
#[default]
Clickcount,
Random,
}
#[derive(Debug, Clone, Copy)]
pub enum FacetKind {
Country,
Language,
Tag,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Facet {
pub value: String,
pub count: usize,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Recording {
pub id: String,
pub stationuuid: String,
pub station_name: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub track: Option<String>,
pub source_url_resolved: String,
pub started_at: String,
pub stopped_at: Option<String>,
pub media_duration_ms: Option<u64>,
pub codec: String,
pub container: String,
pub path: String,
#[serde(default)]
pub final_path: Option<String>,
pub size_bytes: u64,
pub status: String,
pub error: Option<String>,
#[serde(default)]
pub reconnects: u64,
#[serde(default)]
pub recovery_pending: bool,
}
fn coordinate<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Option<f64>, D::Error> {
struct CoordVisitor;
impl<'de> serde::de::Visitor<'de> for CoordVisitor {
type Value = Option<f64>;
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str("an optional coordinate")
}
fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E> {
Ok(v.is_finite().then_some(v))
}
fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E> {
Ok(Some(v as f64))
}
fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E> {
Ok(Some(v as f64))
}
fn visit_none<E>(self) -> Result<Self::Value, E> {
Ok(None)
}
fn visit_unit<E>(self) -> Result<Self::Value, E> {
Ok(None)
}
fn visit_bool<E>(self, _: bool) -> Result<Self::Value, E> {
Ok(None)
}
fn visit_str<E>(self, _: &str) -> Result<Self::Value, E> {
Ok(None)
}
fn visit_seq<A: serde::de::SeqAccess<'de>>(
self,
mut seq: A,
) -> Result<Self::Value, A::Error> {
while seq.next_element::<serde::de::IgnoredAny>()?.is_some() {}
Ok(None)
}
fn visit_map<A: serde::de::MapAccess<'de>>(
self,
mut map: A,
) -> Result<Self::Value, A::Error> {
while map
.next_entry::<serde::de::IgnoredAny, serde::de::IgnoredAny>()?
.is_some()
{}
Ok(None)
}
}
d.deserialize_any(CoordVisitor)
}