Skip to content

Commit

Permalink
Merge pull request #41 from lann/refactor-common-loader
Browse files Browse the repository at this point in the history
ref(*)!: Refactors loader to use the new common package (subset of #34)
  • Loading branch information
lann authored Jun 15, 2024
2 parents 6d7aa0e + fd82d4a commit 44126e4
Show file tree
Hide file tree
Showing 29 changed files with 785 additions and 1,149 deletions.
11 changes: 9 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions ci/run-ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

set -ex

cargo clippy --workspace
cargo test --workspace
cargo clippy --workspace --all-features
cargo test --workspace --all-features
(cd crates/wasm-pkg-loader/tests/e2e && cargo run)
6 changes: 6 additions & 0 deletions crates/wasm-pkg-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ version.workspace = true
authors.workspace = true
license.workspace = true

[features]
metadata-client = ["dep:reqwest"]

[dependencies]
anyhow = "1.0"
dirs = "5.0.1"
http = "1.1.0"
reqwest = { version = "0.12.0", features = ["json"], optional = true }
semver = "1.0.23"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
toml = "0.8.13"
Expand Down
43 changes: 33 additions & 10 deletions crates/wasm-pkg-common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ use std::{

use serde::{Deserialize, Serialize};

use crate::{label::Label, package::PackageRef, Error, Registry};
use crate::{label::Label, package::PackageRef, registry::Registry, Error};

mod toml;

pub const DEFAULT_REGISTRY: &str = "bytecodealliance.org";
const DEFAULT_FALLBACK_NAMESPACE_REGISTRIES: &[(&str, &str)] = &[
// TODO: Switch to wasi.dev once that is ready
("wasi", "bytecodealliance.org"),
("ba", "bytecodealliance.org"),
];

/// Wasm Package registry configuration.
///
Expand All @@ -23,15 +27,22 @@ pub struct Config {
default_registry: Option<Registry>,
namespace_registries: HashMap<Label, Registry>,
package_registry_overrides: HashMap<PackageRef, Registry>,
// Note: these are only used for hard-coded defaults currently
fallback_namespace_registries: HashMap<Label, Registry>,
registry_configs: HashMap<Registry, RegistryConfig>,
}

impl Default for Config {
fn default() -> Self {
let fallback_namespace_registries = DEFAULT_FALLBACK_NAMESPACE_REGISTRIES
.iter()
.map(|(k, v)| (k.parse().unwrap(), v.parse().unwrap()))
.collect();
Self {
default_registry: Some(DEFAULT_REGISTRY.parse().unwrap()),
default_registry: Default::default(),
namespace_registries: Default::default(),
package_registry_overrides: Default::default(),
fallback_namespace_registries,
registry_configs: Default::default(),
}
}
Expand All @@ -42,11 +53,12 @@ impl Config {
///
/// Note that this may differ from the `Default` implementation, which
/// includes hard-coded global defaults.
pub fn new() -> Self {
pub fn empty() -> Self {
Self {
default_registry: Default::default(),
namespace_registries: Default::default(),
package_registry_overrides: Default::default(),
fallback_namespace_registries: Default::default(),
registry_configs: Default::default(),
}
}
Expand All @@ -68,7 +80,7 @@ impl Config {
Ok(config)
}

/// Reads config from
/// Reads config from the default global config file location
pub fn read_global_config() -> Result<Option<Self>, Error> {
let Some(config_dir) = dirs::config_dir() else {
return Ok(None);
Expand Down Expand Up @@ -100,14 +112,18 @@ impl Config {
let Self {
default_registry,
namespace_registries,
package_registry_overrides: package_registries,
package_registry_overrides,
fallback_namespace_registries,
registry_configs,
} = other;
if default_registry.is_some() {
self.default_registry = default_registry;
}
self.namespace_registries.extend(namespace_registries);
self.package_registry_overrides.extend(package_registries);
self.package_registry_overrides
.extend(package_registry_overrides);
self.fallback_namespace_registries
.extend(fallback_namespace_registries);
for (registry, config) in registry_configs {
match self.registry_configs.entry(registry) {
Entry::Occupied(mut occupied) => occupied.get_mut().merge(config),
Expand All @@ -124,13 +140,16 @@ impl Config {
/// - A package registry exactly matching the package
/// - A namespace registry matching the package's namespace
/// - The default registry
/// - Hard-coded fallbacks for certain well-known namespaces
pub fn resolve_registry(&self, package: &PackageRef) -> Option<&Registry> {
if let Some(reg) = self.package_registry_overrides.get(package) {
Some(reg)
} else if let Some(reg) = self.namespace_registries.get(package.namespace()) {
Some(reg)
} else if let Some(reg) = self.default_registry.as_ref() {
Some(reg)
} else if let Some(reg) = self.fallback_namespace_registries.get(package.namespace()) {
Some(reg)
} else {
None
}
Expand All @@ -142,6 +161,8 @@ impl Config {
}

/// Sets the default registry.
///
/// To unset the default registry, pass `None`.
pub fn set_default_registry(&mut self, registry: Option<Registry>) {
self.default_registry = registry;
}
Expand Down Expand Up @@ -189,7 +210,7 @@ impl Config {
}
}

#[derive(Default)]
#[derive(Clone, Default)]
pub struct RegistryConfig {
backend_type: Option<String>,
backend_configs: HashMap<String, ::toml::Table>,
Expand Down Expand Up @@ -221,6 +242,8 @@ impl RegistryConfig {
}

/// Sets the backend type override.
///
/// To unset the backend type override, pass `None`.
pub fn set_backend_type(&mut self, backend_type: Option<String>) {
self.backend_type = backend_type;
}
Expand Down Expand Up @@ -248,11 +271,11 @@ impl RegistryConfig {
/// Set the backend config of the given type by serializing the given config.
pub fn set_backend_config<T: Serialize>(
&mut self,
backend_type: String,
backend_type: impl Into<String>,
backend_config: T,
) -> Result<(), Error> {
let table = ::toml::Table::try_from(backend_config).map_err(Error::invalid_config)?;
self.backend_configs.insert(backend_type, table);
self.backend_configs.insert(backend_type.into(), table);
Ok(())
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/wasm-pkg-common/src/config/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::collections::HashMap;

use serde::Deserialize;

use crate::{label::Label, package::PackageRef, Registry};
use crate::{label::Label, package::PackageRef, registry::Registry};

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
Expand Down Expand Up @@ -37,6 +37,7 @@ impl From<TomlConfig> for super::Config {
default_registry,
namespace_registries,
package_registry_overrides,
fallback_namespace_registries: Default::default(),
registry_configs,
}
}
Expand Down
39 changes: 29 additions & 10 deletions crates/wasm-pkg-common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,52 @@
use http::uri::InvalidUri;
use label::Label;

pub mod config;
mod label;
mod package;
pub mod label;
pub mod metadata;
pub mod package;
pub mod registry;

use label::InvalidLabel;
pub use registry::Registry;

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("error reading config file: {0}")]
ConfigFileIoError(#[source] std::io::Error),
#[error("failed to get registry credentials: {0:#}")]
CredentialError(#[source] anyhow::Error),
#[error("invalid config: {0}")]
InvalidConfig(#[source] Box<dyn std::error::Error>),
InvalidConfig(#[source] anyhow::Error),
#[error("invalid content: {0}")]
InvalidContent(String),
#[error("invalid content digest: {0}")]
InvalidContentDigest(String),
#[error("invalid package manifest: {0}")]
InvalidPackageManifest(String),
#[error("invalid package pattern: {0}")]
InvalidPackagePattern(String),
#[error("invalid label: {0}")]
InvalidLabel(#[from] InvalidLabel),
InvalidLabel(#[from] label::InvalidLabel),
#[error("invalid package ref: {0}")]
InvalidPackageRef(String),
#[error("invalid registry: {0}")]
InvalidRegistry(#[from] InvalidUri),
#[error("invalid registry metadata: {0}")]
InvalidRegistryMetadata(#[source] serde_json::Error),
InvalidRegistryMetadata(#[source] anyhow::Error),
#[error("invalid version: {0}")]
InvalidVersion(#[from] semver::Error),
#[error("IO error: {0}")]
IoError(#[from] std::io::Error),
#[error("no registry configured for namespace {0:?}")]
NoRegistryForNamespace(Label),
#[error("registry error: {0}")]
RegistryError(#[source] anyhow::Error),
#[error("registry metadata error: {0:#}")]
RegistryMetadataError(#[source] anyhow::Error),
#[error("version not found: {0}")]
VersionNotFound(semver::Version),
}

impl Error {
fn invalid_config(err: impl std::error::Error + 'static) -> Self {
Self::InvalidConfig(Box::new(err))
fn invalid_config(err: impl Into<anyhow::Error>) -> Self {
Self::InvalidConfig(err.into())
}
}
Loading

0 comments on commit 44126e4

Please sign in to comment.