Skip to content

Commit 3663e0d

Browse files
authored
feat: add MessagePack reader/writer (#56)
Implements MessagePack binary format support using rmpv: - Parser: from_bytes/from_reader for binary input, from_str for hex - Writer: to_bytes for binary output, to_string as hex for CLI pipeline - All primitive types round-trip: null, bool, int, float, string, bytes - Nested structures (maps, arrays) fully preserved with key order - Binary data (Bytes variant) natively preserved - Cross-format round-trip: MessagePack → JSON → MessagePack - CLI integration: -f msgpack/-t msgpack, .msgpack/.mp extensions - 30+ tests covering round-trips, edge cases, errors, hex encoding Fixes #27
1 parent 46fe72c commit 3663e0d

File tree

4 files changed

+562
-0
lines changed

4 files changed

+562
-0
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ toml = "0.8"
1515
csv = "1"
1616
clap = { version = "4", features = ["derive"] }
1717
quick-xml = "0.31"
18+
rmp-serde = "1"
19+
rmpv = "1"
1820
indexmap = { version = "2", features = ["serde"] }
1921
thiserror = "1"
2022
anyhow = "1"

src/cli.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub enum Format {
1111
Toml,
1212
Csv,
1313
Xml,
14+
Msgpack,
1415
}
1516

1617
impl Format {
@@ -23,6 +24,7 @@ impl Format {
2324
(Format::Toml, "TOML", &["toml"]),
2425
(Format::Csv, "CSV", &["csv"]),
2526
(Format::Xml, "XML", &["xml"]),
27+
(Format::Msgpack, "MessagePack", &["msgpack", "mp"]),
2628
]
2729
}
2830

@@ -35,6 +37,7 @@ impl Format {
3537
"toml" => Some(Format::Toml),
3638
"csv" => Some(Format::Csv),
3739
"xml" => Some(Format::Xml),
40+
"msgpack" | "mp" => Some(Format::Msgpack),
3841
_ => None,
3942
}
4043
}
@@ -55,6 +58,7 @@ impl Format {
5558
"toml" => Some(Format::Toml),
5659
"csv" => Some(Format::Csv),
5760
"xml" => Some(Format::Xml),
61+
"msgpack" | "mp" => Some(Format::Msgpack),
5862
_ => None,
5963
}
6064
}
@@ -69,6 +73,7 @@ impl fmt::Display for Format {
6973
Format::Toml => write!(f, "toml"),
7074
Format::Csv => write!(f, "csv"),
7175
Format::Xml => write!(f, "xml"),
76+
Format::Msgpack => write!(f, "msgpack"),
7277
}
7378
}
7479
}
@@ -196,6 +201,7 @@ pub fn parse_input(input: &str, format: Format) -> crate::error::Result<crate::v
196201
Format::Toml => crate::formats::toml::from_str(input),
197202
Format::Csv => crate::formats::csv::from_str(input),
198203
Format::Xml => crate::formats::xml::from_str(input),
204+
Format::Msgpack => crate::formats::msgpack::from_str(input),
199205
}
200206
}
201207

@@ -218,6 +224,7 @@ pub fn serialize_output(
218224
Format::Toml => crate::formats::toml::to_string(value),
219225
Format::Csv => crate::formats::csv::to_string(value),
220226
Format::Xml => crate::formats::xml::to_string(value),
227+
Format::Msgpack => crate::formats::msgpack::to_string(value),
221228
}
222229
}
223230

@@ -378,6 +385,12 @@ mod tests {
378385
assert_eq!(Format::from_extension("ndjson"), Some(Format::Jsonl));
379386
}
380387

388+
#[test]
389+
fn format_from_extension_msgpack() {
390+
assert_eq!(Format::from_extension("msgpack"), Some(Format::Msgpack));
391+
assert_eq!(Format::from_extension("mp"), Some(Format::Msgpack));
392+
}
393+
381394
#[test]
382395
fn format_from_path_json() {
383396
let p = PathBuf::from("data.json");
@@ -403,6 +416,8 @@ mod tests {
403416
assert_eq!(Format::from_name("xml"), Some(Format::Xml));
404417
assert_eq!(Format::from_name("jsonl"), Some(Format::Jsonl));
405418
assert_eq!(Format::from_name("ndjson"), Some(Format::Jsonl));
419+
assert_eq!(Format::from_name("msgpack"), Some(Format::Msgpack));
420+
assert_eq!(Format::from_name("mp"), Some(Format::Msgpack));
406421
assert_eq!(Format::from_name("nope"), None);
407422
}
408423

@@ -518,6 +533,7 @@ mod tests {
518533
assert_eq!(Format::Toml.to_string(), "toml");
519534
assert_eq!(Format::Csv.to_string(), "csv");
520535
assert_eq!(Format::Xml.to_string(), "xml");
536+
assert_eq!(Format::Msgpack.to_string(), "msgpack");
521537
}
522538

523539
// -- Mapping CLI flags --------------------------------------------------

src/formats/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub mod csv;
22
pub mod json;
33
pub mod jsonl;
4+
pub mod msgpack;
45
pub mod toml;
56
pub mod xml;
67
pub mod yaml;

0 commit comments

Comments
 (0)