Skip to content

Commit

Permalink
feat(validation): add manifest validation
Browse files Browse the repository at this point in the history
This commit adds validation functions along with utility accessors to
`Manifest` to enable checking WADM manifests for common errors.

As the validation functions are exposed they can be used by downstream
crates (ex. `wash`) to validate WADM manifests or try to catch
errors *before* a manifest is used.

Signed-off-by: Victor Adossi <vadossi@cosmonic.com>
  • Loading branch information
vados-cosmonic committed May 20, 2024
1 parent 334db54 commit a5a2d6d
Show file tree
Hide file tree
Showing 10 changed files with 715 additions and 41 deletions.
38 changes: 38 additions & 0 deletions src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::{BTreeMap, HashMap};
use serde::{Deserialize, Serialize};

pub(crate) mod internal;
pub mod validation;

/// The default weight for a spread
pub const DEFAULT_SPREAD_WEIGHT: usize = 100;
Expand Down Expand Up @@ -59,6 +60,38 @@ impl Manifest {
.get(DESCRIPTION_ANNOTATION_KEY)
.map(|v| v.as_str())
}

/// Returns the components in the manifest
pub fn components(&self) -> impl Iterator<Item = &Component> {
self.spec.components.iter()
}

/// Returns only the WebAssembly components in the manifest
pub fn wasm_components(&self) -> impl Iterator<Item = &Component> {
self.components()
.filter(|c| matches!(c.properties, Properties::Component { .. }))
}

/// Returns only the provider components in the manifest
pub fn capability_providers(&self) -> impl Iterator<Item = &Component> {
self.components()
.filter(|c| matches!(c.properties, Properties::Capability { .. }))
}

/// Returns a map of component names to components in the manifest
pub fn component_lookup(&self) -> HashMap<&String, &Component> {
self.components()
.map(|c| (&c.name, c))
.collect::<HashMap<&String, &Component>>()
}

/// Returns only links in the manifest
pub fn links(&self) -> impl Iterator<Item = &Trait> {
self.components()
.flat_map(|c| c.traits.as_ref())
.flatten()
.filter(|t| t.is_link())
}
}

/// The metadata describing the manifest
Expand Down Expand Up @@ -153,6 +186,11 @@ impl Trait {
}
}

/// Check if a trait is a link
pub fn is_link(&self) -> bool {
self.trait_type == LINK_TRAIT
}

/// Helper that creates a new spreadscaler type trait with the given properties
pub fn new_spreadscaler(props: SpreadScalerProperty) -> Trait {
Trait {
Expand Down
Loading

0 comments on commit a5a2d6d

Please sign in to comment.