Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swap ciborium for bincode #184

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
29 changes: 29 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/ditto-ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ serde = { version = "1.0", features = ["derive"] }
petgraph = "0.6"
non-empty-vec = { version = "0.2", features = ["serde"] }
indexmap = { version = "1.9", features = ["serde"] }
bincode = { version = "2.0.0-rc.2", features = ["serde"] }
#unindent = "xx" <-- might come in useful for smart multi-line strings (like Nix)
14 changes: 9 additions & 5 deletions crates/ditto-ast/src/expression.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::{
FullyQualifiedName, FullyQualifiedProperName, Name, ProperName, Span, Type, UnusedName,
};
use bincode::{Decode, Encode};
use indexmap::IndexMap;
use non_empty_vec::NonEmpty;
use serde::{Deserialize, Serialize};

/// The real business value.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)]
#[serde(tag = "expression", content = "data")]
pub enum Expression {
/// Everyone's favourite: the humble function
Expand Down Expand Up @@ -81,6 +82,7 @@ pub enum Expression {
expression: Box<Self>,

/// Patterns to be matched against and their corresponding expressions.
#[bincode(with_serde)]
arms: NonEmpty<(Pattern, Self)>,
},
/// A value constructor local to the current module, e.g. `Just` and `Ok`.
Expand Down Expand Up @@ -226,6 +228,7 @@ pub enum Expression {
/// The expression being updated.
target: Box<Self>,
/// The record updates.
#[bincode(with_serde)]
fields: IndexMap<Name, Self>,
},
/// An array literal.
Expand All @@ -251,6 +254,7 @@ pub enum Expression {
record_type: Type,

/// Record fields.
#[bincode(with_serde)]
fields: IndexMap<Name, Self>,
},
/// `true`
Expand Down Expand Up @@ -551,7 +555,7 @@ impl Expression {
/// ```ditto
/// some_function(argument)
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)]
pub enum Argument {
/// A standard expression argument.
/// Could be a variable, could be another function call.
Expand All @@ -574,7 +578,7 @@ impl Argument {
}

/// A pattern to be matched.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)]
pub enum Pattern {
/// A local constructor pattern.
LocalConstructor {
Expand Down Expand Up @@ -611,7 +615,7 @@ pub enum Pattern {
}

/// A chain of Effect statements.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)]
pub enum Effect {
/// `do { name <- expression; rest }`
Bind {
Expand Down Expand Up @@ -646,7 +650,7 @@ pub enum Effect {
}

/// A value declaration that appears within a `let` expression.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)]
pub struct LetValueDeclaration {
/// The pattern containing names to be bound.
pub pattern: Pattern,
Expand Down
3 changes: 2 additions & 1 deletion crates/ditto-ast/src/graph.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! A convenient graph API, based largely on Haskell's [`Data.Graph`](https://hackage.haskell.org/package/containers-0.6.5.1/docs/Data-Graph.html) module, and built on [`petgraph`](https://crates.io/crates/petgraph).

use bincode::{Decode, Encode};
use petgraph::{algo::kosaraju_scc, graph::NodeIndex, Graph}; // REVIEW or tarjan?
use serde::{Deserialize, Serialize};
use std::{
Expand All @@ -10,7 +11,7 @@ use std::{
/// Strongly connected component.
///
/// <https://en.wikipedia.org/wiki/Strongly_connected_component>
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode)]
#[serde(untagged)]
pub enum Scc<Node> {
/// A single vertex that is not in any cycle.
Expand Down
4 changes: 3 additions & 1 deletion crates/ditto-ast/src/kind.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use bincode::{Decode, Encode};
use non_empty_vec::NonEmpty;
use serde::{Deserialize, Serialize};

/// The kind of types.
///
/// Note that there is currently no source representation for kinds.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Encode, Decode)]
pub enum Kind {
/// Also known as `*` to functional programming folk.
Type,
Expand All @@ -20,6 +21,7 @@ pub enum Kind {
/// Also note that the "return kind" can only be `Kind::Type` at the moment.
Function {
/// The kinds of the arguments this type expects.
#[bincode(with_serde)]
parameters: NonEmpty<Self>,
},
/// A series of labelled types. Used for records.
Expand Down
23 changes: 15 additions & 8 deletions crates/ditto-ast/src/module.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::{graph::Scc, Expression, Kind, ModuleName, Name, ProperName, Span, Type};
use bincode::{Decode, Encode};
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};

/// A ditto module.
///
/// A module captures three namespaces: types, constructors and values.
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Encode, Decode)]
pub struct Module {
/// The name of the module, e.g. `Some.Module`.
///
Expand All @@ -19,14 +20,17 @@ pub struct Module {
pub exports: ModuleExports,

/// Types defined in this module.
#[bincode(with_serde)]
pub types: ModuleTypes,

/// Types defined in this module.
#[bincode(with_serde)]
pub constructors: ModuleConstructors,

/// Top-level values defined within the module.
///
/// The flattened names should form a unique list.
#[bincode(with_serde)]
pub values: ModuleValues,

/// The topological sort order of `values`.
Expand All @@ -39,7 +43,7 @@ pub struct Module {
pub type ModuleTypes = IndexMap<ProperName, ModuleType>;

/// A type defined by a module.
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Encode, Decode)]
pub enum ModuleType {
/// A type introduced by an ordinary type declaration.
Type {
Expand Down Expand Up @@ -123,7 +127,7 @@ pub type ModuleConstructors = IndexMap<ProperName, ModuleConstructor>;
pub type ModuleValues = IndexMap<Name, ModuleValue>;

/// A value defined by a module.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)]
pub struct ModuleValue {
/// Documentation comments (if any).
pub doc_comments: Vec<String>,
Expand All @@ -149,7 +153,7 @@ impl Module {
}

/// A single constructor, e.g. the `Ok` constructor for `Result`.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)]
pub struct ModuleConstructor {
/// Documentation comments (if any).
pub doc_comments: Vec<String>,
Expand Down Expand Up @@ -184,21 +188,24 @@ impl ModuleConstructor {
}

/// Everything that a module can expose.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[derive(Debug, Clone, Default, Serialize, Deserialize, Encode, Decode)]
pub struct ModuleExports {
/// Exposed type constructors.
#[bincode(with_serde)]
pub types: ModuleExportsTypes,
/// Exposed type constructors.
#[bincode(with_serde)]
pub constructors: ModuleExportsConstructors,
/// Exposed values.
#[bincode(with_serde)]
pub values: ModuleExportsValues,
}

/// The type of `module_exports.types`, for convenience.
pub type ModuleExportsTypes = IndexMap<ProperName, ModuleExportsType>;

/// A single exposed type.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)]
pub enum ModuleExportsType {
/// An exported type introduced by an ordinary type declaration.
Type {
Expand Down Expand Up @@ -252,7 +259,7 @@ impl ModuleExportsType {
pub type ModuleExportsConstructors = IndexMap<ProperName, ModuleExportsConstructor>;

/// A single exposed constructor.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)]
pub struct ModuleExportsConstructor {
/// Documentation comments (if any).
pub doc_comments: Vec<String>,
Expand All @@ -270,7 +277,7 @@ pub struct ModuleExportsConstructor {
pub type ModuleExportsValues = IndexMap<Name, ModuleExportsValue>;

/// A single exposed value.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)]
pub struct ModuleExportsValue {
/// Documentation comments (if any).
pub doc_comments: Vec<String>,
Expand Down
23 changes: 15 additions & 8 deletions crates/ditto-ast/src/name.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use bincode::{Decode, Encode};
use ditto_cst as cst;
use non_empty_vec::NonEmpty;
use serde::{Deserialize, Serialize};
use std::fmt;

/// A "name" begins with a lower case letter.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[derive(
Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Encode, Decode,
)]
pub struct Name(pub String);

impl fmt::Display for Name {
Expand All @@ -20,7 +23,9 @@ impl From<cst::Name> for Name {
}

/// An "unused name" begins with a single underscore.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[derive(
Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Encode, Decode,
)]
pub struct UnusedName(pub String);

impl fmt::Display for UnusedName {
Expand All @@ -36,7 +41,9 @@ impl From<cst::UnusedName> for UnusedName {
}

/// A "proper name" begins with an upper case letter.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[derive(
Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Encode, Decode,
)]
pub struct ProperName(pub String);

impl fmt::Display for ProperName {
Expand All @@ -52,7 +59,7 @@ impl From<cst::ProperName> for ProperName {
}

/// A package name consists of lower case letters, numbers and hyphens. It must start with a letter.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Encode, Decode)]
pub struct PackageName(pub String);

impl fmt::Display for PackageName {
Expand All @@ -70,8 +77,8 @@ impl From<cst::PackageName> for PackageName {
/// A [ModuleName] is a non-empty collection of [ProperName]s.
///
/// In the source these are joined with a dot.
#[derive(Debug, Clone, Eq, Serialize, Deserialize)]
pub struct ModuleName(pub NonEmpty<ProperName>);
#[derive(Debug, Clone, Eq, Serialize, Deserialize, Encode, Decode)]
pub struct ModuleName(#[bincode(with_serde)] pub NonEmpty<ProperName>);

impl ModuleName {
/// Convert a module name to a string, joining the component [ProperName]s with the given `separator`.
Expand Down Expand Up @@ -134,7 +141,7 @@ impl From<cst::QualifiedProperName> for ModuleName {
}

/// Something is "qualified" if it can have an initial module name.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Encode, Decode)]
pub struct Qualified<Value> {
/// The optional qualifier.
pub module_name: Option<ProperName>,
Expand Down Expand Up @@ -187,7 +194,7 @@ where
pub type FullyQualifiedModuleName = (Option<PackageName>, ModuleName);

/// The canonical name for an identifier.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Encode, Decode)]
pub struct FullyQualified<Value> {
/// The package and module to which it belongs.
pub module_name: FullyQualifiedModuleName,
Expand Down
Loading