Skip to content

Commit

Permalink
Adapt changes to no_std
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonIT committed Mar 7, 2024
1 parent 5720e72 commit 3982e63
Show file tree
Hide file tree
Showing 15 changed files with 24 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ default = ["which-rustfmt"]
which-rustfmt = ["which"]

[dependencies]
cbor_event = { git = "https://github.com/SimonIT/cbor_event", branch = "no_std" }
cbor_event = { git = "https://github.com/primetype/cbor_event" }
cddl = "0.9.1"
clap = { version = "4.3.12", features = ["derive"] }
codegen = { git = "https://github.com/dcSpark/codegen", branch = "master" }
Expand Down
21 changes: 9 additions & 12 deletions src/generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,9 @@ impl GenerationScope {
.raw("extern crate alloc;")
.raw("#[cfg(test)]\nextern crate std;")
.push_import("alloc::borrow", "ToOwned", None)
.push_import("alloc::boxed", "Box", None)
.push_import("alloc::string", "String", None)
.push_import("alloc::string", "ToString", None)
.push_import("alloc", "vec", None)
.push_import("alloc::vec", "Vec", None);
if cli.preserve_encodings {
Expand Down Expand Up @@ -2398,9 +2400,6 @@ impl GenerationScope {
// // TODO: potentially simplified deserialization some day
// // issue: https://github.com/dcSpark/cddl-codegen/issues/145
// } else {
deser_code.content.line(
"let initial_position = raw.as_mut_ref().stream_position().unwrap();",
);
let mut variant_final_exprs = config.final_exprs.clone();
if cli.preserve_encodings {
for enc_var in encoding_fields(
Expand Down Expand Up @@ -4750,7 +4749,7 @@ fn generate_array_struct_deserialization(
// There's no nice way to access this as Deserializer::special_break() consumes
// the byte so we'll just inline this ugly code instead
if field_cbor_types.contains(&cbor_event::Type::Special) {
"if raw.as_mut_ref().fill_buf().ok().and_then(|buf| buf.get(0)).map(|byte: &u8| cbor_event::Type::from(*byte) == cbor_event::Type::Special && (*byte & 0b0001_1111) != 0x1f).unwrap_or(false)".to_owned()
"if raw.as_ref().get(0).map(|byte: &u8| cbor_event::Type::from(*byte) == cbor_event::Type::Special && (*byte & 0b0001_1111) != 0x1f).unwrap_or(false)".to_owned()
} else {
format!("if raw.cbor_type().map(|ty| ty == {type_str}).unwrap_or(false)")
}
Expand Down Expand Up @@ -6606,9 +6605,7 @@ fn generate_enum(
}
};
if non_overlapping_types_match.is_none() {
deser_body
.line("let initial_position = raw.as_mut_ref().stream_position().unwrap();")
.line("let mut errs = Vec::new();");
deser_body.line("let mut errs = Vec::new();");
}
for variant in variants.iter() {
let enum_gen_info = EnumVariantInRust::new(types, variant, rep, cli);
Expand Down Expand Up @@ -7653,16 +7650,16 @@ fn add_struct_derives<T: DataType>(
cli: &Cli,
) {
data_type.derive("Clone").derive("Debug");
let mut std_derives = vec![];
let mut std_predicates = vec![];
if !custom_json {
if cli.json_serde_derives {
data_type
.derive("serde::Deserialize")
.derive("serde::Serialize");
}
let mut std_derives = vec![];
let mut std_predicates = vec![];
if cli.json_schema_export {
std_derives.push("schemars::JsonSchema".to_string());
if cli.json_schema_export {
std_derives.push("schemars::JsonSchema".to_string());
}
}
if used_in_key {
Expand Down Expand Up @@ -7733,7 +7730,7 @@ fn generate_int(gen_scope: &mut GenerationScope, types: &IntermediateTypes, cli:
.arg("string", "&str")
.ret("Result<Int, JsError>")
.line("// have to redefine so it's visible in WASM")
.line("std::str::FromStr::from_str(string).map(Self).map_err(|e| JsError::new(&format!(\"Int.from_str({}): {:?}\", string, e)))");
.line("alloc::str::FromStr::from_str(string).map(Self).map_err(|e| JsError::new(&format!(\"Int.from_str({}): {:?}\", string, e)))");

wrapper
.s_impl
Expand Down
2 changes: 1 addition & 1 deletion static/Cargo_rust.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ edition = "2018"
std = []

[dependencies]
cbor_event = { git = "https://github.com/SimonIT/cbor_event", branch = "no_std" }
cbor_event = { git = "https://github.com/primetype/cbor_event" }
4 changes: 2 additions & 2 deletions static/Cargo_wasm.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
cddl-lib = { path = "../rust" }
cbor_event = { git = "https://github.com/SimonIT/cbor_event", branch = "no_std" }
wasm-bindgen = { version = "0.2", features=["serde-serialize"] }
cbor_event = { git = "https://github.com/primetype/cbor_event" }
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
linked-hash-map = "0.5.3"
1 change: 1 addition & 0 deletions static/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use alloc::boxed::Box;
use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;
use cbor_event::{self, de::Deserializer};

#[derive(Debug)]
Expand Down
2 changes: 1 addition & 1 deletion tests/canonical/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod tests {
use super::*;
use cbor_event::{de::Deserializer, StringLenSz, Sz};
use serialization::Deserialize;
use std::{print, println};
use std::{format, print, println};

fn print_cbor_types(obj_name: &str, vec: Vec<u8>) {
use cbor_event::Type;
Expand Down
1 change: 1 addition & 0 deletions tests/comment-dsl/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
mod tests {
use super::*;
use alloc::string::ToString;
use std::format;

#[test]
fn group_type() {
Expand Down
2 changes: 1 addition & 1 deletion tests/core/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod tests {
use super::*;
use cbor_event::de::Deserializer;
use serialization::Deserialize;
use std::{print, println};
use std::{format, print, println};

fn print_cbor_types(obj_name: &str, vec: Vec<u8>) {
use cbor_event::Type;
Expand Down
2 changes: 1 addition & 1 deletion tests/external_json_impls
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl<'de> serde::de::Deserialize<'de> for CustomWrapper {
where
D: serde::de::Deserializer<'de>,
{
use std::str::FromStr;
use alloc::str::FromStr;
let s = <String as serde::de::Deserialize>::deserialize(deserializer)?;
u64::from_str(&s)
.map(CustomWrapper::new)
Expand Down
1 change: 1 addition & 0 deletions tests/json/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod tests {
use super::*;
use cbor_event::de::Deserializer;
use serialization::Deserialize;
use std::format;

#[test]
fn bytes_wrapper() {
Expand Down
2 changes: 1 addition & 1 deletion tests/preserve-encodings/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod tests {
use super::*;
use cbor_event::{de::Deserializer, StringLenSz, Sz};
use serialization::Deserialize;
use std::{print, println};
use std::{format, print, println};

fn print_cbor_types(obj_name: &str, vec: Vec<u8>) {
use cbor_event::Type;
Expand Down
1 change: 1 addition & 0 deletions tests/raw-bytes-preserve/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod tests {
use super::*;
use cbor_event::{Sz, StringLenSz, de::Deserializer};
use serialization::Deserialize;
use std::format;

#[test]
fn foo() {
Expand Down
2 changes: 1 addition & 1 deletion tests/raw-bytes/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod tests {
use super::*;
use cbor_event::de::Deserializer;
use serialization::Deserialize;
use std::{print, println};
use std::{format, print, println};

fn print_cbor_types(obj_name: &str, vec: Vec<u8>) {
use cbor_event::Type;
Expand Down
2 changes: 1 addition & 1 deletion tests/rust-wasm-split/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod tests {
use super::*;
use cbor_event::de::Deserializer;
use serialization::Deserialize;
use std::{print, println};
use std::{format, print, println};

fn print_cbor_types(obj_name: &str, vec: Vec<u8>) {
use cbor_event::Type;
Expand Down

0 comments on commit 3982e63

Please sign in to comment.