selene-core 0.3.0

selene-core is the backend for Selene, a local-first music player
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
use std::{
    collections::{HashMap, HashSet},
    str::FromStr,
    sync::LazyLock,
};

use chrono::{DateTime, TimeZone, Utc};
use lunar_lib::warn;
use regex::Regex;

use crate::{
    database::DatabaseEntry,
    errors::MetadataError,
    library::{
        album::{Album, UNKNOWN_ALBUM},
        artist::{Artist, ArtistGroup, artists_from_string},
        track::lyric_data::LyricData,
    },
};

pub const TRACK_NUM_KEY: &str = "track";
static TRACK_NUM_REGEX: LazyLock<Regex> =
    LazyLock::new(|| Regex::new("(?i)^(tra?ck)(.*(num(ber)?))?$").unwrap());

pub const TRACK_TOTAL_KEY: &str = "track_total";
static TRACK_TOTAL_REGEX: LazyLock<Regex> =
    LazyLock::new(|| Regex::new("(?i)^(tra?ck|tot(al)?)(.*(tot(al)?|tra?cks?))$").unwrap());

pub const DISC_NUM_KEY: &str = "disc";
static DISC_NUM_REGEX: LazyLock<Regex> =
    LazyLock::new(|| Regex::new("(?i)^disc(.*(num(ber)?))?$").unwrap());

pub const DISC_TOTAL_KEY: &str = "disc_total";
static DISC_TOTAL_REGEX: LazyLock<Regex> =
    LazyLock::new(|| Regex::new("(?i)^(disc|tot(al)?)(.*(tot(al)?|disc(s)?))$").unwrap());

pub const LYRIC_KEY: &str = "lyrics";
static LYRIC_REGEX: LazyLock<Regex> =
    LazyLock::new(|| Regex::new("(?i)^((un)?synced)?(.?lyric(s)?)|sylt|uslt$").unwrap());

pub const INSTRUMENTAL_KEY: &str = "instrumental";
static INSTRUMENTAL_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new("(?i)inst").unwrap());

pub const ALBUM_KEY: &str = "album";
pub const GENRE_KEY: &str = "genre";
pub const TITLE_KEY: &str = "title";

pub const DATE_KEY: &str = "date";
static DATE_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new("(?i)^year|date$").unwrap());

pub const ARTIST_KEY: &str = "artist";
static ARTIST_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new("(?i)^artist(s)?$").unwrap());

pub const ALBUM_ARTIST_KEY: &str = "album_artist";
static ALBUM_ARTIST_REGEX: LazyLock<Regex> =
    LazyLock::new(|| Regex::new("(?i)^album.*artist(s)?$").unwrap());

static KEY_REGEX: LazyLock<Box<[(&'static Regex, &'static str)]>> = LazyLock::new(|| {
    Box::from([
        (&*TRACK_NUM_REGEX, TRACK_NUM_KEY),
        (&*DISC_NUM_REGEX, DISC_NUM_KEY),
        (&*TRACK_TOTAL_REGEX, TRACK_TOTAL_KEY),
        (&*DISC_TOTAL_REGEX, DISC_TOTAL_KEY),
        (&*LYRIC_REGEX, LYRIC_KEY),
        (&*INSTRUMENTAL_REGEX, INSTRUMENTAL_KEY),
        (&*DATE_REGEX, DATE_KEY),
        (&*ARTIST_REGEX, ARTIST_KEY),
        (&*ALBUM_ARTIST_REGEX, ALBUM_ARTIST_KEY),
    ])
});

pub fn canonicalize_metadata_key(key: impl AsRef<str>) -> String {
    let key = key.as_ref();

    match key.to_ascii_lowercase().as_str() {
        "title" => return TITLE_KEY.to_owned(),
        "genre" => return GENRE_KEY.to_owned(),
        "album" => return ALBUM_KEY.to_owned(),
        _ => {}
    }

    for &(regex, cannon_key) in KEY_REGEX.iter() {
        if regex.is_match(key) {
            return cannon_key.to_owned();
        }
    }

    key.to_owned()
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MetadataKey {
    Album(Option<Album>),
    AlbumArtists(Vec<Artist>),
    Artist(Vec<Artist>),
    Date(Option<DateTime<Utc>>),
    DiscNum(Option<u16>),
    DiscTotal(Option<u16>),
    Genre(Option<String>),
    Lyrics(Option<LyricData>),
    Instrumental(bool),
    Title(Option<String>),
    TrackNum(Option<u16>),
    TrackTotal(Option<u16>),
    Other(String, Option<String>),
}

impl std::hash::Hash for MetadataKey {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.to_key().hash(state);
    }
}

