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
20 changes: 1 addition & 19 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,12 @@
+ `pretty::candid::pp_label` now takes a `&Label` parameter, instead of a `&SharedLabel`.

* Non-breaking changes:
+ The following structs have been moved from the `candid_parser` crate to the `candid::types::syntax` module. See the `candid_parser`'s changelog for more details.
+ Makes the warning message for the special opt subtyping rule more explicit in the `candid::types::subtype::subtype` and `candid::types::subtype::subtype_with_config` functions.

### candid_parser

* Breaking changes:
+ The following structs have been moved to the `candid` crate:
- `IDLType`
- `IDLTypes`
- `PrimType`
- `FuncType`
- `IDLArgType`
- `TypeField`
- `Dec`
- `Binding`
- `IDLProg`
- `IDLInitArgs`
As a consequence, the `FromStr` trait is no longer implemented for the following types:
- `IDLProg`
- `IDLInitArgs`
- `IDLType`
- `IDLTypes`
You must now use the `parse_idl_prog`, `parse_idl_init_args`, `parse_idl_type` and `parse_idl_types` functions to parse these types, respectively.
+ `pretty_parse` doesn't work anymore with the `IDLProg` and `IDLTypes` types. Use `pretty_parse_idl_prog` and `pretty_parse_idl_types` instead.
+ The `candid_parser::types` module has been renamed to `candid_parser::syntax`.
+ The `args` field in both `FuncType` and `IDLInitArgs` now have type `Vec<IDLArgType>`.

* Non-breaking changes:
Expand Down
9 changes: 6 additions & 3 deletions rust/candid/src/pretty/candid.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::collections::HashMap;

use crate::pretty::syntax::pp_docs;
use crate::pretty::utils::*;
use crate::types::{ArgType, Field, FuncMode, Function, Label, Type, TypeEnv, TypeInner};
use pretty::RcDoc;
Expand Down Expand Up @@ -62,15 +61,15 @@ fn needs_quote(id: &str) -> bool {
!is_valid_as_id(id) || is_keyword(id)
}

pub(crate) fn ident_string(id: &str) -> String {
fn ident_string(id: &str) -> String {
if needs_quote(id) {
format!("\"{}\"", id.escape_debug())
} else {
id.to_string()
}
}

pub(crate) fn pp_text(id: &str) -> RcDoc {
pub fn pp_text(id: &str) -> RcDoc {
RcDoc::text(ident_string(id))
}

Expand Down Expand Up @@ -122,6 +121,10 @@ pub fn pp_ty_inner(ty: &TypeInner) -> RcDoc {
}
}

pub fn pp_docs<'a>(docs: &'a [String]) -> RcDoc<'a> {
lines(docs.iter().map(|line| RcDoc::text("// ").append(line)))
}

