-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Semyon Uchvatov
committed
Nov 28, 2024
1 parent
b240904
commit 8d6a19d
Showing
8 changed files
with
151 additions
and
8 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
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
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,128 @@ | ||
use crate::compiler::prelude::*; | ||
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 JSON type" | ||
} | ||
|
||
fn usage(&self) -> &'static str { | ||
indoc! {" | ||
Parses the provided `value` as CBOR. | ||
Only JSON types are returned. If you need to convert a `string` into a `timestamp`, | ||
consider the `parse_timestamp` function. | ||
"} | ||
} | ||
|
||
fn parameters(&self) -> &'static [Parameter] { | ||
&[Parameter { | ||
keyword: "value", | ||
kind: kind::BYTES, | ||
required: true, | ||
}] | ||
} | ||
|
||
fn examples(&self) -> &'static [Example] { | ||
&[Example { | ||
title: "object", | ||
source: r#"parse_cbor!(decode_base64!("oWVmaWVsZGV2YWx1ZQ=="))"#, | ||
result: Ok(r#"{ "field": "value" }"#), | ||
}] | ||
} | ||
|
||
fn compile( | ||
&self, | ||
_state: &state::TypeState, | ||
_ctx: &mut FunctionCompileContext, | ||
arguments: ArgumentList, | ||
) -> Compiled { | ||
let value = arguments.required("value"); | ||
Ok(ParseCborFn { value }.as_expr()) | ||
} | ||
} | ||
|
||
#[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 { | ||
type_def() | ||
} | ||
} | ||
|
||
fn inner_kind() -> Kind { | ||
Kind::null() | ||
| Kind::bytes() | ||
| Kind::integer() | ||
| Kind::float() | ||
| Kind::boolean() | ||
| Kind::array(Collection::any()) | ||
| Kind::object(Collection::any()) | ||
} | ||
|
||
fn type_def() -> TypeDef { | ||
TypeDef::bytes() | ||
.fallible() | ||
.or_boolean() | ||
.or_integer() | ||
.or_float() | ||
.add_null() | ||
.or_array(Collection::from_unknown(inner_kind())) | ||
.or_object(Collection::from_unknown(inner_kind())) | ||
} | ||
|
||
#[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: 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: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
�fobject�fstringevaluefnumber*earray�ehelloeworldgboolean� |
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 @@ | ||
�efieldevalue |