Skip to content

Commit

Permalink
Towards making dictionaries serializable
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Yoric committed Oct 10, 2018
1 parent 8fdd4a1 commit 6a5f4f7
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 8 deletions.
4 changes: 3 additions & 1 deletion crates/binjs_io/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ lzw = "*"
log = "*"
num_alias = "*"
rand = "^0.4"
range-encoding = "0.1.7"
range-encoding = "0.1.8"
serde = "^1.0"
serde_derive = "^1.0"
vec_map = "*"
xml-rs = "*"

Expand Down
2 changes: 1 addition & 1 deletion crates/binjs_io/src/entropy/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int_alias!(InstancesInFile, usize);
int_alias!(FilesContaining, usize);


#[derive(Debug, Default)]
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Dictionary<T> {
/// All booleans appearing in the AST, predicted by path.
pub bool_by_path: PathPredict<Option<bool>, T>,
Expand Down
4 changes: 2 additions & 2 deletions crates/binjs_io/src/entropy/predict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct Entry<'a, K, T> where K: 'a, T: 'a {
}

/// A generic predictor, associating a context and a key to a value.
#[derive(Debug, Default)]
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct ContextPredict<C, K, T> where C: Eq + Hash + Clone, K: Eq + Hash + Clone {
by_context: HashMap<C, HashMap<K, T>>,
}
Expand Down Expand Up @@ -95,7 +95,7 @@ impl<C, K> InstancesToProbabilities for ContextPredict<C, K, usize> where C: Eq
/// limit the prediction depth, e.g. to 0 (don't use any context),
/// 1 (use only the parent + child index) or 2 (use parent +
/// grand-parent and both child indices).
#[derive(Debug, Default)]
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct PathPredict<K, T> where K: Eq + Hash + Clone {
context_predict: ContextPredict<IOPath, K, T>,
}
Expand Down
4 changes: 4 additions & 0 deletions crates/binjs_io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ extern crate log;
extern crate num_alias;
extern crate rand;
extern crate range_encoding;
#[macro_use]
extern crate serde_derive;
extern crate serde;

extern crate vec_map;
extern crate xml as xml_rs;

Expand Down
3 changes: 3 additions & 0 deletions crates/binjs_shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ authors = ["David Teller <[email protected]>"]
itertools = "*"
json = "^0.11"
log = "^0.4"
serde = "^1.0"
serde_derive = "^1.0"

4 changes: 2 additions & 2 deletions crates/binjs_shared/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use std::fmt::*;
/// path.exit_interface("Interface 1"); // Exiting the wrong interface would panic.
/// ```

#[derive(PartialEq, Eq, Hash, Clone, Default)]
#[derive(PartialEq, Eq, Hash, Clone, Default, Deserialize, Serialize)]
pub struct Path<I, F> where I: Debug + PartialEq, F: Debug + PartialEq {
/// Some(foo) if we have entered interface foo but no field yet.
/// Otherwise, None.
Expand All @@ -60,7 +60,7 @@ impl<I, F> From<Vec<PathItem<I, F>>> for Path<I, F> where I: Debug + PartialEq,
}
}

#[derive(Debug, PartialEq, Eq, Hash, Clone)]
#[derive(Debug, PartialEq, Eq, Hash, Clone, Deserialize, Serialize)]
pub struct PathItem<I, F> where I: Debug + PartialEq, F: Debug + PartialEq {
pub interface: I,
pub field: F,
Expand Down
5 changes: 4 additions & 1 deletion crates/binjs_shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ extern crate itertools;
extern crate json;
#[macro_use]
extern crate log;
extern crate serde;
#[macro_use]
extern crate serde_derive;

mod json_conversion;
pub use json_conversion::*;
Expand Down Expand Up @@ -44,7 +47,7 @@ shared_string!(pub FieldName);

/// A container for f64 values that implements an *arbitrary*
/// total order, equality relation, hash.
#[derive(Clone, Debug, Copy)]
#[derive(Clone, Debug, Copy, Deserialize, Serialize)]
pub struct F64(f64);
impl From<f64> for F64 {
fn from(value: f64) -> F64 {
Expand Down
23 changes: 22 additions & 1 deletion crates/binjs_shared/src/shared_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::hash::Hash;
use std::ops::Deref;
use std::rc::Rc;

use serde::de::{ Deserialize, Deserializer };
use serde::ser::{ Serialize, Serializer };

/// An implementation of strings that may easily be shared without copies.
///
Expand Down Expand Up @@ -57,6 +59,25 @@ impl Default for SharedString {
SharedString::Static("<uninitialized SharedString>")
}
}
/// Shared strings are serialized as strings. This loses any sharing :/
impl Serialize for SharedString {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer
{
self.deref().serialize(serializer)
}
}
/// Shared strings are deserialized as Dynamic strings.
impl<'de> Deserialize<'de> for SharedString {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>
{
let dynamic = String::deserialize(deserializer)?;
Ok(SharedString::Dynamic(Rc::new(dynamic)))
}
}
impl SharedString {
pub fn as_str(&self) -> &str {
self.deref()
Expand All @@ -75,7 +96,7 @@ impl SharedString {
#[macro_export]
macro_rules! shared_string {
(pub $name: ident) => {
#[derive(Clone, Eq, PartialOrd, Ord, Debug, Hash)]
#[derive(Clone, Eq, PartialOrd, Ord, Debug, Hash, Serialize, Deserialize)]
pub struct $name(pub shared_string::SharedString);
impl $name {
pub fn from_str(value: &'static str) -> Self {
Expand Down

0 comments on commit 6a5f4f7

Please sign in to comment.