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

feat!: split candid crate #471

Merged
merged 25 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 17 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
38 changes: 29 additions & 9 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 Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
members = [
"rust/candid",
"rust/candid_parser",
"rust/candid_derive",
"tools/didc",
]
Expand Down
43 changes: 0 additions & 43 deletions rust/candid/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ readme = "README.md"
categories = ["encoding", "parsing", "wasm"]
keywords = ["internet-computer", "idl", "candid", "dfinity", "parser"]
include = ["src", "Cargo.toml", "build.rs", "LICENSE", "README.md"]
build = "build.rs"

[build-dependencies]
lalrpop = { version = "0.20.0", optional = true }

[dependencies]
byteorder = "1.4.3"
Expand All @@ -38,22 +35,10 @@ thiserror = "1.0.20"
anyhow = "1.0"
binread = { version = "2.1", features = ["debug_template"] }

lalrpop-util = { version = "0.20.0", optional = true }
lwshang marked this conversation as resolved.
Show resolved Hide resolved
logos = { version = "0.13", optional = true }
convert_case = { version = "0.6", optional = true }

arbitrary = { version = "1.0", optional = true }
# Don't upgrade serde_dhall. It will introduce dependency with invalid license.
serde_dhall = { version = "0.11", default-features = false, optional = true }
fake = { version = "2.4", optional = true }
rand = { version = "0.8", optional = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
stacker = "0.1"

[dev-dependencies]
goldenfile = "1.1.0"
test-generator = "0.3.0"
rand = "0.8"
criterion = "0.4"
serde_cbor = "0.11.2"
Expand All @@ -67,33 +52,5 @@ name = "benchmark"
harness = false
path = "benches/benchmark.rs"

[[test]]
name = "test_suite"
path = "tests/test_suite.rs"
required-features = ["parser"]
[[test]]
name = "value"
path = "tests/value.rs"
required-features = ["parser"]
[[test]]
name = "parse_value"
path = "tests/parse_value.rs"
required-features = ["parser"]
[[test]]
name = "parse_type"
path = "tests/parse_type.rs"
required-features = ["parser"]

[features]
configs = ["serde_dhall"]
random = ["parser", "configs", "arbitrary", "fake", "rand"]
parser = ["lalrpop", "lalrpop-util", "logos", "convert_case"]
all = ["random"]
mute_warnings = []

# docs.rs-specific configuration
# To test locally: RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc --features all
[package.metadata.docs.rs]
features = ["all"]
# defines the configuration attribute `docsrs`
rustdoc-args = ["--cfg", "docsrs"]
4 changes: 2 additions & 2 deletions rust/candid/src/bindings/candid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn is_keyword(id: &str) -> bool {
KEYWORDS.contains(&id)
}

pub(crate) fn is_valid_as_id(id: &str) -> bool {
pub fn is_valid_as_id(id: &str) -> bool {
if id.is_empty() || !id.is_ascii() {
return false;
}
Expand Down Expand Up @@ -161,7 +161,7 @@ pub fn pp_args(args: &[Type]) -> RcDoc {
enclose("(", doc, ")")
}

pub(crate) fn pp_modes(modes: &[crate::types::FuncMode]) -> RcDoc {
pub fn pp_modes(modes: &[crate::types::FuncMode]) -> RcDoc {
RcDoc::concat(modes.iter().map(|m| RcDoc::space().append(m.to_doc())))
}

Expand Down
16 changes: 0 additions & 16 deletions rust/candid/src/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,3 @@
// This module assumes the input are type checked, it is safe to use unwrap.

pub mod candid;
lwshang marked this conversation as resolved.
Show resolved Hide resolved

#[cfg_attr(docsrs, doc(cfg(feature = "parser")))]
#[cfg(feature = "parser")]
pub mod analysis;
#[cfg_attr(docsrs, doc(cfg(feature = "parser")))]
#[cfg(feature = "parser")]
pub mod javascript;
#[cfg_attr(docsrs, doc(cfg(feature = "parser")))]
#[cfg(feature = "parser")]
pub mod motoko;
#[cfg_attr(docsrs, doc(cfg(feature = "parser")))]
#[cfg(feature = "parser")]
pub mod rust;
#[cfg_attr(docsrs, doc(cfg(feature = "parser")))]
#[cfg(feature = "parser")]
pub mod typescript;
125 changes: 4 additions & 121 deletions rust/candid/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,13 @@

use codespan_reporting::diagnostic::Label;
use serde::{de, ser};
use std::io;
use std::{io, num::ParseIntError};
use thiserror::Error;

#[cfg(feature = "parser")]
use crate::parser::token;
#[cfg(feature = "parser")]
use codespan_reporting::{
diagnostic::Diagnostic,
files::{Error as ReportError, SimpleFile},
term::{self, termcolor::StandardStream},
};

pub type Result<T = ()> = std::result::Result<T, Error>;

#[derive(Debug, Error)]
pub enum Error {
#[cfg_attr(docsrs, doc(cfg(feature = "parser")))]
#[cfg(feature = "parser")]
#[error("Candid parser error: {0}")]
Parse(#[from] token::ParserError),

#[error("binary parser error: {}", .0.get(0).map(|f| format!("{} at byte offset {}", f.message, f.range.start/2)).unwrap_or_else(|| "io error".to_string()))]
Binread(Vec<Label<()>>),

Expand All @@ -40,42 +26,6 @@ impl Error {
pub fn subtype<T: ToString>(msg: T) -> Self {
Error::Subtype(msg.to_string())
}
#[cfg_attr(docsrs, doc(cfg(feature = "parser")))]
#[cfg(feature = "parser")]
pub fn report(&self) -> Diagnostic<()> {
match self {
Error::Parse(e) => {
use lalrpop_util::ParseError::*;
let mut diag = Diagnostic::error().with_message("parser error");
let label = match e {
User { error } => {
Label::primary((), error.span.clone()).with_message(&error.err)
}
InvalidToken { location } => {
Label::primary((), *location..location + 1).with_message("Invalid token")
}
UnrecognizedEof { location, expected } => {
diag = diag.with_notes(report_expected(expected));
Label::primary((), *location..location + 1).with_message("Unexpected EOF")
}
UnrecognizedToken { token, expected } => {
diag = diag.with_notes(report_expected(expected));
Label::primary((), token.0..token.2).with_message("Unexpected token")
}
ExtraToken { token } => {
Label::primary((), token.0..token.2).with_message("Extra token")
}
};
diag.with_labels(vec![label])
}
Error::Binread(labels) => {
let diag = Diagnostic::error().with_message("decoding error");
diag.with_labels(labels.to_vec())
}
Error::Subtype(e) => Diagnostic::error().with_message(e),
Error::Custom(e) => Diagnostic::error().with_message(e.to_string()),
}
}
}

fn get_binread_labels(e: &binread::Error) -> Vec<Label<()>> {
Expand Down Expand Up @@ -123,26 +73,6 @@ fn get_binread_labels(e: &binread::Error) -> Vec<Label<()>> {
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "parser")))]
#[cfg(feature = "parser")]
fn report_expected(expected: &[String]) -> Vec<String> {
if expected.is_empty() {
return Vec::new();
}
use pretty::RcDoc;
let doc: RcDoc<()> = RcDoc::intersperse(
expected.iter().map(RcDoc::text),
RcDoc::text(",").append(RcDoc::softline()),
);
let header = if expected.len() == 1 {
"Expects"
} else {
"Expects one of"
};
let doc = RcDoc::text(header).append(RcDoc::softline().append(doc));
vec![doc.pretty(70).to_string()]
}

impl ser::Error for Error {
fn custom<T: std::fmt::Display>(msg: T) -> Self {
Error::msg(format!("Serialize error: {msg}"))
Expand Down Expand Up @@ -174,56 +104,9 @@ impl From<binread::Error> for Error {
Error::Binread(get_binread_labels(&e))
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "parser")))]
#[cfg(feature = "parser")]
impl From<ReportError> for Error {
fn from(e: ReportError) -> Error {
Error::msg(e)
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "random")))]
#[cfg(feature = "random")]
impl From<arbitrary::Error> for Error {
fn from(e: arbitrary::Error) -> Error {
Error::msg(format!("arbitrary error: {e}"))
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "configs")))]
#[cfg(feature = "configs")]
impl From<serde_dhall::Error> for Error {
fn from(e: serde_dhall::Error) -> Error {
Error::msg(format!("dhall error: {e}"))
impl From<ParseIntError> for Error {
fn from(e: ParseIntError) -> Error {
Error::msg(format!("ParseIntError: {e}"))
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "parser")))]
#[cfg(feature = "parser")]
pub fn pretty_parse<T>(name: &str, str: &str) -> Result<T>
where
T: std::str::FromStr<Err = Error>,
{
str.parse::<T>().or_else(|e| {
let writer = StandardStream::stderr(term::termcolor::ColorChoice::Auto);
let config = term::Config::default();
let file = SimpleFile::new(name, str);
term::emit(&mut writer.lock(), &config, &file, &e.report())?;
Err(e)
})
}
#[cfg_attr(docsrs, doc(cfg(feature = "parser")))]
#[cfg(feature = "parser")]
pub fn pretty_read<T>(reader: &mut std::io::Cursor<&[u8]>) -> Result<T>
where
T: binread::BinRead,
{
T::read(reader).or_else(|e| {
let e = Error::from(e);
let writer = StandardStream::stderr(term::termcolor::ColorChoice::Auto);
let config = term::Config::default();
let str = hex::encode(reader.get_ref());
let file = SimpleFile::new("binary", &str);
term::emit(&mut writer.lock(), &config, &file, &e.report())?;
Err(e)
})
}
Loading
Loading