Skip to content

Commit 8973b2f

Browse files
committed
Towards making dictionaries serializable
This changeset extendes Dictionary<> and KindedStringsPerFile<> to support the generic Serde (de)serialization mechanism. While we have not decided a format for storing dictionaries (that's binast/container-format-spec#7 ), this will let us experiment using a simple, temporary format.
1 parent 30f85c7 commit 8973b2f

File tree

8 files changed

+41
-8
lines changed

8 files changed

+41
-8
lines changed

crates/binjs_io/Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ lzw = "*"
2020
log = "*"
2121
num_alias = "*"
2222
rand = "^0.4"
23-
range-encoding = "0.1.7"
23+
range-encoding = "0.1.8"
24+
serde = "^1.0"
25+
serde_derive = "^1.0"
2426
vec_map = "*"
2527
xml-rs = "*"
2628

crates/binjs_io/src/entropy/model.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ int_alias!(InstancesInFile, usize);
1818
int_alias!(FilesContaining, usize);
1919

2020

21-
#[derive(Debug, Default)]
21+
#[derive(Debug, Default, Serialize, Deserialize)]
2222
pub struct Dictionary<T> {
2323
/// All booleans appearing in the AST, predicted by path.
2424
pub bool_by_path: PathPredict<Option<bool>, T>,

crates/binjs_io/src/entropy/predict.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub struct Entry<'a, K, T> where K: 'a, T: 'a {
2020
}
2121

2222
/// A generic predictor, associating a context and a key to a value.
23-
#[derive(Debug, Default)]
23+
#[derive(Debug, Default, Serialize, Deserialize)]
2424
pub struct ContextPredict<C, K, T> where C: Eq + Hash + Clone, K: Eq + Hash + Clone {
2525
by_context: HashMap<C, HashMap<K, T>>,
2626
}
@@ -95,7 +95,7 @@ impl<C, K> InstancesToProbabilities for ContextPredict<C, K, usize> where C: Eq
9595
/// limit the prediction depth, e.g. to 0 (don't use any context),
9696
/// 1 (use only the parent + child index) or 2 (use parent +
9797
/// grand-parent and both child indices).
98-
#[derive(Debug, Default)]
98+
#[derive(Debug, Default, Serialize, Deserialize)]
9999
pub struct PathPredict<K, T> where K: Eq + Hash + Clone {
100100
context_predict: ContextPredict<IOPath, K, T>,
101101
}

crates/binjs_io/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ extern crate log;
1414
extern crate num_alias;
1515
extern crate rand;
1616
extern crate range_encoding;
17+
#[macro_use]
18+
extern crate serde_derive;
19+
extern crate serde;
20+
1721
extern crate vec_map;
1822
extern crate xml as xml_rs;
1923

crates/binjs_shared/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ authors = ["David Teller <[email protected]>"]
77
itertools = "*"
88
json = "^0.11"
99
log = "^0.4"
10+
serde = "^1.0"
11+
serde_derive = "^1.0"
12+

crates/binjs_shared/src/ast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use std::fmt::*;
4444
/// path.exit_interface("Interface 1"); // Exiting the wrong interface would panic.
4545
/// ```
4646
47-
#[derive(PartialEq, Eq, Hash, Clone, Default)]
47+
#[derive(PartialEq, Eq, Hash, Clone, Default, Deserialize, Serialize)]
4848
pub struct Path<I, F> where I: Debug + PartialEq, F: Debug + PartialEq {
4949
/// Some(foo) if we have entered interface foo but no field yet.
5050
/// Otherwise, None.
@@ -60,7 +60,7 @@ impl<I, F> From<Vec<PathItem<I, F>>> for Path<I, F> where I: Debug + PartialEq,
6060
}
6161
}
6262

63-
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
63+
#[derive(Debug, PartialEq, Eq, Hash, Clone, Deserialize, Serialize)]
6464
pub struct PathItem<I, F> where I: Debug + PartialEq, F: Debug + PartialEq {
6565
pub interface: I,
6666
pub field: F,

crates/binjs_shared/src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ extern crate itertools;
22
extern crate json;
33
#[macro_use]
44
extern crate log;
5+
extern crate serde;
6+
#[macro_use]
7+
extern crate serde_derive;
58

69
mod json_conversion;
710
pub use json_conversion::*;
@@ -44,7 +47,7 @@ shared_string!(pub FieldName);
4447

4548
/// A container for f64 values that implements an *arbitrary*
4649
/// total order, equality relation, hash.
47-
#[derive(Clone, Debug, Copy)]
50+
#[derive(Clone, Debug, Copy, Deserialize, Serialize)]
4851
pub struct F64(f64);
4952
impl From<f64> for F64 {
5053
fn from(value: f64) -> F64 {

crates/binjs_shared/src/shared_string.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use std::hash::Hash;
33
use std::ops::Deref;
44
use std::rc::Rc;
55

6+
use serde::de::{ Deserialize, Deserializer };
7+
use serde::ser::{ Serialize, Serializer };
68

79
/// An implementation of strings that may easily be shared without copies.
810
///
@@ -57,6 +59,25 @@ impl Default for SharedString {
5759
SharedString::Static("<uninitialized SharedString>")
5860
}
5961
}
62+
/// Shared strings are serialized as strings. This loses any sharing :/
63+
impl Serialize for SharedString {
64+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
65+
where
66+
S: Serializer
67+
{
68+
self.deref().serialize(serializer)
69+
}
70+
}
71+
/// Shared strings are deserialized as Dynamic strings.
72+
impl<'de> Deserialize<'de> for SharedString {
73+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
74+
where
75+
D: Deserializer<'de>
76+
{
77+
let dynamic = String::deserialize(deserializer)?;
78+
Ok(SharedString::Dynamic(Rc::new(dynamic)))
79+
}
80+
}
6081
impl SharedString {
6182
pub fn as_str(&self) -> &str {
6283
self.deref()
@@ -75,7 +96,7 @@ impl SharedString {
7596
#[macro_export]
7697
macro_rules! shared_string {
7798
(pub $name: ident) => {
78-
#[derive(Clone, Eq, PartialOrd, Ord, Debug, Hash)]
99+
#[derive(Clone, Eq, PartialOrd, Ord, Debug, Hash, Serialize, Deserialize)]
79100
pub struct $name(pub shared_string::SharedString);
80101
impl $name {
81102
pub fn from_str(value: &'static str) -> Self {

0 commit comments

Comments
 (0)