Skip to content

Commit c638b7c

Browse files
committed
Add encoding-impl test
1 parent be2dce7 commit c638b7c

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/swc_atoms/src/wtf8_atom.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,32 @@ use crate::Atom;
1818
#[repr(transparent)]
1919
pub struct Wtf8Atom(pub(super) hstr::Wtf8Atom);
2020

21+
#[cfg(feature = "encoding-impl")]
22+
impl cbor4ii::core::enc::Encode for Wtf8Atom {
23+
#[inline]
24+
fn encode<W: cbor4ii::core::enc::Write>(
25+
&self,
26+
writer: &mut W,
27+
) -> Result<(), cbor4ii::core::enc::Error<W::Error>> {
28+
cbor4ii::core::types::Bytes(self.as_bytes()).encode(writer)
29+
}
30+
}
31+
32+
#[cfg(feature = "encoding-impl")]
33+
impl<'de> cbor4ii::core::dec::Decode<'de> for Wtf8Atom {
34+
#[inline]
35+
fn decode<R: cbor4ii::core::dec::Read<'de>>(
36+
reader: &mut R,
37+
) -> Result<Self, cbor4ii::core::dec::Error<R::Error>> {
38+
let s = <cbor4ii::core::types::Bytes<&[u8]>>::decode(reader)?;
39+
40+
// This is not sound, maybe Wtf8Buf should make bytes operations safe
41+
Ok(Self(hstr::Wtf8Atom::from(unsafe {
42+
Wtf8Buf::from_bytes_unchecked(s.0.into())
43+
})))
44+
}
45+
}
46+
2147
#[cfg(feature = "arbitrary")]
2248
#[cfg_attr(docsrs, doc(cfg(feature = "arbitrary")))]
2349
impl<'a> arbitrary::Arbitrary<'a> for Wtf8Atom {

crates/swc_ecma_parser/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,12 @@ walkdir = { workspace = true }
5555
codspeed-criterion-compat = { workspace = true }
5656
swc_ecma_ast = { version = "17.0.0", path = "../swc_ecma_ast", features = [
5757
"serde-impl",
58+
"encoding-impl",
5859
] }
5960
swc_ecma_visit = { version = "17.0.0", path = "../swc_ecma_visit" }
6061
swc_malloc = { version = "1.2.4", path = "../swc_malloc" }
6162
testing = { version = "17.0.0", path = "../testing" }
63+
cbor4ii = { workspace = true, features = [ "use_std" ] }
6264

6365
[[example]]
6466
name = "typescript"

crates/swc_ecma_parser/tests/js.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,28 @@ fn run_spec(file: &Path, output_json: &Path, config_path: &Path) {
6363
is_test262: false,
6464
});
6565

66-
let json =
67-
serde_json::to_string_pretty(&program).expect("failed to serialize module as json");
66+
// json
67+
{
68+
let json =
69+
serde_json::to_string_pretty(&program).expect("failed to serialize module as json");
70+
71+
if StdErr::from(json).compare_to_file(output_json).is_err() {
72+
panic!()
73+
}
74+
}
75+
76+
// cbor
77+
{
78+
use cbor4ii::core::utils::{ BufWriter, SliceReader };
79+
use cbor4ii::core::enc::Encode;
80+
use cbor4ii::core::dec::Decode;
81+
82+
let mut buf = BufWriter::new(Vec::new());
83+
program.encode(&mut buf).unwrap();
6884

69-
if StdErr::from(json).compare_to_file(output_json).is_err() {
70-
panic!()
85+
let buf = buf.into_inner();
86+
let mut buf = SliceReader::new(buf.as_slice());
87+
let _program = Program::decode(&mut buf).unwrap();
7188
}
7289

7390
Ok(())

0 commit comments

Comments
 (0)