Skip to content

Commit

Permalink
Generate a default Rust binding (#302)
Browse files Browse the repository at this point in the history
* first pass

* actor

* fix

* recursion point

* test

* fix

* fix recursion

* fix semicolon

* impl

* fix

* rename

* didc

* output only defs from actor

* fix
  • Loading branch information
chenyan-dfinity authored Dec 21, 2021
1 parent c477d01 commit cedef7a
Show file tree
Hide file tree
Showing 17 changed files with 852 additions and 10 deletions.
14 changes: 6 additions & 8 deletions rust/candid/src/bindings/analysis.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::parser::typing::TypeEnv;
use crate::types::Type;
use crate::Result;
use std::collections::BTreeSet;

/// Same as chase_actor, with seen set as part of the type. Used for chasing type names from type definitions.
Expand All @@ -8,7 +9,7 @@ pub fn chase_type<'a>(
res: &mut Vec<&'a str>,
env: &'a TypeEnv,
t: &'a Type,
) -> crate::Result<()> {
) -> Result<()> {
use Type::*;
match t {
Var(id) => {
Expand Down Expand Up @@ -47,14 +48,14 @@ pub fn chase_type<'a>(

/// Gather type definitions mentioned in actor, return the non-recursive type names in topological order.
/// Recursive types can appear in any order.
pub fn chase_actor<'a>(env: &'a TypeEnv, actor: &'a Type) -> crate::Result<Vec<&'a str>> {
pub fn chase_actor<'a>(env: &'a TypeEnv, actor: &'a Type) -> Result<Vec<&'a str>> {
let mut seen = BTreeSet::new();
let mut res = Vec::new();
chase_type(&mut seen, &mut res, env, actor)?;
Ok(res)
}

pub fn chase_types<'a>(env: &'a TypeEnv, tys: &'a [Type]) -> crate::Result<Vec<&'a str>> {
pub fn chase_types<'a>(env: &'a TypeEnv, tys: &'a [Type]) -> Result<Vec<&'a str>> {
let mut seen = BTreeSet::new();
let mut res = Vec::new();
for t in tys.iter() {
Expand All @@ -64,18 +65,15 @@ pub fn chase_types<'a>(env: &'a TypeEnv, tys: &'a [Type]) -> crate::Result<Vec<&
}

/// Given a `def_list` produced by the `chase_actor` function, infer which types are recursive
pub fn infer_rec<'a>(
env: &'a TypeEnv,
def_list: &'a [&'a str],
) -> crate::Result<BTreeSet<&'a str>> {
pub fn infer_rec<'a>(env: &'a TypeEnv, def_list: &'a [&'a str]) -> Result<BTreeSet<&'a str>> {
let mut seen = BTreeSet::new();
let mut res = BTreeSet::new();
fn go<'a>(
seen: &mut BTreeSet<&'a str>,
res: &mut BTreeSet<&'a str>,
env: &'a TypeEnv,
t: &'a Type,
) -> crate::Result<()> {
) -> Result<()> {
use Type::*;
match t {
Var(id) => {
Expand Down
1 change: 1 addition & 0 deletions rust/candid/src/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ pub mod analysis;
pub mod candid;
pub mod javascript;
pub mod motoko;
pub mod rust;
pub mod typescript;
Loading

0 comments on commit cedef7a

Please sign in to comment.