impl MetadataKey {
    #[must_use]
    pub fn to_key(&self) -> String {
        match self {
            MetadataKey::Album(_) => ALBUM_KEY.to_owned(),
            MetadataKey::AlbumArtists(_) => ALBUM_ARTIST_KEY.to_owned(),
            MetadataKey::Artist(_) => ARTIST_KEY.to_owned(),
            MetadataKey::Date(_) => DATE_KEY.to_owned(),
            MetadataKey::DiscNum(_) => DISC_NUM_KEY.to_owned(),
            MetadataKey::DiscTotal(_) => DISC_TOTAL_KEY.to_owned(),
            MetadataKey::Genre(_) => GENRE_KEY.to_owned(),
            MetadataKey::Lyrics(_) => LYRIC_KEY.to_owned(),
            MetadataKey::Instrumental(_) => INSTRUMENTAL_KEY.to_owned(),
            MetadataKey::Title(_) => TITLE_KEY.to_owned(),
            MetadataKey::TrackNum(_) => TRACK_NUM_KEY.to_owned(),
            MetadataKey::TrackTotal(_) => TRACK_TOTAL_KEY.to_owned(),
            MetadataKey::Other(key, _) => key.to_owned(),
        }
    }

    pub fn key_from_str(str: impl AsRef<str>) -> Self {
        let key = canonicalize_metadata_key(str);

        match key.as_str() {
            ALBUM_KEY => Self::Album(None),
            ALBUM_ARTIST_KEY => Self::AlbumArtists(Vec::new()),
            ARTIST_KEY => Self::Artist(Vec::new()),
            DATE_KEY => Self::Date(None),
            DISC_NUM_KEY => Self::DiscNum(None),
            DISC_TOTAL_KEY => Self::DiscTotal(None),
            GENRE_KEY => Self::Genre(None),
            LYRIC_KEY => Self::Lyrics(None),
            INSTRUMENTAL_KEY => Self::Instrumental(false),
            TITLE_KEY => Self::Title(None),
            TRACK_NUM_KEY => Self::TrackNum(None),
            TRACK_TOTAL_KEY => Self::TrackTotal(None),
            _ => Self::Other(key, None),
        }
    }

    pub fn key_value_from_str(str: impl AsRef<str>) -> Result<Self, MetadataError> {
        let (key_str, value) = str
            .as_ref()
            .split_once('=')
            .ok_or(MetadataError::InvalidKey(str.as_ref().to_owned()))?;

        let mut key = Self::key_from_str(key_str);
        let value = (!value.is_empty()).then_some(value);

        match &mut key {
            MetadataKey::Title(v) | MetadataKey::Genre(v) | MetadataKey::Other(_, v) => {
                *v = value.map(str::to_owned);
            }
            MetadataKey::DiscNum(v)
            | MetadataKey::DiscTotal(v)
            | MetadataKey::TrackNum(v)
            | MetadataKey::TrackTotal(v) => *v = value.map(u16::from_str).transpose()?,
            MetadataKey::Album(v) => {
                *v = if let Some(value) = value {
                    Some(
                        Album::db_find_by_name(value)?
                            .into_iter()
                            .next()
                            .ok_or(MetadataError::MissingAlbum(value.to_owned()))?,
                    )
                } else {
                    None
                };
            }
            MetadataKey::Artist(v) | MetadataKey::AlbumArtists(v) => {
                *v = if let Some(value) = value {
                    let artists = artists_from_string(value);

                    for a in &artists {
                        if !Artist::db_check(a.id())? {
                            return Err(MetadataError::MissingArtist(a.name().to_owned()));
                        }
                    }

                    artists
                } else {
                    Vec::new()
                }
            }
            MetadataKey::Date(v) => {
                *v = value
                    .map(|s| {
                        extract_date_str(s).ok_or(MetadataError::InvalidValue(
                            key_str.to_owned(),
                            s.to_owned(),
                        ))
                    })
                    .transpose()?;
            }
            MetadataKey::Lyrics(v) => *v = value.map(LyricData::infer_from_string),
            MetadataKey::Instrumental(v) => *v = value.is_some_and(|v| v == "1" || v == "true"),
        }

        Ok(key)
    }
}

impl FromStr for MetadataKey {
    type Err = MetadataError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        MetadataKey::key_value_from_str(s)
    }
}

/// [`RawMetadata`] represents all known keys and their raw (valid) values
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct RawMetadata {
    pub album: Option<String>,
    pub album_artists: Option<String>,
    pub artists: Option<String>,
    pub date: Option<String>,
    pub disc_num: Option<String>,
    pub disc_total: Option<String>,
    pub genre: Option<String>,
    pub lyrics: Option<String>,
    pub instrumental: Option<bool>,
    pub title: Option<String>,
    pub track_num: Option<String>,
    pub track_total: Option<String>,

    pub other: HashMap<String, String>,
}

