1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
use std::{
    any::Any,
    cell::{self, RefCell},
    fmt,
    rc::{Rc, Weak},
};

/// Helper trait which simplifies generation of the Markdown document represented
/// as a tree of `MdNodeRef`s.
pub(super) trait ToMarkdown {
    /// Drives the generation of the `MdNodeRef` tree by either mutating
    /// the outer (parent) `MdNodeRef`, shared reference to the `MdNode` `node`,
    /// or spawning new child `MdNodeRef` references to nodes.
    fn generate(&self, node: MdNodeRef);
}

/// Interface required for any "content" that is expected to be generated into a
/// Markdown valid format, hence the constraint of `fmt::Display`.
///
/// In essence, any AST element that is meant to be rendered into Markdown, should
/// define a type implementing this trait.
pub(super) trait MdElement: fmt::Display + fmt::Debug + 'static {
    /// Returns `Some(id)` of this `MdElement`. Here `id` is synonym for a Markdown actionable
    /// link.
    fn id(&self) -> Option<&str>;

    /// Returns `Some(docs)`, the "docs" of this `MdElement`.
    fn docs(&self) -> Option<&str>;

    /// Sets `docs`, the "docs" of this `MdElement`.
    fn set_docs(&mut self, docs: &str);

    fn as_any(&self) -> &dyn Any;
    fn as_any_mut(&mut self) -> &mut dyn Any;
}

/// A Markdown node containing:
/// * the Markdown renderable `content`,
/// * a weak reference to the `parent` `MdNode` (if any), and
/// * children `MdNodeRef`s
///
/// `content` is expected to implement the `MdElement` trait.
#[derive(Debug)]
pub(super) struct MdNode {
    content: Box<dyn MdElement>,
    parent: Option<Weak<RefCell<MdNode>>>,
    children: Vec<MdNodeRef>,
}

/// Helper function for walking the tree up from some starting `MdNode`, all the way up
/// to the root of the tree.
fn walk_ancestors(parent: Option<&Weak<RefCell<MdNode>>>, cb: &mut impl FnMut(MdNodeRef)) {
    if let Some(parent) = parent.and_then(|x| x.upgrade()) {
        cb(parent.clone().into());
        walk_ancestors(parent.borrow().parent.as_ref(), cb)
    }
}

impl MdNode {
    fn new<T: MdElement + 'static>(item: T) -> Self {
        Self {
            content: Box::new(item),
            parent: None,
            children: vec![],
        }
    }

    /// Returns all ancestors of this `MdNode` all the way to the tree's root.
    pub fn ancestors(&self) -> Vec<MdNodeRef> {
        let mut ancestors = Vec::new();
        walk_ancestors(self.parent.as_ref(), &mut |parent| ancestors.push(parent));
        ancestors
    }

    /// Returns all children of this `MdNode` in a BFS order.
    pub fn children(&self) -> Vec<MdNodeRef> {
        let mut children = self.children.clone();
        for child in &self.children {
            children.append(&mut child.borrow().children());
        }
        children
    }
}

impl fmt::Display for MdNode {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.content.fmt(f)?;

        for child in &self.children {
            child.fmt(f)?;
        }

        Ok(())
    }
}

/// Helper struct for storing a shared mutable reference to `MdNode`.
#[derive(Debug)]
pub(super) struct MdNodeRef(Rc<RefCell<MdNode>>);

impl MdNodeRef {
    pub fn new<T: MdElement + 'static>(item: T) -> Self {
        Self(Rc::new(RefCell::new(MdNode::new(item))))
    }

    /// Spawns new `MdNode` child node, automatically wrapping it in a
    /// `MdNodeRef` and creating a weak link from child to itself.
    pub fn new_child<T: MdElement + 'static>(&self, item: T) -> Self {
        let mut child_node = MdNode::new(item);
        child_node.parent = Some(Rc::downgrade(&self.0));
        let child_ref = Self(Rc::new(RefCell::new(child_node)));
        self.borrow_mut().children.push(child_ref.clone());
        child_ref
    }

    pub fn borrow(&self) -> cell::Ref<MdNode> {
        self.0.borrow()
    }

    pub fn borrow_mut(&self) -> cell::RefMut<MdNode> {
        self.0.borrow_mut()
    }

    /// Returns an immutable reference to `MdNode`'s `content` as-is, that
    /// is as some type implementing the `MdElement` trait.
    pub fn any_ref(&self) -> cell::Ref<Box<dyn MdElement>> {
        cell::Ref::map(self.borrow(), |b| &b.content)
    }

    /// Returns a mutable reference to `MdNode`'s `content` as-is, that
    /// is as some type implementing the `MdElement` trait.
    pub fn any_ref_mut(&self) -> cell::RefMut<Box<dyn MdElement>> {
        cell::RefMut::map(self.borrow_mut(), |b| &mut b.content)
    }

    /// Returns a mutable reference to `MdNode`'s `content` cast to some type
    /// `T` which implements `MdElement` trait.
    ///
    /// Panics if `content` cannot be downcast to `T`.
    pub fn content_ref_mut<T: MdElement + 'static>(&self) -> cell::RefMut<T> {
        cell::RefMut::map(self.borrow_mut(), |b| {
            let r = b.content.as_any_mut();
            r.downcast_mut::<T>().expect("reference is not T type")
        })
    }
}

