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
use regex::Match;
use serde::{Deserialize, Serialize};

use crate::regex_wrapper::RegexWrapper;

use super::StandardEpisode;

#[derive(Serialize, Deserialize, Clone)]
pub struct Profile {
    pub name: String,
    /// initial parts of search phrase, of which is followed by space and series name
    pub search_prefix: Option<String>,
    /// torrent name parsing regex
    pub parse_regex: RegexWrapper,
    /// if set, is a default path for series relocation. I.e. `relocate`/<series-name>/Season X/episode1.mp4
    pub relocate: Option<String>,
}

impl Profile {
    pub fn parse_name<'s, 't>(&'s self, name: &'t str) -> Option<StandardEpisode> {
        let mut out = StandardEpisode::default();
        out.season = 1;
        let captures = self.parse_regex.captures(name)?;
        for name in self.parse_regex.capture_names().filter_map(|x| x) {
            let value = match captures.name(name).as_ref().map(Match::as_str) {
                Some(x) => x,
                None => continue,
            };
            match name {
                "title" => out.title = value.to_string(),
                "season" => out.season = value.parse().ok()?,
                "episode" => out.episode = value.parse().ok()?,
                "checksum" => {
                    out.checksum = u32::from_le_bytes(hex::decode(value).ok()?.try_into().ok()?)
                }
                name => {
                    out.ext.insert(name.to_string(), value.to_string());
                }
            }
        }
        Some(out)
    }
}