Skip to main content

hadris_optical/
image.rs

1use crate::detect::OpticalFormats;
2use crate::error::OpticalFormat;
3
4/// Policy used to choose one filesystem from an optical image.
5#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
6#[non_exhaustive]
7pub enum OpenPolicy {
8    /// Prefer UDF on bridge images, falling back to ISO 9660.
9    #[default]
10    PreferUdf,
11    /// Prefer ISO 9660 on bridge images, falling back to UDF.
12    PreferIso9660,
13    /// Require ISO 9660.
14    Iso9660,
15    /// Require UDF.
16    Udf,
17}
18
19impl OpenPolicy {
20    pub(crate) fn select(self, formats: OpticalFormats) -> Option<OpticalFormat> {
21        match self {
22            Self::PreferUdf if formats.udf().is_some() => Some(OpticalFormat::Udf),
23            Self::PreferUdf if formats.has_iso9660() => Some(OpticalFormat::Iso9660),
24            Self::PreferIso9660 if formats.has_iso9660() => Some(OpticalFormat::Iso9660),
25            Self::PreferIso9660 if formats.udf().is_some() => Some(OpticalFormat::Udf),
26            Self::Iso9660 if formats.has_iso9660() => Some(OpticalFormat::Iso9660),
27            Self::Udf if formats.udf().is_some() => Some(OpticalFormat::Udf),
28            _ => None,
29        }
30    }
31
32    pub(crate) const fn required(self) -> Option<OpticalFormat> {
33        match self {
34            Self::Iso9660 => Some(OpticalFormat::Iso9660),
35            Self::Udf => Some(OpticalFormat::Udf),
36            _ => None,
37        }
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44    use crate::detect::{OpticalFormats, UdfVrs};
45
46    #[test]
47    fn bridge_preferences_select_the_requested_default() {
48        let formats = OpticalFormats::new(true, Some(UdfVrs::Nsr03));
49        assert_eq!(
50            OpenPolicy::PreferUdf.select(formats),
51            Some(OpticalFormat::Udf)
52        );
53        assert_eq!(
54            OpenPolicy::PreferIso9660.select(formats),
55            Some(OpticalFormat::Iso9660)
56        );
57    }
58}