Skip to content

Commit

Permalink
[Rust] adding more util functions (#451)
Browse files Browse the repository at this point in the history
* add a strict mode in didc check

* add instantiate_candid

* bump

* bump logos

* fix
  • Loading branch information
chenyan-dfinity committed Jul 10, 2023
1 parent 7caa48a commit c2a310f
Show file tree
Hide file tree
Showing 11 changed files with 225 additions and 58 deletions.
44 changes: 27 additions & 17 deletions Cargo.lock

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

8 changes: 4 additions & 4 deletions rust/candid/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ byteorder = "1.4.3"
candid_derive = { path = "../candid_derive", version = "=0.6.1" }
codespan-reporting = "0.11"
crc32fast = "1.3.0"
data-encoding = "2.3.2"
data-encoding = "2.4.0"
hex = "0.4.2"
leb128 = "0.2.4"
num_enum = "0.5.1"
num_enum = "0.6.1"
num-bigint = { version = "0.4.2", features = ["serde"] }
num-traits = "0.2.12"
paste = "1.0.0"
pretty = "0.10.0"
pretty = "0.12.0"
serde = { version = "1.0.118", features = ["derive"] }
serde_bytes = "0.11"
sha2 = "0.10.1"
Expand All @@ -39,7 +39,7 @@ anyhow = "1.0"
binread = { version = "2.1", features = ["debug_template"] }

lalrpop-util = { version = "0.20.0", optional = true }
logos = { version = "0.12", optional = true }
logos = { version = "0.13", optional = true }

arbitrary = { version = "1.0", optional = true }
# Don't upgrade serde_dhall. It will introduce dependency with invalid license.
Expand Down
4 changes: 2 additions & 2 deletions rust/candid/src/bindings/candid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub fn pp_label(id: &SharedLabel) -> RcDoc {
}
}