pub fn pp_label(id: &Label) -> RcDoc {
match id {
Label::Named(id) => pp_text(id),
Expand Down
1 change: 0 additions & 1 deletion rust/candid/src/pretty/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! pretty printer for Candid type and value

pub mod candid;
pub mod syntax;
pub mod utils;
1 change: 0 additions & 1 deletion rust/candid/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use serde::ser::Error;
mod impls;
pub mod internal;
pub mod subtype;
pub mod syntax;
pub mod type_env;
#[cfg_attr(docsrs, doc(cfg(feature = "value")))]
#[cfg(feature = "value")]
Expand Down
2 changes: 1 addition & 1 deletion rust/candid_parser/src/bindings/motoko.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// This module implements the Candid to Motoko binding as specified in
// https://github.com/dfinity/motoko/blob/master/design/IDL-Motoko.md

use crate::syntax::{self, IDLActorType, IDLMergedProg, IDLType};
use candid::pretty::candid::is_valid_as_id;
use candid::pretty::utils::*;
use candid::types::syntax::{self, IDLActorType, IDLMergedProg, IDLType};
use candid::types::{ArgType, Field, FuncMode, Function, Label, SharedLabel, Type, TypeInner};
use candid::TypeEnv;
use pretty::RcDoc;
Expand Down
6 changes: 2 additions & 4 deletions rust/candid_parser/src/bindings/rust.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use super::analysis::{chase_actor, infer_rec};
use crate::{
configs::{ConfigState, ConfigTree, Configs, Context, StateElem},
Deserialize,
};
use candid::types::{
syntax::{self, IDLActorType, IDLMergedProg, IDLType},
Field, Function, Label, SharedLabel, Type, TypeEnv, TypeInner,
Deserialize,
};
use candid::types::{Field, Function, Label, SharedLabel, Type, TypeEnv, TypeInner};
use candid::{pretty::utils::*, types::ArgType};
use convert_case::{Case, Casing};
use pretty::RcDoc;
Expand Down
2 changes: 1 addition & 1 deletion rust/candid_parser/src/bindings/typescript.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::javascript::{ident, is_tuple_fields};
use crate::syntax::{self, IDLMergedProg, IDLType};
use candid::pretty::utils::*;
use candid::types::syntax::{self, IDLMergedProg, IDLType};
use candid::types::{Field, Function, Label, SharedLabel, Type, TypeEnv, TypeInner};
use pretty::RcDoc;

Expand Down
17 changes: 3 additions & 14 deletions rust/candid_parser/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
//! When serializing or deserializing Candid goes wrong.

use candid::types::syntax::{IDLProg, IDLTypes};
use crate::token;
use codespan_reporting::diagnostic::Label;
use std::io;
use thiserror::Error;

use crate::{parse_idl_prog, parse_idl_types, token};
use codespan_reporting::{
diagnostic::Diagnostic,
files::{Error as ReportError, SimpleFile},
term::{self, termcolor::StandardStream},
};
use std::io;
use thiserror::Error;

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

Expand Down Expand Up @@ -106,22 +104,13 @@ impl From<toml::de::Error> for Error {
}
}

/// Does not work for parsing [IDLProg] and [IDLTypes], use [pretty_parse_idl_prog] and [pretty_parse_idl_types] instead.
pub fn pretty_parse<T>(name: &str, str: &str) -> Result<T>
where
T: std::str::FromStr<Err = Error>,
{
str.parse::<T>().or_else(|e| pretty_print_err(name, str, e))
}

pub fn pretty_parse_idl_prog(name: &str, str: &str) -> Result<IDLProg> {
parse_idl_prog(str).or_else(|e| pretty_print_err(name, str, e))
}

pub fn pretty_parse_idl_types(name: &str, str: &str) -> Result<IDLTypes> {
parse_idl_types(str).or_else(|e| pretty_print_err(name, str, e))
}

/// Wrap the parser error and pretty print the error message.
/// ```
/// use candid_parser::{pretty_wrap, parse_idl_args};
Expand Down
2 changes: 1 addition & 1 deletion rust/candid_parser/src/grammar.lalrpop
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::test::{Assert, Input, Test};
use super::token::{Token, error, error2, LexicalError, Span, TriviaMap};
use candid::{Principal, types::Label};
use candid::types::syntax::{IDLType, PrimType, TypeField, FuncType, Binding, Dec, IDLProg, IDLTypes, IDLInitArgs, IDLArgType, IDLActorType};
use crate::syntax::{IDLType, PrimType, TypeField, FuncType, Binding, Dec, IDLProg, IDLTypes, IDLInitArgs, IDLArgType, IDLActorType};
use candid::types::value::{IDLField, IDLValue, IDLArgs, VariantValue};
use candid::types::{TypeEnv, FuncMode};
use candid::utils::check_unique;
Expand Down
36 changes: 7 additions & 29 deletions rust/candid_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
//! ```
//! # fn f() -> anyhow::Result<()> {
//! use candid::{TypeEnv, types::{Type, TypeInner}};
//! use candid_parser::{check_prog, parse_idl_prog};
//! use candid_parser::{IDLProg, check_prog};
//! let did_file = r#"
//! type List = opt record { head: int; tail: List };
//! type byte = nat8;
Expand All @@ -58,7 +58,7 @@
//! "#;
//!
//! // Parse did file into an AST
//! let ast = parse_idl_prog(did_file)?;
//! let ast: IDLProg = did_file.parse()?;
//!
//! // Type checking a given .did file
//! // let (env, opt_actor) = check_file("a.did")?;
Expand Down Expand Up @@ -86,7 +86,7 @@
//! use candid::{IDLArgs, types::value::IDLValue};
//! use candid_parser::parse_idl_args;
//! # use candid::TypeEnv;
//! # use candid_parser::{check_prog, parse_idl_prog};
//! # use candid_parser::{IDLProg, check_prog};
//! # let did_file = r#"
//! # type List = opt record { head: int; tail: List };
//! # type byte = nat8;
Expand All @@ -95,7 +95,7 @@
//! # g : (List) -> (int) query;
//! # }
//! # "#;
//! # let ast = parse_idl_prog(did_file)?;
//! # let ast = did_file.parse::<IDLProg>()?;
//! # let mut env = TypeEnv::new();
//! # let actor = check_prog(&mut env, &ast)?.unwrap();
//! // Get method type f : (byte, int, nat, int8) -> (List)
Expand All @@ -119,15 +119,15 @@
#![cfg_attr(docsrs, feature(doc_cfg))]

pub mod error;
pub use error::{
pretty_parse, pretty_parse_idl_prog, pretty_parse_idl_types, pretty_wrap, Error, Result,
};
pub use error::{pretty_parse, pretty_wrap, Error, Result};

pub mod bindings;
pub mod grammar;
pub mod syntax;
pub mod token;
pub mod typing;
pub mod utils;
pub use syntax::IDLProg;
pub use typing::{check_file, check_prog, pretty_check_file};

pub use candid;
Expand All @@ -142,28 +142,6 @@ pub mod configs;
pub mod random;
pub mod test;

pub fn parse_idl_prog(str: &str) -> Result<candid::types::syntax::IDLProg> {
let trivia = token::TriviaMap::default();
let lexer = token::Tokenizer::new_with_trivia(str, trivia.clone());
let res = grammar::IDLProgParser::new().parse(Some(&trivia.clone()), lexer)?;
Ok(res)
}

pub fn parse_idl_init_args(str: &str) -> Result<candid::types::syntax::IDLInitArgs> {
let lexer = token::Tokenizer::new(str);
Ok(grammar::IDLInitArgsParser::new().parse(None, lexer)?)
}

pub fn parse_idl_type(str: &str) -> Result<candid::types::syntax::IDLType> {
let lexer = token::Tokenizer::new(str);
Ok(grammar::TypParser::new().parse(None, lexer)?)
}

pub fn parse_idl_types(str: &str) -> Result<candid::types::syntax::IDLTypes> {
let lexer = token::Tokenizer::new(str);
Ok(grammar::TypsParser::new().parse(None, lexer)?)
}

pub fn parse_idl_args(s: &str) -> crate::Result<candid::IDLArgs> {
let lexer = token::Tokenizer::new(s);
Ok(grammar::ArgsParser::new().parse(None, lexer)?)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use crate::{
mod pretty;

pub use pretty::pretty_print;

use crate::error;
use anyhow::{anyhow, bail, Context, Result};
use candid::{
idl_hash,
types::{FuncMode, Label},
};
use anyhow::{anyhow, bail, Context, Result};
use std::collections::HashMap;

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -35,11 +40,29 @@ impl IDLType {
}
}

impl std::str::FromStr for IDLType {
type Err = error::Error;
fn from_str(str: &str) -> error::Result<Self> {
let trivia = super::token::TriviaMap::default();
let lexer = super::token::Tokenizer::new_with_trivia(str, trivia.clone());
Ok(super::grammar::TypParser::new().parse(Some(&trivia), lexer)?)
}
}

#[derive(Debug, Clone)]
pub struct IDLTypes {
pub args: Vec<IDLType>,
}

impl std::str::FromStr for IDLTypes {
type Err = error::Error;
fn from_str(str: &str) -> error::Result<Self> {
let trivia = super::token::TriviaMap::default();
let lexer = super::token::Tokenizer::new_with_trivia(str, trivia.clone());
Ok(super::grammar::TypsParser::new().parse(Some(&trivia), lexer)?)
}
}

macro_rules! enum_to_doc {
(pub enum $name:ident {
$($variant:ident),*,
Expand Down Expand Up @@ -156,12 +179,30 @@ impl IDLProg {
}
}

impl std::str::FromStr for IDLProg {
type Err = error::Error;
fn from_str(str: &str) -> error::Result<Self> {
let trivia = super::token::TriviaMap::default();
let lexer = super::token::Tokenizer::new_with_trivia(str, trivia.clone());
Ok(super::grammar::IDLProgParser::new().parse(Some(&trivia), lexer)?)
}
}

#[derive(Debug)]
pub struct IDLInitArgs {
pub decs: Vec<Dec>,
pub args: Vec<IDLArgType>,
}

impl std::str::FromStr for IDLInitArgs {
type Err = error::Error;
fn from_str(str: &str) -> error::Result<Self> {
let trivia = super::token::TriviaMap::default();
let lexer = super::token::Tokenizer::new_with_trivia(str, trivia.clone());
Ok(super::grammar::IDLInitArgsParser::new().parse(Some(&trivia), lexer)?)
}
}

#[derive(Debug)]
pub struct IDLMergedProg {
typ_decs: Vec<Binding>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use pretty::RcDoc;

use crate::{
pretty::{
candid::{pp_label, pp_modes, pp_text},
candid::{pp_docs, pp_label, pp_modes, pp_text},
utils::{concat, enclose, enclose_space, ident, kwd, lines, str, INDENT_SPACE, LINE_WIDTH},
},
types::syntax::{
syntax::{
Binding, FuncType, IDLActorType, IDLArgType, IDLMergedProg, IDLType, PrimType, TypeField,
},
};
Expand Down Expand Up @@ -153,10 +153,6 @@ fn pp_defs(prog: &IDLMergedProg) -> RcDoc {
}))
}

pub(crate) fn pp_docs<'a>(docs: &'a [String]) -> RcDoc<'a> {
lines(docs.iter().map(|line| RcDoc::text("// ").append(line)))
}

fn pp_actor(actor: &IDLActorType) -> RcDoc {
let docs = pp_docs(&actor.docs);
let service_doc = match actor.typ {
Expand Down
2 changes: 1 addition & 1 deletion rust/candid_parser/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::typing::check_prog;
use crate::syntax::{Dec, IDLProg, IDLType};
use crate::{Error, Result};
use candid::types::syntax::{Dec, IDLProg, IDLType};
use candid::types::value::IDLArgs;
use candid::types::{Type, TypeEnv};
use candid::DecoderConfig;
Expand Down
Loading