Skip to content
Draft
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
48 changes: 25 additions & 23 deletions rust/candid_parser/src/bindings/motoko.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn pp_ty_rich<'a>(ty: &'a Type, syntax: Option<&'a IDLType>) -> RcDoc<'a> {
pp_service(meths, Some(methods))
}
(TypeInner::Class(ref args, t), Some(IDLType::ClassT(_, syntax_t))) => {
pp_class((args, t), Some(syntax_t))
pp_class((args, t), Some(&syntax_t.kind))
}
(TypeInner::Record(ref fields), Some(IDLType::RecordT(syntax_fields))) => {
pp_record(fields, Some(syntax_fields))
Expand All @@ -111,9 +111,11 @@ fn pp_ty_rich<'a>(ty: &'a Type, syntax: Option<&'a IDLType>) -> RcDoc<'a> {
pp_variant(fields, Some(syntax_fields))
}
(TypeInner::Opt(ref inner), Some(IDLType::OptT(syntax))) => {
str("?").append(pp_ty_rich(inner, Some(syntax)))
str("?").append(pp_ty_rich(inner, Some(&syntax.kind)))
}
(TypeInner::Vec(ref inner), Some(IDLType::VecT(syntax))) => {
pp_vec(inner, Some(&syntax.kind))
}
(TypeInner::Vec(ref inner), Some(IDLType::VecT(syntax))) => pp_vec(inner, Some(syntax)),
(_, _) => pp_ty(ty),
}
}
Expand Down Expand Up @@ -212,7 +214,7 @@ fn pp_service<'a>(serv: &'a [(String, Type)], syntax: Option<&'a [syntax::Bindin
if let Some(bs) = syntax {
if let Some(b) = bs.iter().find(|b| &b.id == id) {
docs = pp_docs(&b.docs);
syntax_field_ty = Some(&b.typ)
syntax_field_ty = Some(&b.typ.kind)
}
}
docs.append(escape(id, true))
Expand Down Expand Up @@ -244,7 +246,7 @@ fn find_field<'a>(
if let Some(bs) = fields {
if let Some(field) = bs.iter().find(|b| b.label == *label) {
docs = pp_docs(&field.docs);
syntax_field_ty = Some(&field.typ);
syntax_field_ty = Some(&field.typ.kind);
}
};
(docs, syntax_field_ty)
Expand Down Expand Up @@ -302,7 +304,7 @@ fn pp_defs<'a>(env: &'a TypeEnv, prog: &'a IDLMergedProg) -> RcDoc<'a> {
docs.append(kwd("public type"))
.append(escape(id, false))
.append(" = ")
.append(pp_ty_rich(ty, syntax.map(|b| &b.typ)))
.append(pp_ty_rich(ty, syntax.map(|b| &b.typ.kind)))
.append(";")
}))
}
Expand All @@ -311,25 +313,25 @@ fn pp_actor<'a>(ty: &'a Type, syntax: Option<&'a IDLActorType>) -> RcDoc<'a> {
let self_doc = kwd("public type Self =");
match ty.as_ref() {
TypeInner::Service(ref serv) => match syntax {
Some(IDLActorType {
typ: IDLType::ServT(ref fields),
docs,
}) => {
let docs = pp_docs(docs);
docs.append(self_doc).append(pp_service(serv, Some(fields)))
}
_ => pp_service(serv, None),
Some(IDLActorType { typ, docs, .. }) => match &typ.kind {
IDLType::ServT(fields) => {
let docs = pp_docs(docs);
docs.append(self_doc).append(pp_service(serv, Some(fields)))
}
_ => pp_service(serv, None),
},
None => pp_service(serv, None),
},
TypeInner::Class(ref args, ref t) => match syntax {
Some(IDLActorType {
typ: IDLType::ClassT(_, syntax_t),
docs,
}) => {
let docs = pp_docs(docs);
docs.append(self_doc)
.append(pp_class((args, t), Some(syntax_t)))
}
_ => self_doc.append(pp_class((args, t), None)),
Some(IDLActorType { typ, docs, .. }) => match &typ.kind {
IDLType::ClassT(_, syntax_t) => {
let docs = pp_docs(docs);
docs.append(self_doc)
.append(pp_class((args, t), Some(&syntax_t.kind)))
}
_ => self_doc.append(pp_class((args, t), None)),
},
None => self_doc.append(pp_class((args, t), None)),
},
TypeInner::Var(_) => self_doc.append(pp_ty(ty)),
_ => unreachable!(),
Expand Down
18 changes: 10 additions & 8 deletions rust/candid_parser/src/bindings/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ fn find_field<'a>(
if let Some(bs) = fields {
if let Some(field) = bs.iter().find(|b| b.label == *label) {
docs = pp_docs(&field.docs);
syntax_field_ty = Some(&field.typ);
syntax_field_ty = Some(&field.typ.kind);
}
};
(docs, syntax_field_ty)
Expand Down Expand Up @@ -178,7 +178,7 @@ fn actor_methods(actor: Option<&IDLActorType>) -> &[syntax::Binding] {
None => return &[],
};

match typ {
match &typ.kind {
IDLType::ServT(methods) => methods,
IDLType::ClassT(_, inner) => {
if let IDLType::ServT(methods) = inner.as_ref() {
Expand Down Expand Up @@ -482,7 +482,7 @@ fn test_{test_name}() {{
.unwrap_or_else(|| to_identifier_case(id, IdentifierCase::UpperCamel).0);
let syntax = self.prog.lookup(id);
let syntax_ty = syntax
.map(|b| &b.typ)
.map(|b| &b.typ.kind)
.or_else(|| self.generated_types.get(*id).map(|t| &**t));
let docs = syntax
.map(|b| pp_docs(b.docs.as_ref()))
Expand Down Expand Up @@ -958,8 +958,9 @@ impl<'b> NominalState<'_, 'b> {
let elem = StateElem::Label(&lab);
let old = self.state.push_state(&elem);
path.push(TypePath::RecordField(id.to_string()));
let syntax_field = syntax_fields
.and_then(|s| s.iter().find(|f| f.label == **id).map(|f| &f.typ));
let syntax_field = syntax_fields.and_then(|s| {
s.iter().find(|f| f.label == **id).map(|f| &f.typ.kind)
});
let ty = self.nominalize(env, path, ty, syntax_field);
path.pop();
self.state.pop_state(old, elem);
Expand Down Expand Up @@ -1003,8 +1004,9 @@ impl<'b> NominalState<'_, 'b> {
} else {
path.push(TypePath::VariantField(id.to_string()));
}
let syntax_field = syntax_fields
.and_then(|s| s.iter().find(|f| f.label == **id).map(|f| &f.typ));
let syntax_field = syntax_fields.and_then(|s| {
s.iter().find(|f| f.label == **id).map(|f| &f.typ.kind)
});
let ty = self.nominalize(env, path, ty, syntax_field);
path.pop();
self.state.pop_state(old, StateElem::Label(&lab));
Expand Down Expand Up @@ -1167,7 +1169,7 @@ impl<'b> NominalState<'_, 'b> {
for (id, ty) in self.state.env.0.iter() {
let elem = StateElem::Label(id);
let old = self.state.push_state(&elem);
let syntax = prog.lookup(id).map(|t| &t.typ);
let syntax = prog.lookup(id).map(|t| &t.typ.kind);
let ty = self.nominalize(&mut res, &mut vec![TypePath::Id(id.clone())], ty, syntax);
res.0.insert(id.to_string(), ty);
self.state.pop_state(old, elem);
Expand Down
24 changes: 14 additions & 10 deletions rust/candid_parser/src/bindings/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn find_field<'a>(
if let Some(bs) = fields {
if let Some(field) = bs.iter().find(|b| b.label == *label) {
docs = pp_docs(&field.docs);
syntax_field_ty = Some(&field.typ);
syntax_field_ty = Some(&field.typ.kind);
}
};
(docs, syntax_field_ty)
Expand All @@ -40,10 +40,10 @@ fn pp_ty_rich<'a>(
pp_service(env, serv, Some(syntax_serv))
}
(TypeInner::Opt(ref t), Some(IDLType::OptT(syntax_inner))) => {
pp_opt(env, t, Some(syntax_inner), is_ref)
pp_opt(env, t, Some(&syntax_inner.kind), is_ref)
}
(TypeInner::Vec(ref t), Some(IDLType::VecT(syntax_inner))) => {
pp_vec(env, t, Some(syntax_inner), is_ref)
pp_vec(env, t, Some(&syntax_inner.kind), is_ref)
}
(_, _) => pp_ty(env, ty, is_ref),
}
Expand Down Expand Up @@ -266,7 +266,7 @@ fn pp_defs<'a>(env: &'a TypeEnv, def_list: &'a [&'a str], prog: &'a IDLMergedPro
lines(def_list.iter().map(|id| {
let ty = env.find_type(id).unwrap();
let syntax = prog.lookup(id);
let syntax_ty = syntax.map(|s| &s.typ);
let syntax_ty = syntax.map(|s| &s.typ.kind);
let docs = syntax
.map(|b| pp_docs(b.docs.as_ref()))
.unwrap_or(RcDoc::nil());
Expand Down Expand Up @@ -309,7 +309,7 @@ fn pp_actor<'a>(env: &'a TypeEnv, ty: &'a Type, syntax: Option<&'a IDLType>) ->
.append(str(" {}")),
TypeInner::Class(_, t) => {
if let Some(IDLType::ClassT(_, syntax_t)) = syntax {
pp_actor(env, t, Some(syntax_t))
pp_actor(env, t, Some(&syntax_t.kind))
} else {
pp_actor(env, t, None)
}
Expand All @@ -333,11 +333,15 @@ import type { IDL } from '@dfinity/candid';
.as_ref()
.map(|s| pp_docs(s.docs.as_ref()))
.unwrap_or(RcDoc::nil());
docs.append(pp_actor(env, actor, syntax_actor.as_ref().map(|s| &s.typ)))
.append(RcDoc::line())
.append("export declare const idlFactory: IDL.InterfaceFactory;")
.append(RcDoc::line())
.append("export declare const init: (args: { IDL: typeof IDL }) => IDL.Type[];")
docs.append(pp_actor(
env,
actor,
syntax_actor.as_ref().map(|s| &s.typ.kind),
))
.append(RcDoc::line())
.append("export declare const idlFactory: IDL.InterfaceFactory;")
.append(RcDoc::line())
.append("export declare const init: (args: { IDL: typeof IDL }) => IDL.Type[];")
}
};
let doc = RcDoc::text(header)
Expand Down
Loading
Loading