-
Notifications
You must be signed in to change notification settings - Fork 8
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
Showing
18 changed files
with
3,216 additions
and
3,215 deletions.
There are no files selected for viewing
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 @@ | ||
newline_style = "Unix" |
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 |
---|---|---|
@@ -1,32 +1,32 @@ | ||
use thiserror::Error; | ||
|
||
mod json; | ||
mod raw_bytes; | ||
|
||
#[derive(Debug, Error, Clone, PartialEq, Eq)] | ||
pub enum CborUtilError { | ||
#[error("invalid CBOR or JSON encoding")] | ||
InvalidEncoding, | ||
#[error(r#"unexpected field "{0}" in raw byte representation"#)] | ||
UnexpectedByteReprKey(String), | ||
#[error("the bytes in {0} representation is invalid")] | ||
InvalidByteRepr(&'static str), | ||
#[error("missing data field for raw byte representation")] | ||
MissingData, | ||
#[error(r#"unknown byte representation "{0}""#)] | ||
UnknownByteRepr(String), | ||
} | ||
|
||
pub type CborUtilResult<T> = Result<T, CborUtilError>; | ||
|
||
pub use json::{cbor_to_json, json_to_cbor}; | ||
pub use raw_bytes::{escape_cbor_buf, unescape_cbor_buf}; | ||
|
||
pub(crate) fn to_cbor( | ||
value: Result<ciborium::Value, ciborium::value::Error>, | ||
) -> serde_bytes::ByteBuf { | ||
let mut buf = Vec::with_capacity(128); | ||
ciborium::ser::into_writer(&value.expect("cannot encode cbor"), &mut buf) | ||
.expect("Cannot serialize proxy"); | ||
serde_bytes::ByteBuf::from(buf) | ||
} | ||
use thiserror::Error; | ||
|
||
mod json; | ||
mod raw_bytes; | ||
|
||
#[derive(Debug, Error, Clone, PartialEq, Eq)] | ||
pub enum CborUtilError { | ||
#[error("invalid CBOR or JSON encoding")] | ||
InvalidEncoding, | ||
#[error(r#"unexpected field "{0}" in raw byte representation"#)] | ||
UnexpectedByteReprKey(String), | ||
#[error("the bytes in {0} representation is invalid")] | ||
InvalidByteRepr(&'static str), | ||
#[error("missing data field for raw byte representation")] | ||
MissingData, | ||
#[error(r#"unknown byte representation "{0}""#)] | ||
UnknownByteRepr(String), | ||
} | ||
|
||
pub type CborUtilResult<T> = Result<T, CborUtilError>; | ||
|
||
pub use json::{cbor_to_json, json_to_cbor}; | ||
pub use raw_bytes::{escape_cbor_buf, unescape_cbor_buf}; | ||
|
||
pub(crate) fn to_cbor( | ||
value: Result<ciborium::Value, ciborium::value::Error>, | ||
) -> serde_bytes::ByteBuf { | ||
let mut buf = Vec::with_capacity(128); | ||
ciborium::ser::into_writer(&value.expect("cannot encode cbor"), &mut buf) | ||
.expect("Cannot serialize proxy"); | ||
serde_bytes::ByteBuf::from(buf) | ||
} |
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 |
---|---|---|
@@ -1,51 +1,51 @@ | ||
use super::{escape_cbor_buf, unescape_cbor_buf, CborUtilError, CborUtilResult}; | ||
|
||
pub fn cbor_to_json(cbor: &[u8]) -> CborUtilResult<String> { | ||
let mut val = cbor4ii::serde::from_slice(cbor).map_err(|_| CborUtilError::InvalidEncoding)?; | ||
escape_cbor_buf(&mut val); | ||
serde_json::to_string_pretty(&val).map_err(|_| CborUtilError::InvalidEncoding) | ||
} | ||
|
||
pub fn json_to_cbor(json: &str) -> CborUtilResult<Vec<u8>> { | ||
let mut val = serde_json::from_str(json).map_err(|_| CborUtilError::InvalidEncoding)?; | ||
unescape_cbor_buf(&mut val)?; | ||
cbor4ii::serde::to_vec(vec![], &val).map_err(|_| CborUtilError::InvalidEncoding) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_cbor_to_json() { | ||
let cbor = b"\x42\x68\x68"; | ||
let json = cbor_to_json(cbor).unwrap(); | ||
assert_eq!( | ||
json, | ||
r#"{ | ||
"__byte_repr": "utf8", | ||
"data": "hh" | ||
}"# | ||
); | ||
} | ||
#[test] | ||
fn test_cbor_to_json_invalid_cbor() { | ||
let cbor = b"\x42\x68"; | ||
let res = cbor_to_json(cbor); | ||
assert_eq!(res, Err(CborUtilError::InvalidEncoding)); | ||
} | ||
|
||
#[test] | ||
fn test_json_to_cbor() { | ||
let json = r#"{ "__byte_repr": "utf8", "data": "hh" }"#; | ||
let cbor = json_to_cbor(json).unwrap(); | ||
let expected_cbor = b"\x42\x68\x68"; | ||
assert_eq!(cbor, expected_cbor); | ||
} | ||
#[test] | ||
fn test_json_to_cbor_invalid_json() { | ||
let json = "{ "; | ||
let res = json_to_cbor(json); | ||
assert_eq!(res, Err(CborUtilError::InvalidEncoding)); | ||
} | ||
} | ||
use super::{escape_cbor_buf, unescape_cbor_buf, CborUtilError, CborUtilResult}; | ||
|
||
pub fn cbor_to_json(cbor: &[u8]) -> CborUtilResult<String> { | ||
let mut val = cbor4ii::serde::from_slice(cbor).map_err(|_| CborUtilError::InvalidEncoding)?; | ||
escape_cbor_buf(&mut val); | ||
serde_json::to_string_pretty(&val).map_err(|_| CborUtilError::InvalidEncoding) | ||
} | ||
|
||
pub fn json_to_cbor(json: &str) -> CborUtilResult<Vec<u8>> { | ||
let mut val = serde_json::from_str(json).map_err(|_| CborUtilError::InvalidEncoding)?; | ||
unescape_cbor_buf(&mut val)?; | ||
cbor4ii::serde::to_vec(vec![], &val).map_err(|_| CborUtilError::InvalidEncoding) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_cbor_to_json() { | ||
let cbor = b"\x42\x68\x68"; | ||
let json = cbor_to_json(cbor).unwrap(); | ||
assert_eq!( | ||
json, | ||
r#"{ | ||
"__byte_repr": "utf8", | ||
"data": "hh" | ||
}"# | ||
); | ||
} | ||
#[test] | ||
fn test_cbor_to_json_invalid_cbor() { | ||
let cbor = b"\x42\x68"; | ||
let res = cbor_to_json(cbor); | ||
assert_eq!(res, Err(CborUtilError::InvalidEncoding)); | ||
} | ||
|
||
#[test] | ||
fn test_json_to_cbor() { | ||
let json = r#"{ "__byte_repr": "utf8", "data": "hh" }"#; | ||
let cbor = json_to_cbor(json).unwrap(); | ||
let expected_cbor = b"\x42\x68\x68"; | ||
assert_eq!(cbor, expected_cbor); | ||
} | ||
#[test] | ||
fn test_json_to_cbor_invalid_json() { | ||
let json = "{ "; | ||
let res = json_to_cbor(json); | ||
assert_eq!(res, Err(CborUtilError::InvalidEncoding)); | ||
} | ||
} |
Oops, something went wrong.