Skip to content

Commit

Permalink
common: Split [package_registries] into [namespace_registries] and [p…
Browse files Browse the repository at this point in the history
…ackage_registry_overrides]
  • Loading branch information
lann committed May 29, 2024
1 parent dec25d2 commit b3c298a
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 70 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ license = "Apache-2.0 WITH LLVM-exception"
[workspace.dependencies]
tokio = "1.35.1"
tracing = "0.1.40"
wasm-pkg-common = { version = "0.3.0", path = "crates/wasm-pkg-common" }
wasm-pkg-loader = { version = "0.3.0", path = "crates/wasm-pkg-loader" }
24 changes: 12 additions & 12 deletions crates/wasm-pkg-common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub const DEFAULT_REGISTRY: &str = "bytecodealliance.org";
pub struct Config {
default_registry: Option<Registry>,
namespace_registries: HashMap<Label, Registry>,
package_registries: HashMap<PackageRef, Registry>,
package_registry_overrides: HashMap<PackageRef, Registry>,
registry_configs: HashMap<Registry, RegistryConfig>,
}

Expand All @@ -31,7 +31,7 @@ impl Default for Config {
Self {
default_registry: Some(DEFAULT_REGISTRY.parse().unwrap()),
namespace_registries: Default::default(),
package_registries: Default::default(),
package_registry_overrides: Default::default(),
registry_configs: Default::default(),
}
}
Expand All @@ -46,7 +46,7 @@ impl Config {
Self {
default_registry: Default::default(),
namespace_registries: Default::default(),
package_registries: Default::default(),
package_registry_overrides: Default::default(),
registry_configs: Default::default(),
}
}
Expand Down Expand Up @@ -100,14 +100,14 @@ impl Config {
let Self {
default_registry,
namespace_registries,
package_registries,
package_registry_overrides: package_registries,
registry_configs,
} = other;
if default_registry.is_some() {
self.default_registry = default_registry;
}
self.namespace_registries.extend(namespace_registries);
self.package_registries.extend(package_registries);
self.package_registry_overrides.extend(package_registries);
for (registry, config) in registry_configs {
match self.registry_configs.entry(registry) {
Entry::Occupied(mut occupied) => occupied.get_mut().merge(config),
Expand All @@ -125,7 +125,7 @@ impl Config {
/// - A namespace registry matching the package's namespace
/// - The default registry
pub fn resolve_registry(&self, package: &PackageRef) -> Option<&Registry> {
if let Some(reg) = self.package_registries.get(package) {
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)
Expand Down Expand Up @@ -158,16 +158,16 @@ impl Config {
self.namespace_registries.insert(namespace, registry);
}

/// Returns a registry configured for the given exact package.
/// Returns a registry override configured for the given package.
///
/// Does not fall back to namespace or default registries; see [`Self::resolve`].
pub fn package_registry(&self, package: &PackageRef) -> Option<&Registry> {
self.package_registries.get(package)
pub fn package_registry_override(&self, package: &PackageRef) -> Option<&Registry> {
self.package_registry_overrides.get(package)
}

/// Sets a registry for the given exact package.
pub fn set_package_registry(&mut self, package: PackageRef, registry: Registry) {
self.package_registries.insert(package, registry);
/// Sets a registry override for the given package.
pub fn set_package_registry_override(&mut self, package: PackageRef, registry: Registry) {
self.package_registry_overrides.insert(package, registry);
}

/// Returns [`RegistryConfig`] for the given registry.
Expand Down
66 changes: 12 additions & 54 deletions crates/wasm-pkg-common/src/config/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@

use std::collections::HashMap;

use serde::{Deserialize, Deserializer};
use serde::Deserialize;

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

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub struct TomlConfig {
default_registry: Option<Registry>,
#[serde(default)]
package_registries: HashMap<PackagePattern, Registry>,
namespace_registries: HashMap<Label, Registry>,
#[serde(default)]
package_registry_overrides: HashMap<PackageRef, Registry>,
#[serde(default)]
registry: HashMap<Registry, TomlRegistryConfig>,
}
Expand All @@ -21,23 +23,11 @@ impl From<TomlConfig> for super::Config {
fn from(value: TomlConfig) -> Self {
let TomlConfig {
default_registry,
package_registries: pattern_registries,
namespace_registries,
package_registry_overrides,
registry,
} = value;

let mut namespace_registries: HashMap<Label, Registry> = Default::default();
let mut package_registries: HashMap<PackageRef, Registry> = Default::default();
for (pattern, registry) in pattern_registries {
match pattern {
PackagePattern::NamespaceWildcard(namespace) => {
namespace_registries.insert(namespace, registry);
}
PackagePattern::ExactPackage(package) => {
package_registries.insert(package, registry);
}
}
}

let registry_configs = registry
.into_iter()
.map(|(reg, config)| (reg, config.into()))
Expand All @@ -46,36 +36,12 @@ impl From<TomlConfig> for super::Config {
Self {
default_registry,
namespace_registries,
package_registries,
package_registry_overrides,
registry_configs,
}
}
}

#[derive(Debug, PartialEq, Eq, Hash, Deserialize)]
#[serde(try_from = "String")]
enum PackagePattern {
NamespaceWildcard(Label),
ExactPackage(PackageRef),
}

impl TryFrom<String> for PackagePattern {
type Error = Error;

fn try_from(mut value: String) -> Result<Self, Self::Error> {
if value.ends_with(":*") {
value.truncate(value.len() - 2);
Ok(PackagePattern::NamespaceWildcard(value.try_into()?))
} else if value.contains(':') {
Ok(PackagePattern::ExactPackage(value.try_into()?))
} else {
Err(Error::InvalidPackagePattern(
"keys must be full package names or <namespace>:* wildcards".into(),
))
}
}
}

#[derive(Deserialize)]
struct TomlRegistryConfig {
#[serde(rename = "type")]
Expand All @@ -97,16 +63,6 @@ impl From<TomlRegistryConfig> for super::RegistryConfig {
}
}

impl<'de> Deserialize<'de> for Registry {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
use serde::de::Error;
Registry::try_from(String::deserialize(deserializer)?).map_err(D::Error::custom)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -116,8 +72,10 @@ mod tests {
let toml_config = toml::toml! {
default_registry = "example.com"

[package_registries]
"wasi:*" = "wasi.dev"
[namespace_registries]
wasi = "wasi.dev"

[package_registry_overrides]
"example:foo" = "example.com"

[registry."wasi.dev".oci]
Expand Down
11 changes: 10 additions & 1 deletion crates/wasm-pkg-common/src/label.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use serde::{Deserialize, Serialize};

/// A Component Model kebab-case label.
#[derive(Clone, PartialEq, Eq, Hash)]
#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(into = "String", try_from = "String")]
pub struct Label(String);

impl AsRef<str> for Label {
Expand All @@ -20,6 +23,12 @@ impl std::fmt::Debug for Label {
}
}

impl From<Label> for String {
fn from(value: Label) -> Self {
value.0
}
}

impl TryFrom<String> for Label {
type Error = InvalidLabel;

Expand Down
11 changes: 10 additions & 1 deletion crates/wasm-pkg-common/src/package.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use serde::{Deserialize, Serialize};

use crate::{label::Label, Error};

/// A package reference, consisting of kebab-case namespace and name, e.g. `wasm-pkg:loader`.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(into = "String", try_from = "String")]
pub struct PackageRef {
namespace: Label,
name: Label,
Expand All @@ -25,6 +28,12 @@ impl std::fmt::Display for PackageRef {
}
}

impl From<PackageRef> for String {
fn from(value: PackageRef) -> Self {
value.to_string()
}
}

impl TryFrom<String> for PackageRef {
type Error = Error;

Expand Down
11 changes: 9 additions & 2 deletions crates/wasm-pkg-common/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ use std::{
};

use http::uri::Authority;
use serde::{de::DeserializeOwned, Deserialize};
use serde::{de::DeserializeOwned, Deserialize, Serialize};

use crate::Error;

/// A registry identifier, which should be a valid HTTP Host.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(into = "String", try_from = "String")]
pub struct Registry(Authority);

impl Registry {
Expand All @@ -36,6 +37,12 @@ impl std::fmt::Display for Registry {
}
}

impl From<Registry> for String {
fn from(value: Registry) -> Self {
value.to_string()
}
}

impl std::str::FromStr for Registry {
type Err = Error;

Expand Down

0 comments on commit b3c298a

Please sign in to comment.