Skip to main content

selene_core/config/common/
core_impls.rs

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