Skip to content

Commit a984031

Browse files
authored
Implement record, variant, etc. for the markdown backend. (#484)
* Implement record, variant, etc. for the markdown backend. Implement basic markdown printing for record, variant, union, enum, and flags types. * Remove the `skip_name` parameter and print named types by name.
1 parent 67c7d74 commit a984031

File tree

1 file changed

+37
-36
lines changed

1 file changed

+37
-36
lines changed

crates/gen-markdown/src/lib.rs

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ impl InterfaceGenerator<'_> {
180180
f = func.name.to_snake_case(),
181181
p = name.to_snake_case(),
182182
));
183-
self.print_ty(ty, false);
183+
self.print_ty(ty);
184184
self.push_str("\n");
185185
}
186186
}
@@ -194,7 +194,7 @@ impl InterfaceGenerator<'_> {
194194
f = func.name.to_snake_case(),
195195
p = "result",
196196
));
197-
self.print_ty(ty, false);
197+
self.print_ty(ty);
198198
self.push_str("\n");
199199
}
200200
}
@@ -206,7 +206,7 @@ impl InterfaceGenerator<'_> {
206206
self.gen.src.push_str(s);
207207
}
208208

209-
fn print_ty(&mut self, ty: &Type, skip_name: bool) {
209+
fn print_ty(&mut self, ty: &Type) {
210210
match ty {
211211
Type::Bool => self.push_str("`bool`"),
212212
Type::U8 => self.push_str("`u8`"),
@@ -223,25 +223,23 @@ impl InterfaceGenerator<'_> {
223223
Type::String => self.push_str("`string`"),
224224
Type::Id(id) => {
225225
let ty = &self.resolve.types[*id];
226-
if !skip_name {
227-
if let Some(name) = &ty.name {
228-
self.push_str("[`");
229-
self.push_str(name);
230-
self.push_str("`](#");
231-
self.push_str(&name.to_snake_case());
232-
self.push_str(")");
233-
return;
234-
}
226+
if let Some(name) = &ty.name {
227+
self.push_str("[`");
228+
self.push_str(name);
229+
self.push_str("`](#");
230+
self.push_str(&name.to_snake_case());
231+
self.push_str(")");
232+
return;
235233
}
236234
match &ty.kind {
237-
TypeDefKind::Type(t) => self.print_ty(t, false),
235+
TypeDefKind::Type(t) => self.print_ty(t),
238236
TypeDefKind::Tuple(t) => {
239237
self.push_str("(");
240238
for (i, t) in t.types.iter().enumerate() {
241239
if i > 0 {
242240
self.push_str(", ");
243241
}
244-
self.print_ty(t, false);
242+
self.print_ty(t);
245243
}
246244
self.push_str(")");
247245
}
@@ -250,29 +248,32 @@ impl InterfaceGenerator<'_> {
250248
| TypeDefKind::Enum(_)
251249
| TypeDefKind::Variant(_)
252250
| TypeDefKind::Union(_) => {
253-
unreachable!()
251+
// These types are always named, so we will have
252+
// printed the name above, so we don't need to print
253+
// the contents.
254+
assert!(ty.name.is_some());
254255
}
255256
TypeDefKind::Option(t) => {
256257
self.push_str("option<");
257-
self.print_ty(t, false);
258+
self.print_ty(t);
258259
self.push_str(">");
259260
}
260261
TypeDefKind::Result(r) => match (r.ok, r.err) {
261262
(Some(ok), Some(err)) => {
262263
self.push_str("result<");
263-
self.print_ty(&ok, false);
264+
self.print_ty(&ok);
264265
self.push_str(", ");
265-
self.print_ty(&err, false);
266+
self.print_ty(&err);
266267
self.push_str(">");
267268
}
268269
(None, Some(err)) => {
269270
self.push_str("result<_, ");
270-
self.print_ty(&err, false);
271+
self.print_ty(&err);
271272
self.push_str(">");
272273
}
273274
(Some(ok), None) => {
274275
self.push_str("result<");
275-
self.print_ty(&ok, false);
276+
self.print_ty(&ok);
276277
self.push_str(">");
277278
}
278279
(None, None) => {
@@ -281,13 +282,13 @@ impl InterfaceGenerator<'_> {
281282
},
282283
TypeDefKind::List(t) => {
283284
self.push_str("list<");
284-
self.print_ty(t, false);
285+
self.print_ty(t);
285286
self.push_str(">");
286287
}
287288
TypeDefKind::Future(t) => match t {
288289
Some(t) => {
289290
self.push_str("future<");
290-
self.print_ty(t, false);
291+
self.print_ty(t);
291292
self.push_str(">");
292293
}
293294
None => {
@@ -297,19 +298,19 @@ impl InterfaceGenerator<'_> {
297298
TypeDefKind::Stream(s) => match (s.element, s.end) {
298299
(Some(element), Some(end)) => {
299300
self.push_str("stream<");
300-
self.print_ty(&element, false);
301+
self.print_ty(&element);
301302
self.push_str(", ");
302-
self.print_ty(&end, false);
303+
self.print_ty(&end);
303304
self.push_str(">");
304305
}
305306
(None, Some(end)) => {
306307
self.push_str("stream<_, ");
307-
self.print_ty(&end, false);
308+
self.print_ty(&end);
308309
self.push_str(">");
309310
}
310311
(Some(element), None) => {
311312
self.push_str("stream<");
312-
self.print_ty(&element, false);
313+
self.print_ty(&element);
313314
self.push_str(">");
314315
}
315316
(None, None) => {
@@ -380,7 +381,7 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
380381
format!("{}::{}", name, field.name),
381382
format!("#{}.{}", name.to_snake_case(), field.name.to_snake_case()),
382383
);
383-
self.print_ty(&field.ty, false);
384+
self.print_ty(&field.ty);
384385
self.gen.src.indent(1);
385386
self.push_str("\n\n");
386387
self.docs(&field.docs);
@@ -405,7 +406,7 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
405406
format!("{}::{}", name, i),
406407
format!("#{}.{}", name.to_snake_case(), i),
407408
);
408-
self.print_ty(ty, false);
409+
self.print_ty(ty);
409410
self.push_str("\n");
410411
}
411412
}
@@ -453,7 +454,7 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
453454
);
454455
if let Some(ty) = &case.ty {
455456
self.push_str(": ");
456-
self.print_ty(ty, false);
457+
self.print_ty(ty);
457458
}
458459
self.gen.src.indent(1);
459460
self.push_str("\n\n");
@@ -477,7 +478,7 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
477478
.hrefs
478479
.insert(format!("{name}::{i}"), format!("#{snake}.{i}"));
479480
self.push_str(": ");
480-
self.print_ty(&case.ty, false);
481+
self.print_ty(&case.ty);
481482
self.gen.src.indent(1);
482483
self.push_str("\n\n");
483484
self.docs(&case.docs);
@@ -513,7 +514,7 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
513514
fn type_option(&mut self, id: TypeId, name: &str, payload: &Type, docs: &Docs) {
514515
self.print_type_header(name);
515516
self.push_str("option<");
516-
self.print_ty(payload, false);
517+
self.print_ty(payload);
517518
self.push_str(">");
518519
self.print_type_info(id, docs);
519520
}
@@ -523,19 +524,19 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
523524
match (result.ok, result.err) {
524525
(Some(ok), Some(err)) => {
525526
self.push_str("result<");
526-
self.print_ty(&ok, false);
527+
self.print_ty(&ok);
527528
self.push_str(", ");
528-
self.print_ty(&err, false);
529+
self.print_ty(&err);
529530
self.push_str(">");
530531
}
531532
(None, Some(err)) => {
532533
self.push_str("result<_, ");
533-
self.print_ty(&err, false);
534+
self.print_ty(&err);
534535
self.push_str(">");
535536
}
536537
(Some(ok), None) => {
537538
self.push_str("result<");
538-
self.print_ty(&ok, false);
539+
self.print_ty(&ok);
539540
self.push_str(">");
540541
}
541542
(None, None) => {
@@ -547,7 +548,7 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
547548

548549
fn type_alias(&mut self, id: TypeId, name: &str, ty: &Type, docs: &Docs) {
549550
self.print_type_header(name);
550-
self.print_ty(ty, true);
551+
self.print_ty(ty);
551552
self.push_str("\n\n");
552553
self.print_type_info(id, docs);
553554
self.push_str("\n");

0 commit comments

Comments
 (0)