1use regex::Match;
2use serde::{Deserialize, Serialize};
3
4use crate::regex_wrapper::RegexWrapper;
5
6use super::StandardEpisode;
7
8#[derive(Serialize, Deserialize, Clone)]
9pub struct Profile {
10 pub name: String,
11 pub search_prefix: Option<String>,
13 pub parse_regex: RegexWrapper,
15 pub relocate: Option<String>,
17}
18
19impl Profile {
20 pub fn parse_name<'s, 't>(&'s self, name: &'t str) -> Option<StandardEpisode> {
21 let mut out = StandardEpisode::default();
22 out.season = 1;
23 let captures = self.parse_regex.captures(name)?;
24 for name in self.parse_regex.capture_names().filter_map(|x| x) {
25 let value = match captures.name(name).as_ref().map(Match::as_str) {
26 Some(x) => x,
27 None => continue,
28 };
29 match name {
30 "title" => out.title = value.to_string(),
31 "season" => out.season = value.parse().ok()?,
32 "episode" => out.episode = value.parse().ok()?,
33 "checksum" => {
34 out.checksum = u32::from_le_bytes(hex::decode(value).ok()?.try_into().ok()?)
35 }
36 name => {
37 out.ext.insert(name.to_string(), value.to_string());
38 }
39 }
40 }
41 Some(out)
42 }
43}