Skip to content

Commit 495efa5

Browse files
committed
lf
1 parent 403d4e0 commit 495efa5

File tree

18 files changed

+3216
-3215
lines changed

18 files changed

+3216
-3215
lines changed

rustfmt.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
newline_style = "Unix"

ytflow-app-util/src/cbor.rs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
use thiserror::Error;
2-
3-
mod json;
4-
mod raw_bytes;
5-
6-
#[derive(Debug, Error, Clone, PartialEq, Eq)]
7-
pub enum CborUtilError {
8-
#[error("invalid CBOR or JSON encoding")]
9-
InvalidEncoding,
10-
#[error(r#"unexpected field "{0}" in raw byte representation"#)]
11-
UnexpectedByteReprKey(String),
12-
#[error("the bytes in {0} representation is invalid")]
13-
InvalidByteRepr(&'static str),
14-
#[error("missing data field for raw byte representation")]
15-
MissingData,
16-
#[error(r#"unknown byte representation "{0}""#)]
17-
UnknownByteRepr(String),
18-
}
19-
20-
pub type CborUtilResult<T> = Result<T, CborUtilError>;
21-
22-
pub use json::{cbor_to_json, json_to_cbor};
23-
pub use raw_bytes::{escape_cbor_buf, unescape_cbor_buf};
24-
25-
pub(crate) fn to_cbor(
26-
value: Result<ciborium::Value, ciborium::value::Error>,
27-
) -> serde_bytes::ByteBuf {
28-
let mut buf = Vec::with_capacity(128);
29-
ciborium::ser::into_writer(&value.expect("cannot encode cbor"), &mut buf)
30-
.expect("Cannot serialize proxy");
31-
serde_bytes::ByteBuf::from(buf)
32-
}
1+
use thiserror::Error;
2+
3+
mod json;
4+
mod raw_bytes;
5+
6+
#[derive(Debug, Error, Clone, PartialEq, Eq)]
7+
pub enum CborUtilError {
8+
#[error("invalid CBOR or JSON encoding")]
9+
InvalidEncoding,
10+
#[error(r#"unexpected field "{0}" in raw byte representation"#)]
11+
UnexpectedByteReprKey(String),
12+
#[error("the bytes in {0} representation is invalid")]
13+
InvalidByteRepr(&'static str),
14+
#[error("missing data field for raw byte representation")]
15+
MissingData,
16+
#[error(r#"unknown byte representation "{0}""#)]
17+
UnknownByteRepr(String),
18+
}
19+
20+
pub type CborUtilResult<T> = Result<T, CborUtilError>;
21+
22+
pub use json::{cbor_to_json, json_to_cbor};
23+
pub use raw_bytes::{escape_cbor_buf, unescape_cbor_buf};
24+
25+
pub(crate) fn to_cbor(
26+
value: Result<ciborium::Value, ciborium::value::Error>,
27+
) -> serde_bytes::ByteBuf {
28+
let mut buf = Vec::with_capacity(128);
29+
ciborium::ser::into_writer(&value.expect("cannot encode cbor"), &mut buf)
30+
.expect("Cannot serialize proxy");
31+
serde_bytes::ByteBuf::from(buf)
32+
}

ytflow-app-util/src/cbor/json.rs

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
1-
use super::{escape_cbor_buf, unescape_cbor_buf, CborUtilError, CborUtilResult};
2-
3-
pub fn cbor_to_json(cbor: &[u8]) -> CborUtilResult<String> {
4-
let mut val = cbor4ii::serde::from_slice(cbor).map_err(|_| CborUtilError::InvalidEncoding)?;
5-
escape_cbor_buf(&mut val);
6-
serde_json::to_string_pretty(&val).map_err(|_| CborUtilError::InvalidEncoding)
7-
}
8-
9-
pub fn json_to_cbor(json: &str) -> CborUtilResult<Vec<u8>> {
10-
let mut val = serde_json::from_str(json).map_err(|_| CborUtilError::InvalidEncoding)?;
11-
unescape_cbor_buf(&mut val)?;
12-
cbor4ii::serde::to_vec(vec![], &val).map_err(|_| CborUtilError::InvalidEncoding)
13-
}
14-
15-
#[cfg(test)]
16-
mod tests {
17-
use super::*;
18-
19-
#[test]
20-
fn test_cbor_to_json() {
21-
let cbor = b"\x42\x68\x68";
22-
let json = cbor_to_json(cbor).unwrap();
23-
assert_eq!(
24-
json,
25-
r#"{
26-
"__byte_repr": "utf8",
27-
"data": "hh"
28-
}"#
29-
);
30-
}
31-
#[test]
32-
fn test_cbor_to_json_invalid_cbor() {
33-
let cbor = b"\x42\x68";
34-
let res = cbor_to_json(cbor);
35-
assert_eq!(res, Err(CborUtilError::InvalidEncoding));
36-
}
37-
38-
#[test]
39-
fn test_json_to_cbor() {
40-
let json = r#"{ "__byte_repr": "utf8", "data": "hh" }"#;
41-
let cbor = json_to_cbor(json).unwrap();
42-
let expected_cbor = b"\x42\x68\x68";
43-
assert_eq!(cbor, expected_cbor);
44-
}
45-
#[test]
46-
fn test_json_to_cbor_invalid_json() {
47-
let json = "{ ";
48-
let res = json_to_cbor(json);
49-
assert_eq!(res, Err(CborUtilError::InvalidEncoding));
50-
}
51-
}
1+
use super::{escape_cbor_buf, unescape_cbor_buf, CborUtilError, CborUtilResult};
2+
3+
pub fn cbor_to_json(cbor: &[u8]) -> CborUtilResult<String> {
4+
let mut val = cbor4ii::serde::from_slice(cbor).map_err(|_| CborUtilError::InvalidEncoding)?;
5+
escape_cbor_buf(&mut val);
6+
serde_json::to_string_pretty(&val).map_err(|_| CborUtilError::InvalidEncoding)
7+
}
8+
9+
pub fn json_to_cbor(json: &str) -> CborUtilResult<Vec<u8>> {
10+
let mut val = serde_json::from_str(json).map_err(|_| CborUtilError::InvalidEncoding)?;
11+
unescape_cbor_buf(&mut val)?;
12+
cbor4ii::serde::to_vec(vec![], &val).map_err(|_| CborUtilError::InvalidEncoding)
13+
}
14+
15+
#[cfg(test)]
16+
mod tests {
17+
use super::*;
18+
19+
#[test]
20+
fn test_cbor_to_json() {
21+
let cbor = b"\x42\x68\x68";
22+
let json = cbor_to_json(cbor).unwrap();
23+
assert_eq!(
24+
json,
25+
r#"{
26+
"__byte_repr": "utf8",
27+
"data": "hh"
28+
}"#
29+
);
30+
}
31+
#[test]
32+
fn test_cbor_to_json_invalid_cbor() {
33+
let cbor = b"\x42\x68";
34+
let res = cbor_to_json(cbor);
35+
assert_eq!(res, Err(CborUtilError::InvalidEncoding));
36+
}
37+
38+
#[test]
39+
fn test_json_to_cbor() {
40+
let json = r#"{ "__byte_repr": "utf8", "data": "hh" }"#;
41+
let cbor = json_to_cbor(json).unwrap();
42+
let expected_cbor = b"\x42\x68\x68";
43+
assert_eq!(cbor, expected_cbor);
44+
}
45+
#[test]
46+
fn test_json_to_cbor_invalid_json() {
47+
let json = "{ ";
48+
let res = json_to_cbor(json);
49+
assert_eq!(res, Err(CborUtilError::InvalidEncoding));
50+
}
51+
}

0 commit comments

Comments
 (0)