impl FromIterator<(String, String)> for RawMetadata {
    fn from_iter<T: IntoIterator<Item = (String, String)>>(iter: T) -> Self {
        let mut raw = RawMetadata::default();

        for (k, v) in iter {
            let key = MetadataKey::key_from_str(k);
            let v = Some(v);

            match key {
                MetadataKey::Album(_) => raw.album = v,
                MetadataKey::AlbumArtists(_) => raw.album_artists = v,
                MetadataKey::Artist(_) => raw.artists = v,
                MetadataKey::Date(_) => raw.date = v,
                MetadataKey::DiscNum(_) => raw.disc_num = v,
                MetadataKey::DiscTotal(_) => raw.disc_total = v,
                MetadataKey::Genre(_) => raw.genre = v,
                MetadataKey::Lyrics(_) => raw.lyrics = v,
                MetadataKey::Instrumental(_) => {
                    raw.instrumental = Some(v.is_some_and(|v| v == "1" || v == "true"));
                }
                MetadataKey::Title(_) => raw.title = v,
                MetadataKey::TrackNum(_) => raw.track_num = v,
                MetadataKey::TrackTotal(_) => raw.track_total = v,
                MetadataKey::Other(k, _) => {
                    if let Some(v) = v {
                        raw.other.insert(k, v);
                    } else {
                        warn!(
                            "MetadataPair::Other had an invalid key/value when extracting: '{k}=None'"
                        );
                    }
                }
            }
        }

        raw
    }
}

/// Extracts the date from a string formatted as `YYYY`. Returns [`None`] if the parsing fails
#[must_use]
pub fn extract_date_str(date: &str) -> Option<DateTime<Utc>> {
    let year: i32 = date.parse().ok()?;
    Utc.with_ymd_and_hms(year, 1, 1, 0, 0, 0).single()
}

impl RawMetadata {
    #[must_use]
    pub fn extract_date(&self) -> Option<DateTime<Utc>> {
        self.date.as_deref().and_then(extract_date_str)
    }

    #[must_use]
    pub fn extract_track_num(&self) -> (Option<u16>, Option<u16>) {
        extract_num(self.track_num.as_deref(), self.track_total.as_deref())
    }

    #[must_use]
    pub fn extract_disc_num(&self) -> (Option<u16>, Option<u16>) {
        extract_num(self.disc_num.as_deref(), self.disc_total.as_deref())
    }

    #[must_use]
    pub fn extract_lyric_data(&self) -> Option<LyricData> {
        self.lyrics
            .as_deref()
            .map(LyricData::infer_from_string)
            .or_else(|| {
                self.instrumental
                    .unwrap_or(false)
                    .then_some(LyricData::Instrumental)
            })
    }

    pub fn extract_track_artists(&self) -> Vec<Artist> {
        self.artists
            .as_ref()
            .map(artists_from_string)
            .unwrap_or_default()
    }

    pub fn extract_album_artists(&self) -> Vec<Artist> {
        self.artists
            .as_ref()
            .map(artists_from_string)
            .unwrap_or_default()
    }

    pub fn extract_album(&self, track_artists: &[Artist]) -> Option<(Album, Vec<Artist>)> {
        let album_artists = self
            .album_artists
            .as_ref()
            .map(artists_from_string)
            .unwrap_or_default();

        let (track_num, track_total) = self.extract_track_num();
        let (disc_num, disc_total) = self.extract_disc_num();

        // Returns true if the album name != track name
        let album_name_differs = self
            .album
            .as_deref()
            .zip(self.title.as_deref())
            .is_none_or(|(a, b)| a != b);

        // Returns true if the album artists are not the same as the track artists
        let album_artist_differs = album_artists != track_artists;

        // Returns true if the track/disc value or total is greater is some and is greater than 1
        let multiple_tracks_or_discs = {
            track_num.is_some_and(|v| v > 1)
                || track_total.is_some_and(|v| v > 1)
                || disc_num.is_some_and(|v| v > 1)
                || disc_total.is_some_and(|v| v > 1)
        };

        if !(album_name_differs || multiple_tracks_or_discs || album_artist_differs) {
            return None;
        }

        let mut album = Album::new(
            self.album.clone().unwrap_or(UNKNOWN_ALBUM.to_owned()),
            ArtistGroup::from_artists(&album_artists),
            Vec::new(),
        );
        album.date = self.date.as_deref().and_then(extract_date_str);
        album.track_total = track_total;
        album.disc_total = disc_total;
        Some((album, album_artists))
    }
}

#[must_use]
pub fn extract_num(num: Option<&str>, total: Option<&str>) -> (Option<u16>, Option<u16>) {
    if let Some(track_values) = num {
        match track_values.split_once('/') {
            Some((num, total)) => (num.parse().ok(), total.parse().ok()),
            None => (
                track_values.parse().ok(),
                total.and_then(|v| v.parse().ok()),
            ),
        }
    } else {
        (None, total.and_then(|v| v.parse().ok()))
    }
}

/// Merges a vec of metadata keys, combining groups of artists into one key, leaving everything else untouched
#[must_use]
pub fn merge_keys(keys: Vec<MetadataKey>) -> Vec<MetadataKey> {
    let mut artists: Vec<Artist> = Vec::new();
    let mut album_artists: Vec<Artist> = Vec::new();
    let mut unique = HashSet::new();

    for key in keys {
        match key {
            MetadataKey::Artist(group) => {
                artists.extend(group);
            }
            MetadataKey::AlbumArtists(group) => {
                album_artists.extend(group);
            }
            _ => {
                unique.replace(key);
            }
        }
    }

    let mut keys: Vec<MetadataKey> = unique.into_iter().collect();
    keys.push(MetadataKey::Artist(artists));
    keys.push(MetadataKey::AlbumArtists(album_artists));

    keys
}