impl Clone for MdNodeRef {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

impl From<Rc<RefCell<MdNode>>> for MdNodeRef {
    fn from(node: Rc<RefCell<MdNode>>) -> Self {
        Self(node)
    }
}

impl fmt::Display for MdNodeRef {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.borrow().fmt(f)
    }
}

/// Struct representing the Markdown tree's root.
///
/// Doesn't render to anything.
#[derive(Debug, Default)]
pub(super) struct MdRoot;

impl MdElement for MdRoot {
    fn id(&self) -> Option<&str> {
        None
    }

    fn docs(&self) -> Option<&str> {
        None
    }

    fn set_docs(&mut self, _: &str) {}

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

impl fmt::Display for MdRoot {
    fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result {
        Ok(())
    }
}

/// Helper enum representing either a Markdown header "#" nested at some
/// `level` down the tree, or a bullet "-" in a list which is idempotent
/// to changing the nesting level.
#[derive(Debug, Clone, Copy)]
pub(super) enum MdHeading {
    Header { level: usize },
    Bullet,
}

impl MdHeading {
    /// Creates new instance of `MdHeading::Header` variant nested at some
    /// `level` down the Markdown tree.
    pub fn new_header(level: usize) -> Self {
        MdHeading::Header { level }
    }

    /// Creates new instance of `MdHeading::Bullet` variant.
    pub fn new_bullet() -> Self {
        MdHeading::Bullet
    }

    /// Copies `MdHeading` and if `MdHeading::Header`, pushes it down one
    /// level in the Markdown tree by incrementing `level`.
    pub fn new_level_down(&self) -> Self {
        let mut copy = *self;
        if let Self::Header { ref mut level } = &mut copy {
            *level += 1;
        }
        copy
    }
}

impl fmt::Display for MdHeading {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let as_string = match self {
            Self::Header { level } => "#".repeat(*level),
            Self::Bullet => "-".to_owned(),
        };
        f.write_str(&as_string)
    }
}

/// Struct representing a Markdown section without any `docs`, consisting
/// of only a `header` (e.g., "###"), maybe some referencable `id` (i.e.,
/// a Markdown link), and some `title`.
///
/// Example rendering:
///
/// ### Typenames
///
#[derive(Debug)]
pub(super) struct MdSection {
    pub heading: MdHeading,
    pub id: Option<String>,
    pub title: String,
}

impl MdSection {
    pub fn new<S: AsRef<str>>(heading: MdHeading, title: S) -> Self {
        Self {
            heading,
            id: None,
            title: title.as_ref().to_owned(),
        }
    }
}

impl MdElement for MdSection {
    fn id(&self) -> Option<&str> {
        self.id.as_ref().map(|s| s.as_str())
    }

    fn docs(&self) -> Option<&str> {
        None
    }

    fn set_docs(&mut self, _: &str) {}

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

fn gen_link<S: AsRef<str>>(id: S) -> String {
    format!("<a href=\"#{id}\" name=\"{id}\"></a>", id = id.as_ref())
}

impl fmt::Display for MdSection {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_fmt(format_args!("{} ", self.heading))?;

        if let Some(id) = &self.id {
            f.write_fmt(format_args!("{} ", gen_link(id)))?;
        }

        writeln!(f, "{}", self.title)
    }
}

/// Struct representing a Markdown section representing any `NamedType` element
/// of the AST.
/// Consists of:
/// * `header`, e.g., "###", or "-" for Enum variants, etc.,
/// * referencable `id`,
/// * some `name`, e.g., `errno`,
/// * `docs` paragraph, and
/// * maybe `MdType`.
///
/// Example rendering (recursive):
///
/// ### <a href="#errno" name="errno"></a> `errno`: Enum(`u16`)
/// Error codes returned by...
///
/// #### Variants
/// - `success` No error occurred...
/// - `2big` Argument list too long...
///
#[derive(Debug)]
pub(super) struct MdNamedType {
    pub heading: MdHeading,
    pub id: String,
    pub name: String,
    pub docs: String,
    pub r#type: Option<MdType>,
}

impl MdNamedType {
    pub fn new<S: AsRef<str>>(heading: MdHeading, id: S, name: S, docs: S) -> Self {
        Self {
            heading,
            id: id.as_ref().to_owned(),
            name: name.as_ref().to_owned(),
            docs: docs.as_ref().to_owned(),
            r#type: None,
        }
    }
}

