Skip to main content

mux_media/types/
input.rs

1mod finalize;
2pub(crate) mod iters;
3mod to_args;
4
5#[allow(unused_imports)]
6use crate::TryFinalizeInit;
7use crate::{ArcPathBuf, GlobSetPattern, RangeUsize, Result};
8use enum_map::{Enum, EnumMap};
9use std::{
10    fs,
11    path::{Path, PathBuf},
12};
13
14/// An input configuration.
15///
16/// # Warning
17///
18/// This struct is not fully initialized after construction. You **must** call
19/// [`Self::try_finalize_init`] before using some methods (e.g. [`Self::collect_fonts`]).
20#[derive(Clone, Debug, PartialEq)]
21#[non_exhaustive]
22pub struct Input {
23    pub dir: PathBuf,
24    pub range: Option<RangeUsize>,
25    pub skip: Option<GlobSetPattern>,
26    pub depth: u8,
27    pub solo: bool,
28    pub need_num: bool,
29    pub out_need_num: bool,
30    pub dirs: EnumMap<InputFileType, Vec<ArcPathBuf>>,
31}
32
33/// A type of input file.
34#[derive(Copy, Clone, Debug, Enum)]
35#[non_exhaustive]
36pub enum InputFileType {
37    Font,
38    Media,
39}
40
41impl Input {
42    pub(crate) const DEPTH_DEFAULT: u8 = 16;
43
44    pub(crate) fn try_default_dir() -> Result<PathBuf> {
45        Self::try_canonicalize_and_read(".")
46    }
47
48    /// Updates output need number flag.
49    ///
50    /// When enabled, [`Self::iter_media_grouped_by_stem`] will returns media number as stem.
51    pub(crate) fn upd_out_need_num(&mut self, need: bool) {
52        self.out_need_num = need;
53        if need {
54            self.need_num = true;
55        }
56    }
57
58    /// Tries canonicalize path to the directory and read its.
59    ///
60    /// # Errors
61    ///
62    /// Returns an error if the directory doesn't exist or its unreadable.
63    pub(crate) fn try_canonicalize_and_read(dir: impl AsRef<Path>) -> Result<PathBuf> {
64        let dir = fs::canonicalize(dir)?;
65        fs::read_dir(&dir)?;
66        Ok(dir)
67    }
68}