Skip to main content

selene_core/config/common/
core_impls.rs

1use std::{
2    fs,
3    path::{Path, PathBuf},
4};
5
6use lunar_lib::config::Config;
7
8use crate::{
9    config::common::{CommonConfig, TranscodeSettings},
10    media_container::{Codec, ContainerFormat},
11    utils::recurse_list_from_root,
12};
13
14// Config trait
15impl Config for CommonConfig {
16    const CONFIG_FILE_NAME: &'static str = "common";
17
18    fn config_dir() -> Option<&'static Path> {
19        Some(crate::config_dir())
20    }
21}
22
23// Accessors
24impl CommonConfig {
25    #[must_use]
26    pub fn library_dir(&self) -> Option<&Path> {
27        self.library.library_directory.as_deref()
28    }
29
30    #[must_use]
31    pub fn sources(&self) -> &[PathBuf] {
32        &self.library.source_directories
33    }
34
35    /// Returns all supported audio files from the configured source directories
36    #[must_use]
37    pub fn get_source_files(&self) -> Vec<PathBuf> {
38        self.library
39            .source_directories
40            .iter()
41            .flat_map(|path| -> Vec<PathBuf> {
42                recurse_list_from_root(path, false)
43                    .filter_map(|f| {
44                        let mut file = fs::File::open(&f).ok()?;
45                        ContainerFormat::from_file(&mut file)?;
46                        Some(f)
47                    })
48                    .collect()
49            })
50            .collect()
51    }
52
53    #[must_use] 
54    pub fn transcode_to(
55        &self,
56        container_format: ContainerFormat,
57        codec: Codec,
58    ) -> Option<TranscodeSettings> {
59        self.library
60            .transcode_options
61            .iter()
62            .find(|s| {
63                s.from_format.is_none_or(|c| c == container_format)
64                    && s.from_codec.is_none_or(|c| c == codec)
65            })
66            .copied()
67    }
68}