-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(parse_cbor): add parse_cbor function #1152
Open
MiracleWisp
wants to merge
2
commits into
vectordotdev:main
Choose a base branch
from
MiracleWisp:cbor_support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
|
@@ -66,6 +67,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]> | ||
|
@@ -101,6 +103,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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't recall what does this do. Did you run
dd-rust-license-tool
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dd-rust-licence-tool fails without this config because "crunchy" package doesn't have repository config in cargo.toml
eira-fransham/crunchy#9