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(clap::Args, Debug, Clone)]
71pub struct Page {
72 #[arg(long, default_value_t=100, value_parser=clap::value_parser!(u16).range(1..=1000))]
73 pub limit: u16,
74 #[arg(long, default_value_t = 0)]
75 pub offset: usize,
76 #[arg(long)]
78 pub all: bool,
79}
80
81#[derive(Debug, Clone, Default, PartialEq, Eq)]
82pub struct Filter {
83 pub geo: Option<crate::geo::GeoFilter>,
84 pub sort: Sort,
85 pub query: String,
86 pub country: Option<String>,
87 pub language: Option<String>,
88 pub tag: Option<String>,
89 pub min_bitrate: u32,
90 pub favorites: bool,
91 pub history: bool,
92 pub custom: bool,
93 pub limit: usize,
94 pub recordings: bool,
95 pub offset: usize,
96}
97
98#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, clap::ValueEnum)]
99pub enum Sort {
100 Name,
101 Bitrate,
102 Votes,
103 #[default]
104 Clickcount,
105 Random,
106}
107
108#[derive(Debug, Clone, Copy)]
109pub enum FacetKind {
110 Country,
111 Language,
112 Tag,
113}
114
115#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
116pub struct Facet {
117 pub value: String,
118 pub count: usize,
119}
120
121#[derive(Debug, Clone, Deserialize, Serialize)]
122pub struct Recording {
123 pub id: String,
124 pub stationuuid: String,
125 pub station_name: String,
126 #[serde(default, skip_serializing_if = "Option::is_none")]
128 pub track: Option<String>,
129 pub source_url_resolved: String,
130 pub started_at: String,
131 pub stopped_at: Option<String>,
132 pub media_duration_ms: Option<u64>,
133 pub codec: String,
134 pub container: String,
135 pub path: String,
136 #[serde(default)]
137 pub final_path: Option<String>,
138 pub size_bytes: u64,
139 pub status: String,
140 pub error: Option<String>,
141 #[serde(default)]
143 pub reconnects: u64,
144 #[serde(default)]
145 pub recovery_pending: bool,
146}
147
148fn coordinate<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Option<f64>, D::Error> {
150 struct CoordVisitor;
151 impl<'de> serde::de::Visitor<'de> for CoordVisitor {
152 type Value = Option<f64>;
153 fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
154 f.write_str("an optional coordinate")
155 }
156 fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E> {
157 Ok(v.is_finite().then_some(v))
158 }
159 fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E> {
160 Ok(Some(v as f64))
161 }
162 fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E> {
163 Ok(Some(v as f64))
164 }
165 fn visit_none<E>(self) -> Result<Self::Value, E> {
166 Ok(None)
167 }
168 fn visit_unit<E>(self) -> Result<Self::Value, E> {
169 Ok(None)
170 }
171 fn visit_bool<E>(self, _: bool) -> Result<Self::Value, E> {
172 Ok(None)
173 }
174 fn visit_str<E>(self, _: &str) -> Result<Self::Value, E> {
175 Ok(None)
176 }
177 fn visit_seq<A: serde::de::SeqAccess<'de>>(
178 self,
179 mut seq: A,
180 ) -> Result<Self::Value, A::Error> {
181 while seq.next_element::<serde::de::IgnoredAny>()?.is_some() {}
182 Ok(None)
183 }
184 fn visit_map<A: serde::de::MapAccess<'de>>(
185 self,
186 mut map: A,
187 ) -> Result<Self::Value, A::Error> {
188 while map
189 .next_entry::<serde::de::IgnoredAny, serde::de::IgnoredAny>()?
190 .is_some()
191 {}
192 Ok(None)
193 }
194 }
195 d.deserialize_any(CoordVisitor)
196}