Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
679ddac
feat: support line comments (wip)
ilbertt Jun 11, 2025
d261a5a
Merge remote-tracking branch 'origin/master' into luca/support-comments
ilbertt Jun 11, 2025
1c99b85
feat: support comments in TS bindings
ilbertt Jun 11, 2025
f47cbb6
fix: typo
ilbertt Jun 11, 2025
f439fe0
test: add comments to parse_idl_prog
ilbertt Jun 11, 2025
f5c2682
feat: support comments in rust bindgen
ilbertt Jun 12, 2025
0083427
feat: support line comments for actors
ilbertt Jun 12, 2025
aa636db
Merge remote-tracking branch 'origin/master' into luca/support-comments
ilbertt Jun 12, 2025
9823791
fix: do not prepend comments in inner types
ilbertt Jun 12, 2025
ee0222d
fix: support multi-line comments, even nested
ilbertt Jun 12, 2025
705f603
feat: support comments from rust
ilbertt Jun 12, 2025
1ae40e6
test: fix comments
ilbertt Jun 12, 2025
0386630
fix: use Rc for comment
ilbertt Jun 13, 2025
3e97d94
refactor: remove unused code
ilbertt Jun 13, 2025
f73994a
refactor: move comment lines around instead of the entire comment string
ilbertt Jun 13, 2025
5d9d8b8
refactor: remove unnecessary types
ilbertt Jun 13, 2025
c0c974d
refactor: remove unnecessary code
ilbertt Jun 13, 2025
57b40ab
refactor: constants
ilbertt Jun 13, 2025
688cabb
refactor: remove unnecessary escape chars
ilbertt Jun 13, 2025
2451fcc
fix: ignore line comments at the end of the line
ilbertt Jun 16, 2025
d65a184
fix: do not ignore comments at the start of the file
ilbertt Jun 16, 2025
e68d8ea
fix: ignore indented isolated comments
ilbertt Jun 16, 2025
ac6bb44
fix: implement common traits for `Type`
ilbertt Jun 17, 2025
5d92e27
fix: traits impls
ilbertt Jun 17, 2025
cb0e67a
refactor: rename common type
ilbertt Jun 17, 2025
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
1 change: 1 addition & 0 deletions rust/candid/src/binary_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ impl ConsType {
let field = Field {
id: Label::Id(f.id).into(),
ty: f.index.to_type(len)?,
comment: None,
};
res.push(field);
}
Expand Down
4 changes: 2 additions & 2 deletions rust/candid/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -923,8 +923,8 @@ impl<'de> de::Deserializer<'de> for &mut Deserializer<'de> {
(TypeInner::Record(ref e), TypeInner::Record(ref w)) => {
match (&e[..], &w[..]) {
(
[Field { id: e_id0, ty: ek }, Field { id: e_id1, ty: ev }],
[Field { id: w_id0, ty: wk }, Field { id: w_id1, ty: wv }],
[Field { id: e_id0, ty: ek, .. }, Field { id: e_id1, ty: ev, .. }],
[Field { id: w_id0, ty: wk, .. }, Field { id: w_id1, ty: wv, .. }],
) if **e_id0 == Label::Id(0)
&& **e_id1 == Label::Id(1)
&& **w_id0 == Label::Id(0)
Expand Down
33 changes: 26 additions & 7 deletions rust/candid/src/pretty/candid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ pub(crate) fn pp_text(id: &str) -> RcDoc {
}

pub fn pp_ty(ty: &Type) -> RcDoc {
pp_ty_inner(ty.as_ref())
pp_ty_inner(ty.as_ref(), ty.comment())
}

pub fn pp_ty_inner(ty: &TypeInner) -> RcDoc {
pub fn pp_ty_inner<'a>(ty: &'a TypeInner, comment: Option<&[String]>) -> RcDoc<'a> {
use TypeInner::*;
match ty {
Null => str("null"),
Expand All @@ -103,7 +103,7 @@ pub fn pp_ty_inner(ty: &TypeInner) -> RcDoc {
Vec(ref t) if matches!(t.as_ref(), Nat8) => str("blob"),
Vec(ref t) => kwd("vec").append(pp_ty(t)),
Record(ref fs) => {
let t = Type(ty.clone().into());
let t = Type::from((ty.clone(), comment));
if t.is_tuple() {
let tuple = concat(fs.iter().map(|f| pp_ty(&f.ty)), ";");
kwd("record").append(enclose_space("{", tuple, "}"))
Expand Down Expand Up @@ -141,7 +141,9 @@ pub(crate) fn pp_field(field: &Field, is_variant: bool) -> RcDoc {
} else {
kwd(" :").append(pp_ty(&field.ty))
};
pp_label(&field.id).append(ty_doc)
pp_comment(field.comment())
.append(pp_label(&field.id))
.append(ty_doc)
}

fn pp_fields(fs: &[Field], is_variant: bool) -> RcDoc {
Expand Down Expand Up @@ -195,7 +197,10 @@ fn pp_service(serv: &[(String, Type)]) -> RcDoc {
TypeInner::Var(_) => pp_ty(func),
_ => unreachable!(),
};
pp_text(id).append(kwd(" :")).append(func_doc)
pp_comment(func.comment())
.append(pp_text(id))
.append(kwd(" :"))
.append(func_doc)
}),
";",
);
Expand All @@ -204,7 +209,8 @@ fn pp_service(serv: &[(String, Type)]) -> RcDoc {

fn pp_defs(env: &TypeEnv) -> RcDoc {
lines(env.0.iter().map(|(id, ty)| {
kwd("type")
pp_comment(ty.comment())
.append(kwd("type"))
.append(ident(id))
.append(kwd("="))
.append(pp_ty(ty))
Expand All @@ -220,6 +226,17 @@ fn pp_actor(ty: &Type) -> RcDoc {
}
}

fn pp_comment(comment_lines: Option<&[String]>) -> RcDoc {
let mut comment_doc = RcDoc::nil();
if let Some(comment_lines) = comment_lines {
for line in comment_lines {
comment_doc =
comment_doc.append(RcDoc::text("// ").append(line).append(RcDoc::hardline()));
}
}
comment_doc
}

pub fn pp_init_args<'a>(env: &'a TypeEnv, args: &'a [ArgType]) -> RcDoc<'a> {
pp_defs(env).append(pp_args(args))
}
Expand All @@ -228,7 +245,9 @@ pub fn compile(env: &TypeEnv, actor: &Option<Type>) -> String {
None => pp_defs(env).pretty(LINE_WIDTH).to_string(),
Some(actor) => {
let defs = pp_defs(env);
let actor = kwd("service :").append(pp_actor(actor));
let actor = pp_comment(actor.comment())
.append(kwd("service :"))
.append(pp_actor(actor));
let doc = defs.append(actor);
doc.pretty(LINE_WIDTH).to_string()
}
Expand Down
5 changes: 2 additions & 3 deletions rust/candid/src/pretty/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ pub fn concat<'a, D>(docs: D, sep: &'a str) -> RcDoc<'a>
where
D: Iterator<Item = RcDoc<'a>> + Clone,
{
RcDoc::intersperse(docs.clone().map(|d| d.append(sep)), RcDoc::line()).flat_alt(
RcDoc::intersperse(docs, RcDoc::text(sep).append(RcDoc::line())),
)
RcDoc::intersperse(docs.clone().map(|d| d.append(sep)), RcDoc::line())
.flat_alt(strict_concat(docs, sep))
}

pub fn lines<'a, D>(docs: D) -> RcDoc<'a>
Expand Down
4 changes: 2 additions & 2 deletions rust/candid/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ impl TypeSerialize {

sleb128_encode(&mut buf, Opcode::Record as i64)?;
leb128_encode(&mut buf, fs.len() as u64)?;
for Field { id, ty } in fs {
for Field { id, ty, .. } in fs {
leb128_encode(&mut buf, u64::from(id.get_id()))?;
self.encode(&mut buf, ty)?;
}
Expand All @@ -316,7 +316,7 @@ impl TypeSerialize {

sleb128_encode(&mut buf, Opcode::Variant as i64)?;
leb128_encode(&mut buf, fs.len() as u64)?;
for Field { id, ty } in fs {
for Field { id, ty, .. } in fs {
leb128_encode(&mut buf, u64::from(id.get_id()))?;
self.encode(&mut buf, ty)?;
}
Expand Down
10 changes: 9 additions & 1 deletion rust/candid/src/types/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,12 @@ macro_rules! map_impl {
Field {
id: Label::Id(0).into(),
ty: K::ty(),
comment: None,
},
Field {
id: Label::Id(1).into(),
ty: V::ty(),
comment: None,
},
]).into();
TypeInner::Vec(tuple).into()
Expand Down Expand Up @@ -276,10 +278,12 @@ where
Field {
id: Label::Named("Ok".to_owned()).into(),
ty: T::ty(),
comment: None,
},
Field {
id: Label::Named("Err".to_owned()).into(),
ty: E::ty(),
comment: None,
},
])
.into()
Expand Down Expand Up @@ -470,7 +474,7 @@ macro_rules! tuple_impls {
{
fn _ty() -> Type {
TypeInner::Record(vec![
$(Field{ id: Label::Id($n).into(), ty: $name::ty() },)+
$(Field{ id: Label::Id($n).into(), ty: $name::ty(), comment: None },)+
]).into()
}
fn idl_serialize<S>(&self, serializer: S) -> Result<(), S::Error>
Expand Down Expand Up @@ -512,10 +516,12 @@ impl CandidType for std::time::SystemTime {
Field {
id: Label::Named("nanos_since_epoch".to_owned()).into(),
ty: u32::ty(),
comment: None,
},
Field {
id: Label::Named("secs_since_epoch".to_owned()).into(),
ty: u64::ty(),
comment: None,
},
])
.into()
Expand Down Expand Up @@ -547,10 +553,12 @@ impl CandidType for std::time::Duration {
Field {
id: Label::Named("secs".to_owned()).into(),
ty: u64::ty(),
comment: None,
},
Field {
id: Label::Named("nanos".to_owned()).into(),
ty: u32::ty(),
comment: None,
},
])
.into()
Expand Down
Loading
Loading