Skip to content

Commit

Permalink
option, primitives, struct with enum field
Browse files Browse the repository at this point in the history
  • Loading branch information
StuartHarris committed Nov 21, 2024
1 parent c548406 commit 23ad27a
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 25 deletions.
52 changes: 49 additions & 3 deletions crux_cli/src/codegen/generator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use rustdoc_types::{Type, Variant};
use rustdoc_types::{GenericArg, GenericArgs, Type, Variant};

use crate::codegen::format::{ContainerFormat, Named};

Expand Down Expand Up @@ -86,12 +86,58 @@ pub(crate) fn generate(edges: &[(Node, Node)]) {
impl From<&Type> for Format {
fn from(value: &Type) -> Self {
match value {
Type::ResolvedPath(path) => Format::TypeName(path.name.clone()),
Type::ResolvedPath(path) => {
if let Some(args) = &path.args {
match args.as_ref() {
GenericArgs::AngleBracketed {
args,
constraints: _,
} => {
if path.name == "Option" {
let format = match args[0] {
GenericArg::Type(ref t) => t.into(),
_ => todo!(),
};
Format::Option(Box::new(format))
} else {
Format::TypeName(path.name.clone())
}
}
GenericArgs::Parenthesized {
inputs: _,
output: _,
} => todo!(),
}
} else {
Format::TypeName(path.name.clone())
}
}
Type::DynTrait(_dyn_trait) => todo!(),
Type::Generic(_) => todo!(),
Type::Primitive(s) => match s.as_ref() {
"bool" => Format::Bool,
"char" => Format::Char,
"isize" => match std::mem::size_of::<isize>() {
4 => Format::I32,
8 => Format::I64,
_ => panic!("unsupported isize size"),
},
"i8" => Format::I8,
"i16" => Format::I16,
"i32" => Format::I32,
"i64" => Format::I64,
"i128" => Format::I128,
"usize" => match std::mem::size_of::<usize>() {
4 => Format::U32,
8 => Format::U64,
_ => panic!("unsupported usize size"),
},
"u8" => Format::U8,
"u16" => Format::U16,
"u32" => Format::U32,
_ => todo!(),
"u64" => Format::U64,
"u128" => Format::U128,
s => panic!("need to implement primitive {s}"),
},
Type::FunctionPointer(_function_pointer) => todo!(),
Type::Tuple(_vec) => todo!(),
Expand Down
4 changes: 2 additions & 2 deletions crux_cli/src/codegen/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ ascent! {

field(struct_, field) <--
root(impl_, struct_),
edge(struct_, field, ?Edge::HasField|Edge::Unit);
edge(struct_, field, ?Edge::HasVariant|Edge::HasField|Edge::Unit);
field(struct2, field2) <--
field(struct1, field1),
edge(field1, struct2, Edge::ForType),
edge(struct2, field2, ?Edge::HasField|Edge::Unit);
edge(struct2, field2, ?Edge::HasVariant|Edge::HasField|Edge::Unit);

variant(enum_, variant) <--
root(impl_, enum_),
Expand Down
7 changes: 3 additions & 4 deletions examples/bridge_echo/shared/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub mod app;

use lazy_static::lazy_static;
use wasm_bindgen::prelude::wasm_bindgen;

pub use crux_core::{bridge::Bridge, Core, Request};

Expand All @@ -15,17 +14,17 @@ lazy_static! {
static ref CORE: Bridge<Effect, App> = Bridge::new(Core::new());
}

#[wasm_bindgen]
#[cfg_attr(target_family = "wasm", wasm_bindgen::prelude::wasm_bindgen)]
pub fn process_event(data: &[u8]) -> Vec<u8> {
CORE.process_event(data)
}

#[wasm_bindgen]
#[cfg_attr(target_family = "wasm", wasm_bindgen::prelude::wasm_bindgen)]
pub fn handle_response(id: u32, data: &[u8]) -> Vec<u8> {
CORE.handle_response(id, data)
}

#[wasm_bindgen]
#[cfg_attr(target_family = "wasm", wasm_bindgen::prelude::wasm_bindgen)]
pub fn view() -> Vec<u8> {
CORE.view()
}
7 changes: 3 additions & 4 deletions examples/cat_facts/shared/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub mod app;

use lazy_static::lazy_static;
use wasm_bindgen::prelude::wasm_bindgen;

use crux_core::bridge::Bridge;
pub use crux_core::{Core, Request};
Expand All @@ -20,17 +19,17 @@ lazy_static! {
static ref CORE: Bridge<Effect, CatFacts> = Bridge::new(Core::new());
}

#[wasm_bindgen]
#[cfg_attr(target_family = "wasm", wasm_bindgen::prelude::wasm_bindgen)]
pub fn process_event(data: &[u8]) -> Vec<u8> {
CORE.process_event(data)
}

#[wasm_bindgen]
#[cfg_attr(target_family = "wasm", wasm_bindgen::prelude::wasm_bindgen)]
pub fn handle_response(id: u32, data: &[u8]) -> Vec<u8> {
CORE.handle_response(id, data)
}

#[wasm_bindgen]
#[cfg_attr(target_family = "wasm", wasm_bindgen::prelude::wasm_bindgen)]
pub fn view() -> Vec<u8> {
CORE.view()
}
7 changes: 3 additions & 4 deletions examples/counter/shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pub mod app;
pub mod capabilities;

use lazy_static::lazy_static;
use wasm_bindgen::prelude::wasm_bindgen;

pub use crux_core::bridge::{Bridge, Request};
pub use crux_core::Core;
Expand All @@ -19,17 +18,17 @@ lazy_static! {
static ref CORE: Bridge<Effect, App> = Bridge::new(Core::new());
}

#[wasm_bindgen]
#[cfg_attr(target_family = "wasm", wasm_bindgen::prelude::wasm_bindgen)]
pub fn process_event(data: &[u8]) -> Vec<u8> {
CORE.process_event(data)
}

#[wasm_bindgen]
#[cfg_attr(target_family = "wasm", wasm_bindgen::prelude::wasm_bindgen)]
pub fn handle_response(id: u32, data: &[u8]) -> Vec<u8> {
CORE.handle_response(id, data)
}

#[wasm_bindgen]
#[cfg_attr(target_family = "wasm", wasm_bindgen::prelude::wasm_bindgen)]
pub fn view() -> Vec<u8> {
CORE.view()
}
7 changes: 3 additions & 4 deletions examples/hello_world/shared/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub mod app; // not exposed so you can remove this in your project

use lazy_static::lazy_static;
use wasm_bindgen::prelude::wasm_bindgen;

pub use crux_core::{bridge::Bridge, Core, Request};

Expand All @@ -15,17 +14,17 @@ lazy_static! {
static ref CORE: Bridge<Effect, Hello> = Bridge::new(Core::new());
}

#[wasm_bindgen]
#[cfg_attr(target_family = "wasm", wasm_bindgen::prelude::wasm_bindgen)]
pub fn process_event(data: &[u8]) -> Vec<u8> {
CORE.process_event(data)
}

#[wasm_bindgen]
#[cfg_attr(target_family = "wasm", wasm_bindgen::prelude::wasm_bindgen)]
pub fn handle_response(id: u32, data: &[u8]) -> Vec<u8> {
CORE.handle_response(id, data)
}

#[wasm_bindgen]
#[cfg_attr(target_family = "wasm", wasm_bindgen::prelude::wasm_bindgen)]
pub fn view() -> Vec<u8> {
CORE.view()
}
7 changes: 3 additions & 4 deletions examples/notes/shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pub mod app;
pub mod capabilities;

use lazy_static::lazy_static;
use wasm_bindgen::prelude::wasm_bindgen;

use crux_core::bridge::Bridge;
pub use crux_core::{bridge::Request, Core};
Expand All @@ -15,17 +14,17 @@ lazy_static! {
static ref CORE: Bridge<Effect, NoteEditor> = Bridge::new(Core::new());
}

#[wasm_bindgen]
#[cfg_attr(target_family = "wasm", wasm_bindgen::prelude::wasm_bindgen)]
pub fn process_event(data: &[u8]) -> Vec<u8> {
CORE.process_event(data)
}

#[wasm_bindgen]
#[cfg_attr(target_family = "wasm", wasm_bindgen::prelude::wasm_bindgen)]
pub fn handle_response(id: u32, data: &[u8]) -> Vec<u8> {
CORE.handle_response(id, data)
}

#[wasm_bindgen]
#[cfg_attr(target_family = "wasm", wasm_bindgen::prelude::wasm_bindgen)]
pub fn view() -> Vec<u8> {
CORE.view()
}

0 comments on commit 23ad27a

Please sign in to comment.