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

Commit

Permalink
Merge pull request #97 from golemcloud/wit-value-intovalue
Browse files Browse the repository at this point in the history
IntoValue instance for WitValue
  • Loading branch information
vigoo authored Nov 30, 2024
2 parents 06e127a + cb1c754 commit d926c16
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
2 changes: 1 addition & 1 deletion wasm-rpc-stubgen/src/stub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ impl StubDefinition {
fn partition_world_items<'a>(
&'a self,
world_items: &'a IndexMap<WorldKey, WorldItem>,
) -> WorldItemsByType {
) -> WorldItemsByType<'a> {
let mut types = Vec::<(String, TypeId)>::new();
let mut functions = Vec::new();
let mut interfaces = Vec::new();
Expand Down
2 changes: 1 addition & 1 deletion wasm-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub use type_annotated_value::*;
pub use text::{type_annotated_value_from_str, type_annotated_value_to_string};

#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for Uri {
impl arbitrary::Arbitrary<'_> for Uri {
fn arbitrary(u: &mut arbitrary::Unstructured) -> arbitrary::Result<Self> {
let uri = u.arbitrary::<String>()?;
Ok(Uri { value: uri })
Expand Down
60 changes: 56 additions & 4 deletions wasm-rpc/src/type_annotated_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use crate::protobuf::{TypeAnnotatedValue as RootTypeAnnotatedValue, TypedResult}
use crate::protobuf::{
TypedEnum, TypedFlags, TypedHandle, TypedList, TypedRecord, TypedTuple, TypedVariant,
};
use crate::{Value, WitValue};
use crate::{NodeIndex, Uri, Value, WitValue};
use golem_wasm_ast::analysis::analysed_type::{
bool, chr, f32, f64, list, option, result, result_err, result_ok, s16, s32, s64, s8, str,
tuple, u16, u32, u64, u8,
bool, case, chr, f32, f64, field, list, option, record, result, result_err, result_ok, s16,
s32, s64, s8, str, tuple, u16, u32, u64, u8, variant,
};
use golem_wasm_ast::analysis::protobuf::Type;
use golem_wasm_ast::analysis::AnalysedType;
Expand Down Expand Up @@ -95,7 +95,7 @@ impl TryFrom<ValueAndType> for crate::protobuf::TypeAnnotatedValue {
}
}

/// Specific trait to convert a type into a `ValueAndType` type.
/// Specific trait to convert a type into a pair of `Value` and `AnalysedType`.
pub trait IntoValue {
fn into_value(self) -> Value;
fn get_type() -> AnalysedType;
Expand Down Expand Up @@ -351,6 +351,58 @@ impl IntoValue for Uuid {
}
}

impl IntoValue for WitValue {
fn into_value(self) -> Value {
self.into()
}

fn get_type() -> AnalysedType {
record(vec![field(
"nodes",
list(variant(vec![
case("record-value", list(NodeIndex::get_type())),
case(
"variant-value",
tuple(vec![u32(), option(NodeIndex::get_type())]),
),
case("enum-value", u32()),
case("flags-value", list(bool())),
case("tuple-value", list(NodeIndex::get_type())),
case("list-value", list(NodeIndex::get_type())),
case("option-value", option(NodeIndex::get_type())),
case(
"result-value",
result(option(NodeIndex::get_type()), option(NodeIndex::get_type())),
),
case("prim-u8", u8()),
case("prim-u16", u16()),
case("prim-u32", u32()),
case("prim-u64", u64()),
case("prim-s8", s8()),
case("prim-s16", s16()),
case("prim-s32", s32()),
case("prim-s64", s64()),
case("prim-float32", f32()),
case("prim-float64", f64()),
case("prim-char", chr()),
case("prim-bool", bool()),
case("prim-string", str()),
case("handle", tuple(vec![Uri::get_type(), u64()])),
])),
)])
}
}

impl IntoValue for Uri {
fn into_value(self) -> Value {
Value::Record(vec![Value::String(self.value)])
}

fn get_type() -> AnalysedType {
record(vec![field("value", str())])
}
}

pub trait TypeAnnotatedValueConstructors: Sized {
fn create<T: Into<Type>>(value: &Value, typ: T) -> Result<Self, Vec<String>>;
}
Expand Down

0 comments on commit d926c16

Please sign in to comment.