use std::sync::LazyLock;
use crate::matcher::rule_loader::RuleSet;
use crate::matcher::span::{MatchSpan, Property};
use crate::priority;
use crate::properties::{
aspect_ratio, bit_rate, bonus, crc32, date, episode_count, episodes, language, part, size,
subtitle_language, uuid, version, website, year,
};
pub(super) static VIDEO_CODEC_RULES: LazyLock<RuleSet> =
LazyLock::new(|| RuleSet::from_toml(include_str!("../rules/video_codec.toml")));
pub(super) static COLOR_DEPTH_RULES: LazyLock<RuleSet> =
LazyLock::new(|| RuleSet::from_toml(include_str!("../rules/color_depth.toml")));
pub(super) static COUNTRY_RULES: LazyLock<RuleSet> =
LazyLock::new(|| RuleSet::from_toml(include_str!("../rules/country.toml")));
pub(super) static STREAMING_SERVICE_RULES: LazyLock<RuleSet> =
LazyLock::new(|| RuleSet::from_toml(include_str!("../rules/streaming_service.toml")));
pub(super) static VIDEO_PROFILE_RULES: LazyLock<RuleSet> =
LazyLock::new(|| RuleSet::from_toml(include_str!("../rules/video_profile.toml")));
pub(super) static EPISODE_DETAILS_RULES: LazyLock<RuleSet> =
LazyLock::new(|| RuleSet::from_toml(include_str!("../rules/episode_details.toml")));
pub(super) static ANIME_BONUS_RULES: LazyLock<RuleSet> =
LazyLock::new(|| RuleSet::from_toml(include_str!("../rules/anime_bonus.toml")));
pub(super) static EDITION_RULES: LazyLock<RuleSet> =
LazyLock::new(|| RuleSet::from_toml(include_str!("../rules/edition.toml")));
pub(super) static AUDIO_CODEC_RULES: LazyLock<RuleSet> =
LazyLock::new(|| RuleSet::from_toml(include_str!("../rules/audio_codec.toml")));
pub(super) static AUDIO_PROFILE_RULES: LazyLock<RuleSet> =
LazyLock::new(|| RuleSet::from_toml(include_str!("../rules/audio_profile.toml")));
pub(super) static AUDIO_CHANNELS_RULES: LazyLock<RuleSet> =
LazyLock::new(|| RuleSet::from_toml(include_str!("../rules/audio_channels.toml")));
pub(super) static OTHER_RULES: LazyLock<RuleSet> =
LazyLock::new(|| RuleSet::from_toml(include_str!("../rules/other.toml")));
pub(super) static OTHER_POSITIONAL_RULES: LazyLock<RuleSet> =
LazyLock::new(|| RuleSet::from_toml(include_str!("../rules/other_positional.toml")));
pub(super) static VIDEO_API_RULES: LazyLock<RuleSet> =
LazyLock::new(|| RuleSet::from_toml(include_str!("../rules/video_api.toml")));
pub(super) static SOURCE_RULES: LazyLock<RuleSet> =
LazyLock::new(|| RuleSet::from_toml(include_str!("../rules/source.toml")));
pub(super) static SCREEN_SIZE_RULES: LazyLock<RuleSet> =
LazyLock::new(|| RuleSet::from_toml(include_str!("../rules/screen_size.toml")));
pub(super) static CONTAINER_RULES: LazyLock<RuleSet> =
LazyLock::new(|| RuleSet::from_toml(include_str!("../rules/container.toml")));
pub(super) static FRAME_RATE_RULES: LazyLock<RuleSet> =
LazyLock::new(|| RuleSet::from_toml(include_str!("../rules/frame_rate.toml")));
pub(super) static LANGUAGE_RULES: LazyLock<RuleSet> =
LazyLock::new(|| RuleSet::from_toml(include_str!("../rules/language.toml")));
pub(super) static SUBTITLE_LANGUAGE_RULES: LazyLock<RuleSet> =
LazyLock::new(|| RuleSet::from_toml(include_str!("../rules/subtitle_language.toml")));
pub(super) static EPISODE_FORMAT_RULES: LazyLock<RuleSet> =
LazyLock::new(|| RuleSet::from_toml(include_str!("../rules/episode_format.toml")));
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum SegmentScope {
FilenameOnly,
AllSegments,
}
pub(super) type LegacyMatcherFn = fn(&str) -> Vec<MatchSpan>;
pub(super) struct TomlRule {
pub rules: &'static LazyLock<RuleSet>,
pub property: Property,
pub priority: i32,
pub scope: SegmentScope,
}
use SegmentScope::{AllSegments, FilenameOnly};
use priority::{DEFAULT, HEURISTIC, POSITIONAL, STRUCTURAL, VOCABULARY};
const fn r(
rules: &'static LazyLock<RuleSet>,
property: Property,
priority: i32,
scope: SegmentScope,
) -> TomlRule {
TomlRule {
rules,
property,
priority,
scope,
}
}
#[rustfmt::skip]
pub(super) fn build_toml_rules() -> Vec<TomlRule> {
use Property::*;
vec![
r(&VIDEO_CODEC_RULES, VideoCodec, DEFAULT, AllSegments),
r(&COLOR_DEPTH_RULES, ColorDepth, DEFAULT, AllSegments),
r(&AUDIO_CODEC_RULES, AudioCodec, DEFAULT, AllSegments),
r(&AUDIO_PROFILE_RULES, AudioProfile, VOCABULARY, AllSegments),
r(&AUDIO_CHANNELS_RULES, AudioChannels, HEURISTIC, AllSegments),
r(&FRAME_RATE_RULES, FrameRate, DEFAULT, AllSegments),
r(&SCREEN_SIZE_RULES, ScreenSize, DEFAULT, AllSegments),
r(&STREAMING_SERVICE_RULES, StreamingService, VOCABULARY, FilenameOnly),
r(&VIDEO_PROFILE_RULES, VideoProfile, POSITIONAL, FilenameOnly),
r(&EPISODE_DETAILS_RULES, EpisodeDetails, HEURISTIC, FilenameOnly),
r(&ANIME_BONUS_RULES, EpisodeDetails, HEURISTIC, FilenameOnly),
r(&EPISODE_FORMAT_RULES, EpisodeFormat, HEURISTIC, FilenameOnly),
r(&EDITION_RULES, Edition, DEFAULT, AllSegments),
r(&OTHER_RULES, Other, DEFAULT, AllSegments),
r(&OTHER_POSITIONAL_RULES, Other, POSITIONAL, FilenameOnly),
r(&VIDEO_API_RULES, VideoApi, DEFAULT, FilenameOnly),
r(&SOURCE_RULES, Source, DEFAULT, AllSegments),
r(&CONTAINER_RULES, Container, STRUCTURAL, FilenameOnly),
r(&COUNTRY_RULES, Country, POSITIONAL, FilenameOnly),
r(&LANGUAGE_RULES, Language, HEURISTIC, AllSegments),
r(&SUBTITLE_LANGUAGE_RULES, SubtitleLanguage, HEURISTIC, FilenameOnly),
]
}
#[rustfmt::skip]
pub(super) fn build_legacy_matchers() -> Vec<LegacyMatcherFn> {
vec![
aspect_ratio::find_matches,
year::find_matches,
date::find_matches,
episodes::find_matches,
episode_count::find_matches,
language::find_matches,
subtitle_language::find_matches,
crc32::find_matches,
uuid::find_matches,
website::find_matches,
size::find_matches,
bit_rate::find_matches,
part::find_matches,
bonus::find_matches,
version::find_matches,
]
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn toml_rule_sets_parse_and_have_entries() {
assert!(VIDEO_CODEC_RULES.exact_count() >= 10);
assert!(COLOR_DEPTH_RULES.exact_count() >= 3);
assert!(STREAMING_SERVICE_RULES.exact_count() >= 10);
assert!(VIDEO_PROFILE_RULES.exact_count() >= 2);
assert!(EPISODE_DETAILS_RULES.exact_count() >= 4);
assert!(EDITION_RULES.exact_count() >= 10);
}
#[test]
fn registry_tables_are_populated() {
assert!(build_toml_rules().len() >= 20, "TOML rule registry shrank?");
assert!(
build_legacy_matchers().len() >= 10,
"legacy matcher registry shrank?"
);
}
}