diff --git a/components/extract-wit/src/lib.rs b/components/extract-wit/src/lib.rs index 15067a2..a6724a5 100644 --- a/components/extract-wit/src/lib.rs +++ b/components/extract-wit/src/lib.rs @@ -5,81 +5,76 @@ use std::collections::BTreeMap; use crate::{ componentized::component::types::{Component, Error}, exports::componentized::component::wit::{ - Docs, Enum, EnumCase, Flag, Flags, Function, FunctionKind, Guest, GuestWit, Handle, - IncludeName, Interface, InterfaceId, List, Map, Package, PackageId, PackageName, Param, - Record, RecordField, Result as Result_, Stability, Stable, Tuple, Type, TypeDef, - TypeDefKind, TypeId, TypeOwner, Unstable, Variant, VariantCase, Version, VersionIdentifier, - Wit, World, WorldId, WorldInclude, WorldItem, WorldItemInterface, WorldKey, + Docs, Enum, EnumCase, Flag, Flags, Function, FunctionKind, Guest, Handle, IncludeName, + Interface, InterfaceId, List, Map, Package, PackageId, PackageName, Param, Record, + RecordField, Result as Result_, Stability, Stable, Tuple, Type, TypeDef, TypeDefKind, + TypeId, TypeOwner, Unstable, Variant, VariantCase, Version, VersionIdentifier, Wit, World, + WorldId, WorldInclude, WorldItem, WorldItemInterface, WorldKey, }, }; pub(crate) struct ExtractWit; impl Guest for ExtractWit { - type Wit = ExtractedWit; - #[allow(async_fn_in_trait)] - async fn extract(component: &Component) -> Result<(Wit, Package), Error> { - let wasm = component.into_wasm(); - let decoded = wit_component::decode(&wasm)?; - - let wit = ExtractedWit::new(decoded.resolve()); - let package = wit - .package(ExtractedWit::package_id(decoded.package())) - .expect("decoded package must exist"); + async fn extract(component: Component) -> Result { + let decoded = wit_component::decode(&component)?; - Ok((Wit::new(wit), package)) + Wit::new(decoded.resolve(), decoded.package()) } } -pub(crate) struct ExtractedWit { - worlds: BTreeMap, - interfaces: BTreeMap, - types: BTreeMap, - packages: BTreeMap, -} - -impl ExtractedWit { - fn new(resolve: &wit_parser::Resolve) -> Self { - Self { +impl Wit { + fn new( + resolve: &wit_parser::Resolve, + package_id: wit_parser::PackageId, + ) -> Result { + let wit = Self { worlds: resolve.worlds.clone().into_iter().fold( BTreeMap::new(), |mut worlds, (id, world)| { - worlds.insert(Self::world_id(id).world_id, Self::world(world)); + worlds.insert(Self::world_id(id), Self::world(world)); worlds }, ), interfaces: resolve.interfaces.clone().into_iter().fold( BTreeMap::new(), |mut interfaces, (id, interface)| { - interfaces.insert( - Self::interface_id(id).interface_id, - Self::interface(interface), - ); + interfaces.insert(Self::interface_id(id), Self::interface(interface)); interfaces }, ), types: resolve.types.clone().into_iter().fold( BTreeMap::new(), |mut types, (id, type_def)| { - types.insert(Self::type_id(id).type_id, Self::type_def(type_def)); + types.insert(Self::type_id(id), Self::type_def(type_def)); types }, ), packages: resolve.packages.clone().into_iter().fold( BTreeMap::new(), |mut packages, (id, package)| { - packages.insert(Self::package_id(id).package_id, Self::package(package)); + packages.insert(Self::package_id(id), Self::package(package)); packages }, ), + + default_package: Some(Self::package_id(package_id)), + }; + + if wit + .packages + .get(&wit.default_package.clone().unwrap()) + .is_none() + { + Err(Error::Other(Some("decoded package must exist".to_string())))?; } + + Ok(wit) } fn world_id(id: wit_parser::WorldId) -> WorldId { - WorldId { - world_id: u32::try_from(id.index()).expect("id too large"), - } + WorldId::from(format!("world:{}", id.index())) } fn world(world: wit_parser::World) -> World { @@ -147,9 +142,7 @@ impl ExtractedWit { } fn interface_id(id: wit_parser::InterfaceId) -> InterfaceId { - InterfaceId { - interface_id: u32::try_from(id.index()).expect("id too large"), - } + InterfaceId::from(format!("interface:{}", id.index())) } fn interface(interface: wit_parser::Interface) -> Interface { @@ -205,9 +198,7 @@ impl ExtractedWit { } fn type_id(id: wit_parser::TypeId) -> TypeId { - TypeId { - type_id: u32::try_from(id.index()).expect("id too large"), - } + TypeId::from(format!("type:{}", id.index())) } fn type_(type_: wit_parser::Type) -> Type { @@ -369,9 +360,7 @@ impl ExtractedWit { } fn package_id(id: wit_parser::PackageId) -> PackageId { - PackageId { - package_id: u32::try_from(id.index()).expect("id too large"), - } + PackageId::from(format!("package:{}", id.index())) } fn package(package: wit_parser::Package) -> Package { @@ -444,28 +433,6 @@ impl ExtractedWit { } } -impl GuestWit for ExtractedWit { - #[allow(async_fn_in_trait)] - fn world(&self, id: WorldId) -> Option { - self.worlds.get(&id.world_id).cloned() - } - - #[allow(async_fn_in_trait)] - fn interface(&self, id: InterfaceId) -> Option { - self.interfaces.get(&id.interface_id).cloned() - } - - #[allow(async_fn_in_trait)] - fn type_(&self, id: TypeId) -> Option { - self.types.get(&id.type_id).cloned() - } - - #[allow(async_fn_in_trait)] - fn package(&self, id: PackageId) -> Option { - self.packages.get(&id.package_id).cloned() - } -} - impl From for Error { fn from(value: anyhow::Error) -> Self { Self::Other(Some(value.to_string())) diff --git a/components/wac-loader/src/lib.rs b/components/wac-loader/src/lib.rs index 8d24a46..9563a53 100644 --- a/components/wac-loader/src/lib.rs +++ b/components/wac-loader/src/lib.rs @@ -3,57 +3,31 @@ use wac_graph::{types::Package, CompositionGraph, EncodeOptions}; use crate::exports::componentized::component::{ - types::{Component, ComponentBorrow, Error, Guest as TypesGuest, GuestComponent}, + types::{Component, Error}, wac_loader::Guest, }; pub(crate) struct WacLoader; -impl TypesGuest for WacLoader { - type Component = WacComponent; -} - impl Guest for WacLoader { #[allow(async_fn_in_trait)] - async fn plug( - socket: ComponentBorrow<'_>, - plugs: Vec>, - ) -> Result { + async fn plug(socket: Component, plugs: Vec) -> Result { let mut graph = CompositionGraph::new(); - let socket: &WacComponent = socket.get(); - let socket = Package::from_bytes("socket", None, socket.into_wasm(), graph.types_mut())?; + let socket = Package::from_bytes("socket", None, socket, graph.types_mut())?; let socket = graph.register_package(socket)?; let mut graph_plugs = Vec::new(); for plug in plugs { - let plug: &WacComponent = plug.get(); - let plug = Package::from_bytes("plug", None, plug.into_wasm(), graph.types_mut())?; + let plug = Package::from_bytes("plug", None, plug, graph.types_mut())?; let plug = graph.register_package(plug)?; graph_plugs.push(plug); } wac_graph::plug(&mut graph, graph_plugs, socket)?; - let composed_wasm = graph.encode(EncodeOptions::default())?; - - Ok(Component::new(WacComponent::new(composed_wasm))) - } -} + let component = graph.encode(EncodeOptions::default())?; -pub(crate) struct WacComponent { - wasm: Vec, -} - -impl WacComponent { - fn new(wasm: Vec) -> Self { - Self { wasm } - } -} - -impl GuestComponent for WacComponent { - #[allow(async_fn_in_trait)] - fn into_wasm(&self) -> Vec { - self.wasm.clone() + Ok(component) } } diff --git a/components/wasm-loader/src/lib.rs b/components/wasm-loader/src/lib.rs index 720a3ba..b624a5c 100644 --- a/components/wasm-loader/src/lib.rs +++ b/components/wasm-loader/src/lib.rs @@ -1,41 +1,17 @@ #![no_main] use crate::{ - exports::componentized::component::types::{ - Component, Error, Guest as TypesGuest, GuestComponent, - }, + exports::componentized::component::types::{Component, Error}, exports::componentized::component::wasm_loader::Guest, }; use wit_bindgen::rt::async_support::StreamReader; pub(crate) struct WasmLoader; -impl TypesGuest for WasmLoader { - type Component = WasmComponent; -} - impl Guest for WasmLoader { #[allow(async_fn_in_trait)] async fn load(wasm: StreamReader) -> Result { - let wasm = wasm.collect().await; - Ok(Component::new(WasmComponent::new(wasm))) - } -} - -pub(crate) struct WasmComponent { - wasm: Vec, -} - -impl WasmComponent { - fn new(wasm: Vec) -> Self { - Self { wasm } - } -} - -impl GuestComponent for WasmComponent { - #[allow(async_fn_in_trait)] - fn into_wasm(&self) -> Vec { - self.wasm.clone() + Ok(wasm.collect().await) } } diff --git a/components/wit/deps/componentized-component-0.0.0-0/package.wit b/components/wit/deps/componentized-component-0.0.0-0/package.wit index 6661769..bd32726 100644 --- a/components/wit/deps/componentized-component-0.0.0-0/package.wit +++ b/components/wit/deps/componentized-component-0.0.0-0/package.wit @@ -5,9 +5,7 @@ interface types { other(option), } - resource component { - into-wasm: func() -> list; - } + type component = list; } interface wasm-loader { @@ -43,15 +41,13 @@ interface wasm-directory-loader { interface wac-loader { use types.{component, error}; - plug: async func(socket: borrow, plugs: list>) -> result; + plug: async func(socket: component, plugs: list) -> result; } interface wit { use types.{component, error}; - record type-id { - type-id: u32, - } + type type-id = string; variant %type { %bool, @@ -209,13 +205,9 @@ interface wit { external-id: option, } - record interface-id { - interface-id: u32, - } + type interface-id = string; - record world-id { - world-id: u32, - } + type world-id = string; variant type-owner { %world(world-id), @@ -261,9 +253,7 @@ interface wit { %interface(interface-id), } - record package-id { - package-id: u32, - } + type package-id = string; record %interface { name: option, @@ -297,14 +287,15 @@ interface wit { worlds: list>, } - resource wit { - %interface: func(id: interface-id) -> option<%interface>; - %package: func(id: package-id) -> option<%package>; - %type: func(id: type-id) -> option; - %world: func(id: world-id) -> option<%world>; + record wit { + interfaces: map, + packages: map, + types: map, + worlds: map, + default-package: option, } - extract: async func(component: borrow) -> result, error>; + extract: async func(component: component) -> result; } world imports { diff --git a/components/wit/worlds.wit b/components/wit/worlds.wit index 36d778e..7e17ec3 100644 --- a/components/wit/worlds.wit +++ b/components/wit/worlds.wit @@ -5,7 +5,6 @@ world extract-wit { } world wac-loader { - import componentized:component/types@0.0.0-0; export componentized:component/types@0.0.0-0; export componentized:component/wac-loader@0.0.0-0; } diff --git a/wit/loader.wit b/wit/loader.wit index 7f76c1b..0b5a96b 100644 --- a/wit/loader.wit +++ b/wit/loader.wit @@ -31,5 +31,5 @@ interface wasm-directory-loader { interface wac-loader { use types.{component, error}; - plug: async func(socket: borrow, plugs: list>) -> result; + plug: async func(socket: component, plugs: list) -> result; } \ No newline at end of file diff --git a/wit/types.wit b/wit/types.wit index 250cdef..f62c4ad 100644 --- a/wit/types.wit +++ b/wit/types.wit @@ -6,8 +6,6 @@ interface types { other(option), } - resource component { - into-wasm: func() -> list; - } + type component = list; } diff --git a/wit/wit.wit b/wit/wit.wit index a939a56..226ad1c 100644 --- a/wit/wit.wit +++ b/wit/wit.wit @@ -2,9 +2,7 @@ interface wit { use types.{component, error}; - record type-id { - type-id: u32, - } + type type-id = string; variant %type { %bool, @@ -177,9 +175,7 @@ interface wit { numeric(u64), } - record interface-id { - interface-id: u32, - } + type interface-id = string; record %interface { name: option, @@ -190,9 +186,7 @@ interface wit { %package: option, } - record world-id { - world-id: u32, - } + type world-id = string; record %world { name: string, @@ -233,9 +227,7 @@ interface wit { %interface(interface-id), } - record package-id { - package-id: u32, - } + type package-id = string; record %package { name: package-name, @@ -250,13 +242,14 @@ interface wit { version: option, } - resource wit { - %interface: func(id: interface-id) -> option<%interface>; - %package: func(id: package-id) -> option<%package>; - %type: func(id: type-id) -> option; - %world: func(id: world-id) -> option<%world>; - } + record wit { + interfaces: map, + packages: map, + types: map, + worlds: map, - extract: async func(component: borrow) -> result, error>; + default-package: option, + } + extract: async func(component: component) -> result; }