Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
218 changes: 111 additions & 107 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

resolver = "2"

members = ["crates/plotnik-cli", "crates/plotnik-lib", "crates/plotnik-langs", "crates/plotnik-macros", "crates/plotnik-core"]
members = ["crates/plotnik-cli", "crates/plotnik-lib", "crates/plotnik-langs", "crates/plotnik-core"]
2 changes: 2 additions & 0 deletions crates/plotnik-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ repository = "https://github.com/plotnik-lang/plotnik"
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(coverage_nightly)'] }

[dependencies]
indexmap = { version = "2", features = ["serde"] }
postcard = { version = "1", features = ["alloc"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
16 changes: 16 additions & 0 deletions crates/plotnik-core/src/grammar/binary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//! Binary serialization for grammars using postcard.

use super::json::GrammarError;
use super::types::Grammar;

impl Grammar {
/// Deserialize grammar from binary format.
pub fn from_binary(bytes: &[u8]) -> Result<Self, GrammarError> {
postcard::from_bytes(bytes).map_err(GrammarError::Binary)
}

/// Serialize grammar to binary format.
pub fn to_binary(&self) -> Vec<u8> {
postcard::to_allocvec(self).expect("serialization should not fail")
}
}
39 changes: 39 additions & 0 deletions crates/plotnik-core/src/grammar/binary_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use super::*;

#[test]
fn roundtrip() {
let json = r#"{
"name": "test",
"rules": {
"source_file": { "type": "SYMBOL", "name": "expression" },
"expression": { "type": "STRING", "value": "x" }
}
}"#;

let grammar = Grammar::from_json(json).unwrap();
let binary = grammar.to_binary();
let decoded = Grammar::from_binary(&binary).unwrap();

assert_eq!(grammar.name, decoded.name);
assert_eq!(grammar.rules.len(), decoded.rules.len());
}

#[test]
fn roundtrip_preserves_order() {
let json = r#"{
"name": "test",
"rules": {
"program": { "type": "SYMBOL", "name": "statement" },
"statement": { "type": "SYMBOL", "name": "expression" },
"expression": { "type": "STRING", "value": "x" }
}
}"#;

let grammar = Grammar::from_json(json).unwrap();
let binary = grammar.to_binary();
let decoded = Grammar::from_binary(&binary).unwrap();

assert_eq!(decoded.rules[0].0, "program");
assert_eq!(decoded.rules[1].0, "statement");
assert_eq!(decoded.rules[2].0, "expression");
}
Loading