Skip to content

Commit

Permalink
lf
Browse files Browse the repository at this point in the history
  • Loading branch information
bdbai committed Apr 28, 2024
1 parent 403d4e0 commit 495efa5
Show file tree
Hide file tree
Showing 18 changed files with 3,216 additions and 3,215 deletions.
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
newline_style = "Unix"
64 changes: 32 additions & 32 deletions ytflow-app-util/src/cbor.rs
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)
}
102 changes: 51 additions & 51 deletions ytflow-app-util/src/cbor/json.rs
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));
}
}
Loading

0 comments on commit 495efa5

Please sign in to comment.