wasmer_package/package/
strictness.rs

1use std::{fmt::Debug, path::Path};
2
3use super::ManifestError;
4
5/// The strictness to use when working with a
6/// `Package` values from this crate.
7///
8/// This can be useful when loading a package that may be edited interactively
9/// or if you just want to use a package and don't care if the manifest is
10/// invalid.
11#[derive(Debug, Default, Copy, Clone, PartialEq)]
12pub enum Strictness {
13    /// Prefer to lose data rather than error out.
14    #[default]
15    Lossy,
16    /// All package issues should be errors.
17    Strict,
18}
19
20impl Strictness {
21    pub(crate) fn is_strict(self) -> bool {
22        matches!(self, Strictness::Strict)
23    }
24
25    pub(crate) fn outside_base_directory(
26        &self,
27        path: &Path,
28        base_dir: &Path,
29    ) -> Result<(), ManifestError> {
30        match self {
31            Strictness::Lossy => todo!(),
32            Strictness::Strict => Err(ManifestError::OutsideBaseDirectory {
33                path: path.to_path_buf(),
34                base_dir: base_dir.to_path_buf(),
35            }),
36        }
37    }
38
39    pub(crate) fn missing_file(&self, path: &Path, base_dir: &Path) -> Result<(), ManifestError> {
40        match self {
41            Strictness::Lossy => Ok(()),
42            Strictness::Strict => Err(ManifestError::MissingFile {
43                path: path.to_path_buf(),
44                base_dir: base_dir.to_path_buf(),
45            }),
46        }
47    }
48}