Skip to content

Commit

Permalink
update to syn 2 and release (#452)
Browse files Browse the repository at this point in the history
* upgrade syn to v2

* bump version

* changelog

* cargo update
  • Loading branch information
chenyan-dfinity authored Jul 11, 2023
1 parent 528a343 commit 5066e27
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 128 deletions.
205 changes: 114 additions & 91 deletions Cargo.lock

Large diffs are not rendered by default.

21 changes: 20 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@

# Changelog

## 2022-06-30 (Rust 0.9.0)
## 2023-07-11

### Rust (0.9.1)

* `utils::service_equal` to check if two service are structurally equal under variable renaming.
* `utils::instantiate_candid` to generate metadata from did file: separate init args, flatten imports. For now, comments in the original did file is not preserved.
* `impl From<Func/Service>` trait for `define_function/define_service` macros.
* Make `bindings::candid::pp_args` a public method.
* Bump dependencies, notably `pretty`, `logos` and `syn`.

### Candid UI

* Bump agent-js to fix the new response code change
* Bump candid to 0.9

### didc

* Add a strict mode for `didc check` which checks for structural equality instead of backward compatibility.

## 2023-06-30 (Rust 0.9.0)

### Breaking changes:

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ the data they exchange, with type safety and extensibility.

* The [spec](spec/) directory contains Candid specifications, including the [Candid language specification](spec/Candid.md) and a soundness proof.
* The [official manual](https://internetcomputer.org/docs/current/developer-docs/build/candid/candid-intro/) is hosted by dfinity; see [./docs](docs/) for the source code.
* Roman Kashitsyn's [Candid for engineers](https://mmapped.blog/posts/20-candid-for-engineers.html) blog post.
* Joachim Breitner's [Candid explainer](https://www.joachim-breitner.de/blog/782-A_Candid_explainer__The_rough_idea) blog post.

## Implementations

Expand Down
5 changes: 2 additions & 3 deletions rust/candid/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "candid"
version = "0.9.0"
version = "0.9.1"
edition = "2021"
authors = ["DFINITY Team"]
description = "Candid is an interface description language (IDL) for interacting with canisters running on the Internet Computer."
Expand All @@ -20,7 +20,7 @@ lalrpop = { version = "0.20.0", optional = true }

[dependencies]
byteorder = "1.4.3"
candid_derive = { path = "../candid_derive", version = "=0.6.1" }
candid_derive = { path = "../candid_derive", version = "=0.6.2" }
codespan-reporting = "0.11"
crc32fast = "1.3.0"
data-encoding = "2.4.0"
Expand Down Expand Up @@ -55,7 +55,6 @@ goldenfile = "1.1.0"
test-generator = "0.3.0"
rand = "0.8"
criterion = "0.4"
serde = { version = "1.0.133", features = ["derive"] }
serde_cbor = "0.11.2"
serde_json = "1.0.74"
serde_test = "1.0.137"
Expand Down
4 changes: 2 additions & 2 deletions rust/candid_derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "candid_derive"
version = "0.6.1"
version = "0.6.2"
edition = "2021"
authors = ["DFINITY Team"]
description = "Macros implementation of #[derive(CandidType)] for the Candid."
Expand All @@ -20,6 +20,6 @@ proc-macro = true

[dependencies]
quote = "1.0.7"
syn = { version = "1.0.38", features = ["visit", "full"] }
syn = { version = "2.0", features = ["visit", "full"] }
proc-macro2 = "1.0.19"
lazy_static = "1.4.0"
39 changes: 21 additions & 18 deletions rust/candid_derive/src/derive.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{candid_path, idl_hash};
use super::{candid_path, get_lit_str, idl_hash};
use proc_macro2::TokenStream;
use quote::quote;
use std::collections::BTreeSet;
Expand Down Expand Up @@ -265,12 +265,13 @@ struct Field {
with_bytes: bool,
}

fn get_serde_meta_items(attr: &syn::Attribute) -> Result<Vec<syn::NestedMeta>, ()> {
if !attr.path.is_ident("serde") {
fn get_serde_meta_items(attr: &syn::Attribute) -> Result<Vec<syn::Meta>, ()> {
if !attr.path().is_ident("serde") {
return Ok(Vec::new());
}
match attr.parse_meta() {
Ok(syn::Meta::List(meta)) => Ok(meta.nested.into_iter().collect()),
let nested = attr.parse_args_with(Punctuated::<syn::Meta, Token![,]>::parse_terminated);
match nested {
Ok(nested) => Ok(nested.into_iter().collect()),
_ => Err(()),
}
}
Expand All @@ -281,36 +282,38 @@ struct Attributes {
}

fn get_attrs(attrs: &[syn::Attribute]) -> Attributes {
use syn::Meta::{List, NameValue};
use syn::NestedMeta::Meta;
use syn::Meta;
let mut res = Attributes {
rename: None,
with_bytes: false,
};
for item in attrs.iter().flat_map(get_serde_meta_items).flatten() {
match &item {
// #[serde(rename = "foo")]
Meta(NameValue(m)) if m.path.is_ident("rename") => {
if let syn::Lit::Str(lit) = &m.lit {
Meta::NameValue(m) if m.path.is_ident("rename") => {
if let Ok(lit) = get_lit_str(&m.value) {
res.rename = Some(lit.value());
}
}
// #[serde(rename(serialize = "foo"))]
Meta(List(metas)) if metas.path.is_ident("rename") => {
for item in metas.nested.iter() {
match item {
Meta(NameValue(m)) if m.path.is_ident("serialize") => {
if let syn::Lit::Str(lit) = &m.lit {
res.rename = Some(lit.value());
Meta::List(metas) if metas.path.is_ident("rename") => {
let nested = metas.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated);
if let Ok(nested) = nested {
for item in nested {
match item {
Meta::NameValue(m) if m.path.is_ident("serialize") => {
if let Ok(lit) = get_lit_str(&m.value) {
res.rename = Some(lit.value());
}
}
_ => continue,
}
_ => continue,
}
}
}
// #[serde(with = "serde_bytes")]
Meta(NameValue(m)) if m.path.is_ident("with") => {
if let syn::Lit::Str(lit) = &m.lit {
Meta::NameValue(m) if m.path.is_ident("with") => {
if let Ok(lit) = get_lit_str(&m.value) {
if lit.value() == "serde_bytes" {
res.with_bytes = true;
}
Expand Down
20 changes: 9 additions & 11 deletions rust/candid_derive/src/func.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::candid_path;
use super::{candid_path, get_lit_str};
use lazy_static::lazy_static;
use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use std::collections::BTreeMap;
use std::sync::Mutex;
use syn::{AttributeArgs, Error, ItemFn, Result, ReturnType, Signature, Type};
use syn::{Error, ItemFn, Meta, Result, ReturnType, Signature, Type};

struct Method {
args: Vec<String>,
Expand All @@ -22,8 +22,8 @@ lazy_static! {
static ref INIT: Mutex<Option<Option<Vec<String>>>> = Mutex::new(Some(Default::default()));
}

pub(crate) fn candid_method(attrs: AttributeArgs, fun: ItemFn) -> Result<TokenStream> {
let attrs = get_candid_attribute(&attrs)?;
pub(crate) fn candid_method(attrs: Vec<Meta>, fun: ItemFn) -> Result<TokenStream> {
let attrs = get_candid_attribute(attrs)?;
let sig = &fun.sig;
if !sig.generics.params.is_empty() {
return Err(Error::new_spanned(
Expand Down Expand Up @@ -193,22 +193,20 @@ struct CandidAttribute {
is_init: bool,
}

fn get_candid_attribute(attrs: &[syn::NestedMeta]) -> Result<CandidAttribute> {
use syn::Meta::{NameValue, Path};
use syn::NestedMeta::Meta;
fn get_candid_attribute(attrs: Vec<Meta>) -> Result<CandidAttribute> {
let mut res = CandidAttribute {
rename: None,
method_type: None,
is_init: false,
};
for attr in attrs.iter() {
for attr in attrs {
match &attr {
Meta(NameValue(m)) if m.path.is_ident("rename") && res.rename.is_none() => {
if let syn::Lit::Str(lit) = &m.lit {
Meta::NameValue(m) if m.path.is_ident("rename") && res.rename.is_none() => {
if let Ok(lit) = get_lit_str(&m.value) {
res.rename = Some(lit.value());
}
}
Meta(Path(p)) if res.method_type.is_none() => {
Meta::Path(p) if res.method_type.is_none() => {
let mode = p.get_ident().unwrap().to_string();
match mode.as_ref() {
"query" | "composite_query" | "update" | "oneway" => {
Expand Down
16 changes: 14 additions & 2 deletions rust/candid_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ pub fn derive_idl_type(input: TokenStream) -> TokenStream {

#[proc_macro_attribute]
pub fn candid_method(attr: TokenStream, item: TokenStream) -> TokenStream {
let attrs = parse_macro_input!(attr as syn::AttributeArgs);
use syn::{parse::Parser, punctuated::Punctuated, Meta, Token};
let attrs = match Punctuated::<Meta, Token![,]>::parse_terminated.parse(attr) {
Ok(attrs) => attrs.into_iter().collect(),
Err(e) => return e.to_compile_error().into(),
};
let fun = parse_macro_input!(item as syn::ItemFn);
func::candid_method(attrs, fun).map_or_else(|e| e.to_compile_error().into(), Into::into)
}
Expand Down Expand Up @@ -48,12 +52,20 @@ pub(crate) fn candid_path(
None => quote::quote! { ::candid },
}
}
pub(crate) fn get_lit_str(expr: &syn::Expr) -> std::result::Result<syn::LitStr, ()> {
if let syn::Expr::Lit(expr) = expr {
if let syn::Lit::Str(lit) = &expr.lit {
return Ok(lit.clone());
}
}
Err(())
}

fn get_custom_candid_path(input: &syn::DeriveInput) -> Result<Option<proc_macro2::TokenStream>> {
let candid_path_helper_attribute_option = input
.attrs
.iter()
.find(|attr| attr.path.is_ident("candid_path"));
.find(|attr| attr.path().is_ident("candid_path"));

match candid_path_helper_attribute_option {
Some(candid_path_helper_attribute) => {
Expand Down

0 comments on commit 5066e27

Please sign in to comment.