Skip to main content

radio_cli/
model.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Default, PartialEq, Deserialize, Serialize)]
4#[serde(default)]
5pub struct Station {
6    #[serde(deserialize_with = "coordinate")]
7    pub geo_lat: Option<f64>,
8    #[serde(deserialize_with = "coordinate")]
9    pub geo_long: Option<f64>,
10    pub stationuuid: String,
11    pub name: String,
12    pub url: String,
13    pub url_resolved: String,
14    pub homepage: String,
15    pub favicon: String,
16    pub country: String,
17    pub countrycode: String,
18    pub state: String,
19    pub language: String,
20    pub languagecodes: String,
21    pub tags: String,
22    pub codec: String,
23    pub bitrate: u32,
24    pub hls: u8,
25    pub lastcheckok: u8,
26    pub votes: u64,
27    pub clickcount: u64,
28    pub custom: bool,
29    pub active: bool,
30    pub favorite: bool,
31}
32
33impl Station {
34    pub fn primary_url(&self) -> &str {
35        if self.url_resolved.is_empty() {
36            &self.url
37        } else {
38            &self.url_resolved
39        }
40    }
41
42    pub fn stream_candidates(&self, backups: &[String]) -> Vec<String> {
43        let mut result = Vec::new();
44        for url in std::iter::once(self.primary_url())
45            .chain(std::iter::once(self.url.as_str()))
46            .chain(backups.iter().map(String::as_str))
47        {
48            if crate::config::validate_url(url).is_ok() && !result.iter().any(|s| s == url) {
49                result.push(url.to_owned());
50            }
51        }
52        result
53    }
54
55    pub fn search_text(&self) -> String {
56        format!(
57            "{} {} {} {} {} {} {}",
58            self.name,
59            self.country,
60            self.countrycode,
61            self.state,
62            self.language,
63            self.languagecodes,
64            self.tags
65        )
66        .to_lowercase()
67    }
68}
69
70#[derive(Debug, Clone, Default, PartialEq, Eq)]
71pub struct Filter {
72    pub geo: Option<crate::geo::GeoFilter>,
73    pub sort: Sort,
74    pub query: String,
75    pub country: Option<String>,
76    pub language: Option<String>,
77    pub tag: Option<String>,
78    pub min_bitrate: u32,
79    pub favorites: bool,
80    pub history: bool,
81    pub custom: bool,
82    pub limit: usize,
83    pub recordings: bool,
84    pub offset: usize,
85}
86
87#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, clap::ValueEnum)]
88pub enum Sort {
89    Name,
90    Bitrate,
91    Votes,
92    #[default]
93    Clickcount,
94    Random,
95}
96
97#[derive(Debug, Clone, Copy)]
98pub enum FacetKind {
99    Country,
100    Language,
101    Tag,
102}
103
104#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
105pub struct Facet {
106    pub value: String,
107    pub count: usize,
108}
109
110#[derive(Debug, Clone, Deserialize, Serialize)]
111pub struct Recording {
112    pub id: String,
113    pub stationuuid: String,
114    pub station_name: String,
115    /// Track title captured when recording was requested; absent for old records.
116    #[serde(default, skip_serializing_if = "Option::is_none")]
117    pub track: Option<String>,
118    pub source_url_resolved: String,
119    pub started_at: String,
120    pub stopped_at: Option<String>,
121    pub media_duration_ms: Option<u64>,
122    pub codec: String,
123    pub container: String,
124    pub path: String,
125    #[serde(default)]
126    pub final_path: Option<String>,
127    pub size_bytes: u64,
128    pub status: String,
129    pub error: Option<String>,
130    /// FFmpeg reconnect attempts; live audio may have gaps even on completion.
131    #[serde(default)]
132    pub reconnects: u64,
133    #[serde(default)]
134    pub recovery_pending: bool,
135}
136
137// Bad geographic metadata must not discard an otherwise usable station.
138fn coordinate<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Option<f64>, D::Error> {
139    let value = serde_json::Value::deserialize(d)?;
140    Ok(value.as_f64().filter(|n| n.is_finite()))
141}