wasmer_package/package/
strictness.rs

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