/// Helper struct encapsulating the `TypeRef` value.
// TODO `MdType` should probably store `TypeRef` and recursively
// unwind itself into final `String` representation rather than
// being outright flattened.
#[derive(Debug)]
pub(super) enum MdType {
    Enum { repr: String },
    Int { repr: String },
    Flags { repr: String },
    Struct,
    Union,
    Array { r#type: String },
    Pointer { r#type: String },
    ConstPointer { r#type: String },
    Builtin { repr: String },
    Handle,
    Alias { r#type: String },
}

impl fmt::Display for MdType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Enum { repr } => f.write_fmt(format_args!(": Enum(`{}`)", repr))?,
            Self::Int { repr } => f.write_fmt(format_args!(": Int(`{}`)", repr))?,
            Self::Flags { repr } => f.write_fmt(format_args!(": Flags(`{}`)", repr))?,
            Self::Struct => f.write_fmt(format_args!(": Struct"))?,
            Self::Union => f.write_fmt(format_args!(": Union"))?,
            Self::Array { r#type } => f.write_fmt(format_args!(": `Array<{}>`", r#type))?,
            Self::Pointer { r#type } => f.write_fmt(format_args!(": `Pointer<{}>`", r#type))?,
            Self::ConstPointer { r#type } => {
                f.write_fmt(format_args!(": `ConstPointer<{}>`", r#type))?
            }
            Self::Builtin { repr } => f.write_fmt(format_args!(": `{}`", repr))?,
            Self::Handle => {}
            Self::Alias { r#type } => {
                f.write_fmt(format_args!(": [`{tt}`](#{tt})", tt = r#type))?
            }
        };

        Ok(())
    }
}

impl MdElement for MdNamedType {
    fn id(&self) -> Option<&str> {
        Some(&self.id)
    }

    fn docs(&self) -> Option<&str> {
        Some(&self.docs)
    }

    fn set_docs(&mut self, docs: &str) {
        self.docs = docs.to_owned();
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

impl fmt::Display for MdNamedType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_fmt(format_args!(
            "{heading} {link} `{name}`",
            heading = self.heading,
            link = gen_link(&self.id),
            name = self.name,
        ))?;

        if let Some(tt) = &self.r#type {
            f.write_fmt(format_args!("{}", tt))?;
        }

        writeln!(f, "\n{}", self.docs)
    }
}

/// Struct representing a Markdown section representing any `InterfaceFunc` element
/// of the AST.
/// Consists of:
/// * `header`, e.g., "###",
/// * referencable `id`,
/// * some `name`, e.g., `path_open`,
/// * function `inputs`, i.e., arguments,
/// * function `outputs`, i.e., results, and
/// * `docs` paragraph.
///
/// Example rendering:
///
/// ### <a href="#args_get" name="args_get"></a> Fn args_get(argv: `Pointer<Pointer<u8>>`, ...) -> `errno`
/// Read command-line...
///
/// #### Params
/// - `argv`: `Pointer<Pointer<u8>>` Some docs...
/// - ...
///
/// #### Results
/// - `error`: `errno` Error code...
///
#[derive(Debug)]
pub(super) struct MdFunc {
    pub heading: MdHeading,
    pub id: String,
    pub name: String,
    pub inputs: Vec<(String, String)>,
    pub outputs: Vec<String>,
    pub docs: String,
}

impl MdFunc {
    pub fn new<S: AsRef<str>>(heading: MdHeading, id: S, name: S, docs: S) -> Self {
        Self {
            heading,
            id: id.as_ref().to_owned(),
            name: name.as_ref().to_owned(),
            inputs: vec![],
            outputs: vec![],
            docs: docs.as_ref().to_owned(),
        }
    }
}

impl MdElement for MdFunc {
    fn id(&self) -> Option<&str> {
        Some(&self.id)
    }

    fn docs(&self) -> Option<&str> {
        Some(&self.docs)
    }

    fn set_docs(&mut self, docs: &str) {
        self.docs = docs.to_owned();
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

impl fmt::Display for MdFunc {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        // Expand inputs
        let inputs = self
            .inputs
            .iter()
            .map(|(name, r#type)| format!("{}: {}", name, r#type))
            .collect::<Vec<_>>()
            .join(", ");
        // Expand outputs
        let outputs: Vec<_> = self
            .outputs
            .iter()
            .map(|r#type| format!("{}", r#type))
            .collect();
        let outputs = match outputs.len() {
            0 => "".to_owned(),
            1 => format!(" -> {}", outputs[0]),
            _ => format!(" -> ({})", outputs.join(", ")),
        };
        // Format
        writeln!(f, "\n---\n")?;

        f.write_fmt(format_args!(
            "{heading} {link} `{name}({inputs}){outputs}`",
            heading = self.heading,
            link = gen_link(&self.id),
            name = self.name,
            inputs = inputs,
            outputs = outputs,
        ))?;

        writeln!(f, "\n{}", self.docs)
    }
}