wasmer_package/package/
strictness.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use std::{fmt::Debug, path::Path};

use anyhow::Error;

use super::ManifestError;

/// The strictness to use when working with a
/// [`crate::wasmer_package::Package`].
///
/// This can be useful when loading a package that may be edited interactively
/// or if you just want to use a package and don't care if the manifest is
/// invalid.
#[derive(Debug, Default, Copy, Clone, PartialEq)]
pub enum Strictness {
    /// Prefer to lose data rather than error out.
    #[default]
    Lossy,
    /// All package issues should be errors.
    Strict,
}

impl Strictness {
    pub(crate) fn is_strict(self) -> bool {
        matches!(self, Strictness::Strict)
    }

    pub(crate) fn on_error(&self, _path: &Path, error: Error) -> Result<(), Error> {
        match self {
            Strictness::Lossy => Ok(()),
            Strictness::Strict => Err(error),
        }
    }

    pub(crate) fn outside_base_directory(
        &self,
        path: &Path,
        base_dir: &Path,
    ) -> Result<(), ManifestError> {
        match self {
            Strictness::Lossy => todo!(),
            Strictness::Strict => Err(ManifestError::OutsideBaseDirectory {
                path: path.to_path_buf(),
                base_dir: base_dir.to_path_buf(),
            }),
        }
    }

    pub(crate) fn missing_file(&self, path: &Path, base_dir: &Path) -> Result<(), ManifestError> {
        match self {
            Strictness::Lossy => Ok(()),
            Strictness::Strict => Err(ManifestError::MissingFile {
                path: path.to_path_buf(),
                base_dir: base_dir.to_path_buf(),
            }),
        }
    }
}