Skip to content

Commit

Permalink
add serialization support for wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
anweiss committed Sep 17, 2019
1 parent 9dd7122 commit 8856c62
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use super::token::{ByteSliceValue, ByteVecValue, RangeValue, SocketPlug, Value};
use std::fmt;

#[cfg(target_arch = "wasm32")]
use serde::Serialize;

#[cfg(feature = "std")]
use std::borrow::Cow;

Expand All @@ -23,6 +26,7 @@ pub trait Node {
/// ```abnf
/// cddl = S 1*(rule S)
/// ```
#[cfg_attr(target_arch = "wasm32", derive(Serialize))]
#[derive(Default, Debug)]
pub struct CDDL<'a> {
/// Zero or more production rules
Expand Down Expand Up @@ -59,6 +63,7 @@ impl<'a> Node for CDDL<'a> {
/// EALPHA = ALPHA / "@" / "_" / "$"
/// DIGIT = %x30-39
/// ```
#[cfg_attr(target_arch = "wasm32", derive(Serialize))]
#[derive(Debug, PartialEq, Clone)]
pub struct Identifier<'a>(pub (&'a str, Option<&'a SocketPlug>));

Expand Down Expand Up @@ -97,6 +102,7 @@ impl<'a> From<&'static str> for Identifier<'a> {
/// rule = typename [genericparm] S assignt S type
/// / groupname [genericparm] S assigng S grpent
/// ```
#[cfg_attr(target_arch = "wasm32", derive(Serialize))]
#[derive(Debug)]
pub enum Rule<'a> {
/// Type expression
Expand Down Expand Up @@ -155,6 +161,7 @@ impl<'a> Rule<'a> {
/// ```abnf
/// typename [genericparm] S assignt S type
/// ```
#[cfg_attr(target_arch = "wasm32", derive(Serialize))]
#[derive(Debug)]
pub struct TypeRule<'a> {
/// Type name identifier
Expand Down Expand Up @@ -200,6 +207,7 @@ impl<'a> Node for TypeRule<'a> {
/// ```abnf
/// groupname [genericparm] S assigng S grpent
/// ```
#[cfg_attr(target_arch = "wasm32", derive(Serialize))]
#[derive(Debug)]
pub struct GroupRule<'a> {
/// Group name identifier
Expand Down Expand Up @@ -245,6 +253,7 @@ impl<'a> Node for GroupRule<'a> {
/// ```abnf
/// genericparm = "<" S id S *("," S id S ) ">"
/// ```
#[cfg_attr(target_arch = "wasm32", derive(Serialize))]
#[derive(Default, Debug)]
pub struct GenericParm<'a>(pub Vec<Identifier<'a>>);

Expand All @@ -270,6 +279,7 @@ impl<'a> fmt::Display for GenericParm<'a> {
/// ```abnf
/// genericarg = "<" S type1 S *("," S type1 S ) ">"
/// ```
#[cfg_attr(target_arch = "wasm32", derive(Serialize))]
#[derive(Debug, Clone)]
pub struct GenericArg<'a>(pub Vec<Type1<'a>>);

Expand All @@ -295,6 +305,7 @@ impl<'a> fmt::Display for GenericArg<'a> {
/// ```abnf
/// type = type1 *(S "/" S type1)
/// ```
#[cfg_attr(target_arch = "wasm32", derive(Serialize))]
#[derive(Debug, Clone)]
pub struct Type<'a>(pub Vec<Type1<'a>>);

Expand All @@ -320,6 +331,7 @@ impl<'a> fmt::Display for Type<'a> {
/// ```abnf
/// type1 = type2 [S (rangeop / ctlop) S type2]
/// ```
#[cfg_attr(target_arch = "wasm32", derive(Serialize))]
#[derive(Debug, Clone)]
pub struct Type1<'a> {
/// Type
Expand Down Expand Up @@ -360,6 +372,7 @@ impl<'a> fmt::Display for Type1<'a> {
/// rangeop = "..." / ".."
/// ctlop = "." id
/// ```
#[cfg_attr(target_arch = "wasm32", derive(Serialize))]
#[derive(Debug, PartialEq, Clone)]
pub enum RangeCtlOp {
/// Range operator where value is `true` if inclusive
Expand Down Expand Up @@ -393,6 +406,7 @@ impl<'a> fmt::Display for RangeCtlOp {
/// / "#" DIGIT ["." uint] ; major/ai
/// / "#" ; any
/// ```
#[cfg_attr(target_arch = "wasm32", derive(Serialize))]
#[derive(Debug, Clone)]
pub enum Type2<'a> {
/// Integer value
Expand Down Expand Up @@ -539,6 +553,7 @@ impl<'a> From<ByteVecValue> for Type2<'a> {
/// ```abnf
/// group = grpchoice * (S "//" S grpchoice)
/// ```
#[cfg_attr(target_arch = "wasm32", derive(Serialize))]
#[derive(Debug, Clone)]
pub struct Group<'a>(pub Vec<GroupChoice<'a>>);

Expand Down Expand Up @@ -566,6 +581,7 @@ impl<'a> fmt::Display for Group<'a> {
/// ```
///
/// If tuple is true, then entry is marked by a trailing comma
#[cfg_attr(target_arch = "wasm32", derive(Serialize))]
#[derive(Debug, Clone)]
pub struct GroupChoice<'a>(pub Vec<(GroupEntry<'a>, bool)>);

Expand Down Expand Up @@ -596,6 +612,7 @@ impl<'a> fmt::Display for GroupChoice<'a> {
/// / [occur S] groupname [genericarg] ; preempted by above
/// / [occur S] "(" S group S ")"
/// ```
#[cfg_attr(target_arch = "wasm32", derive(Serialize))]
#[derive(Debug, Clone)]
pub enum GroupEntry<'a> {
/// Value group entry type
Expand Down Expand Up @@ -628,6 +645,7 @@ impl<'a> fmt::Display for GroupEntry<'a> {
/// ```abnf
/// [occur S] [memberkey S] type
/// ```
#[cfg_attr(target_arch = "wasm32", derive(Serialize))]
#[derive(Debug, Clone)]
pub struct ValueMemberKeyEntry<'a> {
/// Optional occurrence indicator
Expand Down Expand Up @@ -657,6 +675,7 @@ impl<'a> fmt::Display for ValueMemberKeyEntry<'a> {
}

/// Group entry from a named type or group
#[cfg_attr(target_arch = "wasm32", derive(Serialize))]
#[derive(Debug, Clone)]
pub struct TypeGroupnameEntry<'a> {
/// Optional occurrence indicator
Expand Down Expand Up @@ -691,6 +710,7 @@ impl<'a> fmt::Display for TypeGroupnameEntry<'a> {
/// / bareword S ":"
/// / value S ":"
/// ```
#[cfg_attr(target_arch = "wasm32", derive(Serialize))]
#[derive(Debug, Clone)]
pub enum MemberKey<'a> {
/// Type expression. If second value in tuple is `true`, a cut is present
Expand Down Expand Up @@ -723,6 +743,7 @@ impl<'a> fmt::Display for MemberKey<'a> {
/// / "+"
/// / "?"
/// ```
#[cfg_attr(target_arch = "wasm32", derive(Serialize))]
#[derive(Debug, Clone)]
pub enum Occur {
/// Occurrence indicator in the form n*m, where n is an optional lower limit
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ pub mod repl;
pub mod token;
/// Validation against various data structures (e.g. JSON, CBOR)
#[cfg(feature = "std")]
#[cfg(not(target_arch = "wasm32"))]
pub mod validation;

#[doc(inline)]
Expand All @@ -293,6 +294,7 @@ pub use self::{

#[doc(inline)]
#[cfg(feature = "std")]
#[cfg(not(target_arch = "wasm32"))]
pub use self::validation::{
cbor::{self as cbor_validator, validate_cbor_from_slice},
json::{self as json_validator, validate_json_from_str},
Expand Down
14 changes: 14 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -959,10 +959,24 @@ impl<'a> Parser<'a> {
}

/// Returns a `ast::CDDL` from a `&str`
#[cfg(not(target_arch = "wasm32"))]
pub fn cddl_from_str<'a>(input: &'a str) -> Result<CDDL<'a>> {
Parser::new(Lexer::new(input))?.parse_cddl()
}

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn cddl_from_str(input: &str) -> result::Result<JsValue, JsValue> {
let c = Parser::new(Lexer::new(input))
.map_err(|e| JsValue::from(e.to_string()))?
.parse_cddl()
.map_err(|e| JsValue::from(e.to_string()))?;

JsValue::from_serde(&c)
.map_err(|e| JsValue::from(e.to_string()))
.map(|c| c)
}

/// Validates CDDL input against RFC 8610
#[cfg(not(target_arch = "wasm32"))]
pub fn compile_cddl_from_str(input: &str) -> Result<()> {
Expand Down
5 changes: 5 additions & 0 deletions src/token.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::{convert::TryFrom, fmt};

#[cfg(target_arch = "wasm32")]
use serde::Serialize;

#[cfg(not(feature = "std"))]
use alloc::{string::String, vec::Vec};

Expand Down Expand Up @@ -283,6 +286,7 @@ impl<'a> fmt::Display for RangeValue<'a> {
}
}

#[cfg_attr(target_arch = "wasm32", derive(Serialize))]
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum Value<'a> {
// TODO: support hexfloat and exponent
Expand Down Expand Up @@ -372,6 +376,7 @@ impl fmt::Display for ByteVecValue {
}
}

#[cfg_attr(target_arch = "wasm32", derive(Serialize))]
#[derive(Debug, PartialEq)]
pub enum SocketPlug {
TYPE,
Expand Down

0 comments on commit 8856c62

Please sign in to comment.