Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Commit

Permalink
Make the package field optional
Browse files Browse the repository at this point in the history
in wasmer.toml, the package field will now be optional. This is to add support
for unnamed packages.

Unnamed packages are useful when you don't want to publish and maintain a
package just for an app. In that case, you can simply associate the package with
a webc hash, instead associating it with a package name/version etc.
  • Loading branch information
ayys committed Mar 29, 2024
1 parent 2b2f52b commit ea611e1
Showing 1 changed file with 35 additions and 16 deletions.
51 changes: 35 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ pub enum ImportsError {
#[non_exhaustive]
pub struct Manifest {
/// Metadata about the package itself.
pub package: Package,
pub package: Option<Package>,
/// The package's dependencies.
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
#[builder(default)]
Expand Down Expand Up @@ -742,12 +742,15 @@ impl Manifest {
.map_err(|_e| ManifestError::MissingManifest(manifest_path_buf))?;
let mut manifest: Self = toml::from_str(contents.as_str())?;

if manifest.package.readme.is_none() {
manifest.package.readme = locate_file(path, README_PATHS);
}
if let Some(mut package) = manifest.package.clone() {
if package.readme.is_none() {
package.readme = locate_file(path, README_PATHS);
}

if manifest.package.license_file.is_none() {
manifest.package.license_file = locate_file(path, LICENSE_PATHS);
if package.license_file.is_none() {
package.license_file = locate_file(path, LICENSE_PATHS);
}
manifest.package = Some(package);
}
manifest.validate()?;

Expand Down Expand Up @@ -818,12 +821,14 @@ impl Manifest {
}
}

if let Some(entrypoint) = self.package.entrypoint.as_deref() {
if !commands.contains_key(entrypoint) {
return Err(ValidationError::InvalidEntrypoint {
entrypoint: entrypoint.to_string(),
available_commands: commands.keys().map(ToString::to_string).collect(),
});
if let Some(package) = &self.package {
if let Some(entrypoint) = package.entrypoint.as_deref() {
if !commands.contains_key(entrypoint) {
return Err(ValidationError::InvalidEntrypoint {
entrypoint: entrypoint.to_string(),
available_commands: commands.keys().map(ToString::to_string).collect(),
});
}
}
}

Expand Down Expand Up @@ -868,7 +873,7 @@ fn locate_file(path: &Path, candidates: &[&str]) -> Option<PathBuf> {
impl ManifestBuilder {
pub fn new(package: Package) -> Self {
let mut builder = ManifestBuilder::default();
builder.package(package);
builder.package(Some(package));
builder
}

Expand Down Expand Up @@ -958,7 +963,7 @@ mod tests {
#[test]
fn test_to_string() {
Manifest {
package: Package {
package: Some(Package {
name: "package/name".to_string(),
version: Version::parse("1.0.0").unwrap(),
description: "test".to_string(),
Expand All @@ -972,7 +977,7 @@ mod tests {
rename_commands_to_raw_command_name: false,
entrypoint: None,
private: false,
},
}),
dependencies: HashMap::new(),
modules: vec![Module {
name: "test".to_string(),
Expand Down Expand Up @@ -1198,7 +1203,21 @@ annotations = { file = "Runefile.yml", kind = "yaml" }
description = "The best package."
};
let manifest: Manifest = wasmer_toml.try_into().unwrap();
assert!(!manifest.package.disable_command_rename);
if let Some(package) = manifest.package {
assert!(!package.disable_command_rename);
}
}

#[test]
fn parse_manifest_without_package_section() {
let wasmer_toml = toml! {
[[module]]
name = "test-module"
source = "data.wasm"
abi = "wasi"
};
let manifest: Manifest = wasmer_toml.try_into().unwrap();
assert!(manifest.package.is_none());
}

#[test]
Expand Down

0 comments on commit ea611e1

Please sign in to comment.