fn pp_field(field: &Field, is_variant: bool) -> RcDoc {
pub(crate) fn pp_field(field: &Field, is_variant: bool) -> RcDoc {
let ty_doc = if is_variant && *field.ty == TypeInner::Null {
RcDoc::nil()
} else {
Expand All @@ -156,7 +156,7 @@ pub fn pp_function(func: &Function) -> RcDoc {
.nest(INDENT_SPACE)
}

fn pp_args(args: &[Type]) -> RcDoc {
pub fn pp_args(args: &[Type]) -> RcDoc {
let doc = concat(args.iter().map(pp_ty), ",");
enclose("(", doc, ")")
}
Expand Down
40 changes: 17 additions & 23 deletions rust/candid/src/parser/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ use lalrpop_util::ParseError;
use logos::{Lexer, Logos};

#[derive(Logos, Debug, Clone, PartialEq, Eq, Ord, PartialOrd)]
#[logos(skip r"[ \t\r\n]+")]
#[logos(skip r"//[^\n]*")] // line comment
pub enum Token {
#[regex(r"[ \t\r\n]+", logos::skip)]
// line comment
#[regex("//[^\n]*", logos::skip)]
#[token("/*")]
StartComment,
#[error]
UnexpectedToken,
#[token("=")]
Equals,
#[token("(")]
Expand Down Expand Up @@ -79,24 +76,21 @@ pub enum Token {
#[regex("[0-9]*\\.[0-9]*", parse_number)]
#[regex("[0-9]+(\\.[0-9]*)?[eE][+-]?[0-9]+", parse_number)]
Float(String),
#[regex("true|false", |lex| lex.slice().parse())]
#[regex("true|false", |lex| lex.slice().parse().map_err(|_| ()))]
Boolean(bool),
}

#[derive(Logos, Debug, Clone, PartialEq, Eq)]
enum Comment {
#[error]
Skip,
#[token("*/")]
End,
#[token("/*")]
Start,
}

#[derive(Logos, Debug, Clone, PartialEq, Eq)]
#[allow(clippy::enum_variant_names)]
enum Text {
#[error]
Error,
#[regex(r#"[^\\"]+"#)]
Text,
#[regex(r"\\.")]
Expand Down Expand Up @@ -181,23 +175,23 @@ impl<'input> Iterator for Tokenizer<'input> {
let token = self.lex.next()?;
let span = self.lex.span();
match token {
Token::UnexpectedToken => {
Err(_) => {
let err = format!("Unknown token {}", self.lex.slice());
Some(Err(LexicalError::new(err, span)))
}
Token::StartComment => {
Ok(Token::StartComment) => {
let mut lex = self.lex.to_owned().morph::<Comment>();
let mut nesting = 1;
loop {
match lex.next() {
Some(Comment::Skip) => continue,
Some(Comment::End) => {
Some(Err(_)) => continue,
Some(Ok(Comment::End)) => {
nesting -= 1;
if nesting == 0 {
break;
}
}
Some(Comment::Start) => nesting += 1,
Some(Ok(Comment::Start)) => nesting += 1,
None => {
return Some(Err(LexicalError::new(
"Unclosed comment",
Expand All @@ -209,14 +203,14 @@ impl<'input> Iterator for Tokenizer<'input> {
self.lex = lex.morph::<Token>();
self.next()
}
Token::StartString => {
Ok(Token::StartString) => {
let mut result = String::new();
let mut lex = self.lex.to_owned().morph::<Text>();
loop {
use self::Text::*;
match lex.next() {
Some(Text) => result += lex.slice(),
Some(EscapeCharacter) => match lex.slice().chars().nth(1).unwrap() {
Some(Ok(Text)) => result += lex.slice(),
Some(Ok(EscapeCharacter)) => match lex.slice().chars().nth(1).unwrap() {
'n' => result.push('\n'),
'r' => result.push('\r'),
't' => result.push('\t'),
Expand All @@ -230,7 +224,7 @@ impl<'input> Iterator for Tokenizer<'input> {
)))
}
},
Some(Codepoint) => {
Some(Ok(Codepoint)) => {
let slice = lex.slice();
let hex = slice[3..slice.len() - 1].replace('_', "");
match u32::from_str_radix(&hex, 16)
Expand All @@ -249,7 +243,7 @@ impl<'input> Iterator for Tokenizer<'input> {
Err(e) => return Some(Err(e)),
}
}
Some(Byte) => {
Some(Ok(Byte)) => {
let hex = &lex.slice()[1..];
match u8::from_str_radix(hex, 16) {
Ok(byte) => {
Expand All @@ -266,8 +260,8 @@ impl<'input> Iterator for Tokenizer<'input> {
}
}
}
Some(EndString) => break,
Some(Error) => {
Some(Ok(EndString)) => break,
Some(Err(_)) => {
return Some(Err(LexicalError::new(
format!("Unexpected string {}", lex.slice()),
lex.span(),
Expand All @@ -284,7 +278,7 @@ impl<'input> Iterator for Tokenizer<'input> {
self.lex = lex.morph::<Token>();
Some(Ok((span.start, Token::Text(result), self.lex.span().end)))
}
_ => Some(Ok((span.start, token, span.end))),
Ok(token) => Some(Ok((span.start, token, span.end))),
}
}
}
9 changes: 9 additions & 0 deletions rust/candid/src/types/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,15 @@ pub struct Field {
pub id: SharedLabel,
pub ty: Type,
}
impl fmt::Display for Field {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
crate::bindings::candid::pp_field(self, false).pretty(80)
)
}
}
#[macro_export]
/// Construct a field type, which can be used in `TypeInner::Record` and `TypeInner::Variant`.
///
Expand Down
10 changes: 10 additions & 0 deletions rust/candid/src/types/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ macro_rules! define_function {
self.0.idl_serialize(serializer)
}
}
impl From<$crate::types::reference::Func> for $func {
fn from(v: $crate::types::reference::Func) -> Self {
Self(v)
}
}
impl $func {
pub fn new(principal: $crate::Principal, method: String) -> Self {
$func($crate::types::reference::Func { principal, method })
Expand All @@ -60,6 +65,11 @@ macro_rules! define_service {
self.0.idl_serialize(serializer)
}
}
impl From<$crate::types::reference::Service> for $serv {
fn from(v: $crate::types::reference::Service) -> Self {
Self(v)
}
}
impl $serv {
pub fn new(principal: $crate::Principal) -> Self {
$serv($crate::types::reference::Service { principal })
Expand Down
Loading

0 comments on commit c2a310f

Please sign in to comment.