-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(parse_cbor): add parse_cbor function (#1152)
* Add cbor support * Move json_type_def to separate module --------- Co-authored-by: Semyon Uchvatov <[email protected]>
- Loading branch information
1 parent
719fb53
commit 7f010fc
Showing
13 changed files
with
194 additions
and
74 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,7 @@ chacha20poly1305,https://github.com/RustCrypto/AEADs/tree/master/chacha20poly130 | |
charset,https://github.com/hsivonen/charset,Apache-2.0 OR MIT,Henri Sivonen <[email protected]> | ||
chrono,https://github.com/chronotope/chrono,MIT OR Apache-2.0,The chrono Authors | ||
chrono-tz,https://github.com/chronotope/chrono-tz,MIT OR Apache-2.0,The chrono-tz Authors | ||
ciborium,https://github.com/enarx/ciborium,Apache-2.0,Nathaniel McCallum <[email protected]> | ||
cidr,https://github.com/stbuehler/rust-cidr,MIT,Stefan Bühler <[email protected]> | ||
cidr-utils,https://github.com/magiclen/cidr-utils,MIT,Magic Len <[email protected]> | ||
cipher,https://github.com/RustCrypto/traits,MIT OR Apache-2.0,RustCrypto Developers | ||
|
@@ -68,6 +69,7 @@ crc32fast,https://github.com/srijs/rust-crc32fast,MIT OR Apache-2.0,"Sam Rijs <s | |
crossbeam-channel,https://github.com/crossbeam-rs/crossbeam,MIT OR Apache-2.0,The crossbeam-channel Authors | ||
crossbeam-epoch,https://github.com/crossbeam-rs/crossbeam,MIT OR Apache-2.0,The crossbeam-epoch Authors | ||
crossbeam-utils,https://github.com/crossbeam-rs/crossbeam,MIT OR Apache-2.0,The crossbeam-utils Authors | ||
crunchy,https://github.com/eira-fransham/crunchy,MIT,Vurich <[email protected]> | ||
crypto-common,https://github.com/RustCrypto/traits,MIT OR Apache-2.0,RustCrypto Developers | ||
crypto_secretbox,https://github.com/RustCrypto/nacl-compat/tree/master/crypto_secretbox,Apache-2.0 OR MIT,RustCrypto Developers | ||
csv,https://github.com/BurntSushi/rust-csv,Unlicense OR MIT,Andrew Gallant <[email protected]> | ||
|
@@ -103,6 +105,7 @@ generic-array,https://github.com/fizyk20/generic-array,MIT,"Bartłomiej Kamińsk | |
getrandom,https://github.com/rust-random/getrandom,MIT OR Apache-2.0,The Rand Project Developers | ||
gimli,https://github.com/gimli-rs/gimli,MIT OR Apache-2.0,The gimli Authors | ||
grok,https://github.com/daschl/grok,Apache-2.0,Michael Nitschinger <[email protected]> | ||
half,https://github.com/starkat99/half-rs,MIT OR Apache-2.0,Kathryn Long <[email protected]> | ||
hashbrown,https://github.com/rust-lang/hashbrown,MIT OR Apache-2.0,Amanieu d'Antras <[email protected]> | ||
heck,https://github.com/withoutboats/heck,MIT OR Apache-2.0,The heck Authors | ||
heck,https://github.com/withoutboats/heck,MIT OR Apache-2.0,Without Boats <[email protected]> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add `parse_cbor` function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[overrides] | ||
"crunchy" = { origin = "https://github.com/eira-fransham/crunchy" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use crate::prelude::{Collection, TypeDef}; | ||
use crate::value::Kind; | ||
|
||
pub(crate) fn json_inner_kind() -> Kind { | ||
Kind::null() | ||
| Kind::bytes() | ||
| Kind::integer() | ||
| Kind::float() | ||
| Kind::boolean() | ||
| Kind::array(Collection::any()) | ||
| Kind::object(Collection::any()) | ||
} | ||
|
||
pub(crate) fn json_type_def() -> TypeDef { | ||
TypeDef::bytes() | ||
.fallible() | ||
.or_boolean() | ||
.or_integer() | ||
.or_float() | ||
.add_null() | ||
.or_null() | ||
.or_array(Collection::from_unknown(json_inner_kind())) | ||
.or_object(Collection::from_unknown(json_inner_kind())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub(crate) mod json_type_def; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
use crate::compiler::prelude::*; | ||
use crate::stdlib::json_utils::json_type_def::json_type_def; | ||
use ciborium::de::from_reader; | ||
use zstd::zstd_safe::WriteBuf; | ||
|
||
fn parse_cbor(value: Value) -> Resolved { | ||
let bytes = value.try_bytes()?; | ||
let value = from_reader(bytes.as_slice()).map_err(|e| format!("unable to parse cbor: {e}"))?; | ||
Ok(value) | ||
} | ||
|
||
#[derive(Clone, Copy, Debug)] | ||
pub struct ParseCbor; | ||
|
||
impl Function for ParseCbor { | ||
fn identifier(&self) -> &'static str { | ||
"parse_cbor" | ||
} | ||
|
||
fn summary(&self) -> &'static str { | ||
"parse a string to a CBOR type" | ||
} | ||
|
||
fn usage(&self) -> &'static str { | ||
indoc! {" | ||
Parses the provided `value` as CBOR. | ||
"} | ||
} | ||
|
||
fn examples(&self) -> &'static [Example] { | ||
&[ | ||
Example { | ||
title: "object", | ||
source: r#"parse_cbor!(decode_base64!("oWVmaWVsZGV2YWx1ZQ=="))"#, | ||
result: Ok(r#"{ "field": "value" }"#), | ||
}, | ||
Example { | ||
title: "array", | ||
source: r#"parse_cbor!(decode_base64!("gvUA"))"#, | ||
result: Ok("[true, 0]"), | ||
}, | ||
Example { | ||
title: "string", | ||
source: r#"parse_cbor!(decode_base64!("ZWhlbGxv"))"#, | ||
result: Ok("hello"), | ||
}, | ||
Example { | ||
title: "integer", | ||
source: r#"parse_cbor!(decode_base64!("GCo="))"#, | ||
result: Ok("42"), | ||
}, | ||
Example { | ||
title: "float", | ||
source: r#"parse_cbor!(decode_base64!("+0BFEKPXCj1x"))"#, | ||
result: Ok("42.13"), | ||
}, | ||
Example { | ||
title: "boolean", | ||
source: r#"parse_cbor!(decode_base64!("9A=="))"#, | ||
result: Ok("false"), | ||
}, | ||
] | ||
} | ||
|
||
fn compile( | ||
&self, | ||
_state: &state::TypeState, | ||
_ctx: &mut FunctionCompileContext, | ||
arguments: ArgumentList, | ||
) -> Compiled { | ||
let value = arguments.required("value"); | ||
Ok(ParseCborFn { value }.as_expr()) | ||
} | ||
|
||
fn parameters(&self) -> &'static [Parameter] { | ||
&[Parameter { | ||
keyword: "value", | ||
kind: kind::BYTES, | ||
required: true, | ||
}] | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
struct ParseCborFn { | ||
value: Box<dyn Expression>, | ||
} | ||
|
||
impl FunctionExpression for ParseCborFn { | ||
fn resolve(&self, ctx: &mut Context) -> Resolved { | ||
let value = self.value.resolve(ctx)?; | ||
parse_cbor(value) | ||
} | ||
|
||
fn type_def(&self, _: &state::TypeState) -> TypeDef { | ||
json_type_def() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::value; | ||
use nom::AsBytes; | ||
use std::env; | ||
use std::fs; | ||
use std::path::PathBuf; | ||
|
||
fn test_data_dir() -> PathBuf { | ||
PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap()).join("tests/data/cbor") | ||
} | ||
|
||
fn read_cbor_file(cbor_bin_message_path: &str) -> Vec<u8> { | ||
fs::read(test_data_dir().join(cbor_bin_message_path)).unwrap() | ||
} | ||
|
||
test_function![ | ||
parse_cbor => ParseCbor; | ||
|
||
parses { | ||
args: func_args![ value: value!(read_cbor_file("simple.cbor").as_bytes()) ], | ||
want: Ok(value!({ field: "value" })), | ||
tdef: json_type_def(), | ||
} | ||
|
||
complex_cbor { | ||
args: func_args![ value: value!(read_cbor_file("complex.cbor").as_bytes()) ], | ||
want: Ok(value!({ object: {string: "value", number: 42, array: ["hello", "world"], boolean: false} })), | ||
tdef: json_type_def(), | ||
} | ||
]; | ||
} |
Oops, something went wrong.