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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
#![doc(
    html_root_url = "https://docs.rs/structdoc/0.1.4/structdoc/",
    test(attr(deny(warnings)))
)]
#![forbid(unsafe_code)]
#![warn(missing_docs)]

//! Extract documentation out of types and make use of it at runtime.
//!
//! The [`StructDoc`] trait describes types which know their own documentation at runtime. It can
//! be derived (see the [`StructDoc`] documentation for deriving details). The [`Documentation`] is
//! a type holding the actual documentation.
//!
//! # Motivation
//!
//! Sometimes, an application needs some structured input from the user ‒ configuration, input
//! files, etc. Therefore, the format needs to be documented somehow. But doing so manually has
//! several disadvantages:
//!
//! * Manual documentation tends to be out of sync.
//! * It needs additional manual work.
//! * If parts of the structure come from different parts of the application or even different
//!   libraries, the documentation needs to either be collected from all these places or written
//!   manually at a different place (making the chance of forgetting to update it even higher).
//!
//! This crate tries to help with that ‒ it allows extracting doc strings and composing them
//! together to form the documentation automatically, using procedural derive. The structure is
//! guaranteed to match and the documentation strings are much more likely to be updated, as they
//! are close to the actual definitions being changed.
//!
//! It is able to use both its own and [`serde`]'s attributes, because [`serde`] is very commonly
//! used to read the structured data.
//!
//! # Examples
//!
//! ```rust
//! # #![allow(dead_code)]
//! use std::num::NonZeroU32;
//!
//! use serde_derive::Deserialize;
//! use structdoc::StructDoc;
//!
//! #[derive(Deserialize, StructDoc)]
//! struct Point {
//!     /// The horizontal position.
//!     x: i32,
//!
//!     /// The vertical position.
//!     y: i32,
//! }
//!
//! #[derive(Deserialize, StructDoc)]
//! struct Circle {
//!     // Will flatten both on the serde side and structdoc, effectively creating a structure with
//!     // 3 fields for both of them.
//!     #[serde(flatten)]
//!     center: Point,
//!
//!     /// The diameter of the circle.
//!     diameter: NonZeroU32,
//! }
//!
//! println!("{}", Circle::document());
//! ```
//!
//! # TODO
//!
//! This crate is young and has some missing things:
//!
//! * Probably some corner-cases are not handled properly. Also, not everything that can derive
//!   [`Deserialize`] can derive [`StructDoc`] yet.
//! * Some ability to manually traverse the documentation.
//! * Allow tweaking how the documentation is printed.
//! * Proper tests.
//! * Error handling during derive ‒ the error messages would need some improvements and some
//!   things are simply ignored. Furthermore, if you specify some nonsensical combination of
//!   attributes, you're as likely to get some garbage documentation out instead of error.
//! * There are plans to provide implementations for types from other crates, under feature flags.
//!
//! In other words, let this crate generate the documentation, but skim the result before shipping
//! to make sure it is correct and makes sense. Pull requests to fix bugs are indeed welcome.
//!
//! [`serde`]: https://serde.rs
//! [`Deserialize`]: https://docs.rs/serde/~1/serde/trait.Deserialize.html

use std::borrow::Cow;
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::mem;

use itertools::Itertools;

mod impls;

use bitflags::bitflags;

#[cfg(feature = "structdoc-derive")]
pub use structdoc_derive::StructDoc;

/// Text representation.
///
/// Many things inside here can take either owned strings or string literals.
pub type Text = Cow<'static, str>;

bitflags! {
    /// Flags on nodes of [`Documentation`].
    ///
    /// Can be put onto a documentation node with [`Documentation::set_flag`].
    pub struct Flags: u8 {
        /// Flatten structure into a parent.
        ///
        /// For structure field inside a structure, this skips the one level and puts all the inner
        /// fields directly inside the outer struct.
        ///
        /// For enums inside structs, this suggests that the fields are merged inline the outer
        /// struct, but still keeps the separation inside the documentation.
        const FLATTEN  = 0b0001;

        /// This part of documentation should be hidden.
        const HIDE     = 0b0010;

        /// The presence of this field is optional.
        ///
        /// This may be caused either by it to reasonably contain a no-value (eg. `Option<T>`,
        /// `Vec<T>`) or by having a default value. Any possible default value should be described
        /// in the doc comment.
        const OPTIONAL = 0b0100;
    }
}

bitflags! {
    #[derive(Default)]
    struct Processing: u8 {
        const SORT    = 0b0000_0001;
        const HIDE    = 0b0000_0010;
        const FLATTEN = 0b0000_0100;
        const STRUCT  = 0b0000_1000;
        const ENUM    = 0b0001_0000;
    }
}

/// An arity of an container.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Arity {
    /// Contains one thing.
    ///
    /// Or, at most one, in case it is also optional.
    One,

    /// Multiple things of the same kind, preserving order.
    ManyOrdered,

    /// Multiple things of the same kind, without specified order.
    ManyUnordered,
}

/// A tagging of an enum.
///
/// Corresponds to the [serde enum representations](https://serde.rs/enum-representations.html).
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Tagging {
    #[allow(missing_docs)]
    Untagged,

    #[allow(missing_docs)]
    External,

    #[allow(missing_docs)]
    Internal { tag: String },

    #[allow(missing_docs)]
    Adjacent { tag: String, content: String },
}

#[derive(Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
struct Entry {
    caption: String,
    text: Vec<String>,
    flags: Vec<Text>,
    sub: Vec<Entry>,
    processing: Processing,
}

impl Entry {
    fn sort(&mut self) {
        for sub in &mut self.sub {
            sub.sort();
        }
        if self.processing.contains(Processing::SORT) {
            self.sub.sort();
        }
    }

    fn print(&self, fmt: &mut Formatter, indent: &mut String) -> FmtResult {
        let flags = if self.flags.is_empty() {
            String::new()
        } else {
            let space = if self.caption.is_empty() { "" } else { " " };
            format!("{}({})", space, self.flags.iter().rev().join(", "))
        };
        let colon = if self.text.is_empty() && self.sub.is_empty() {
            ' '
        } else {
            ':'
        };
        if indent.len() >= 2 {
            indent.truncate(indent.len() - 2);
            indent.push_str("* ");
        }
        writeln!(fmt, "{}{}{}{}", indent, self.caption, flags, colon)?;
        if indent.len() >= 2 {
            indent.truncate(indent.len() - 2);
            indent.push_str("  ");
        }
        indent.push_str("| ");
        for line in &self.text {
            writeln!(fmt, "{}{}", indent, line)?;
        }
        indent.truncate(indent.len() - 2);
        indent.push_str("    ");
        for sub in &self.sub {
            sub.print(fmt, indent)?;
        }
        assert!(indent.len() >= 4);
        indent.truncate(indent.len() - 4);
        Ok(())
    }

    fn is_empty(&self) -> bool {
        self.caption.is_empty() && self.text.is_empty() && self.sub.is_empty()
    }
}

/// A documentation node with actual documentation text.
#[derive(Clone, Debug)]
pub struct Field {
    doc: Text,
    node: Node,
}

impl Field {
    /// Creates a field from (undocumented) documentation node and the documentation text.
    ///
    /// This is the proper way to add descriptions to struct fields and enum variants.
    pub fn new(inner: Documentation, doc: impl Into<Text>) -> Self {
        Field {
            doc: doc.into(),
            node: inner.0,
        }
    }

    fn entry(&self, prefix: &str, name: &str) -> Entry {
        let mut entry = self.node.entry();
        if !self.doc.is_empty() {
            entry.text.extend(self.doc.lines().map(str::to_owned));
        }
        entry.caption = format!("{}{}", prefix, name);
        entry
    }
}

#[derive(Clone, Debug)]
enum Node {
    Leaf(Text),
    Wrapper {
        child: Box<Node>,
        arity: Arity,
        flags: Flags,
    },
    Map {
        key: Box<Node>,
        value: Box<Node>,
    },
    Struct(Vec<(Text, Field)>),
    Enum {
        variants: Vec<(Text, Field)>,
        tagging: Tagging,
    },
}

impl Node {
    fn set_flag(&mut self, flag: Flags) {
        if let Node::Wrapper { ref mut flags, .. } = self {
            *flags |= flag;
        } else {
            let mut old = Node::Leaf(Text::default());
            mem::swap(&mut old, self);
            *self = Node::Wrapper {
                child: Box::new(old),
                flags: flag,
                arity: Arity::One,
            };
        }
    }

    fn struct_from<'i, I>(fields: I) -> Entry
    where
        I: IntoIterator<Item = &'i (Text, Field)>,
    {
        let mut sub = Vec::new();
        for (name, field) in fields {
            let mut entry = field.entry("Field ", name);
            if entry.processing.contains(Processing::FLATTEN)
                && entry.processing.contains(Processing::ENUM)
            {
                entry.flags.push("Inlined to parent".into());
            }
            if entry.processing.contains(Processing::HIDE) {
                continue;
            } else if entry.processing.contains(Processing::FLATTEN)
                && entry.processing.contains(Processing::STRUCT)
            {
                sub.extend(entry.sub);
            } else {
                sub.push(entry);
            }
        }

        Entry {
            caption: String::new(),
            text: Vec::new(),
            flags: vec!["Struct".into()],
            sub,
            processing: Processing::SORT | Processing::STRUCT,
        }
    }

    fn entry(&self) -> Entry {
        match self {
            Node::Leaf(ty) => {
                let flags = if ty.is_empty() {
                    Vec::new()
                } else {
                    vec![ty.clone()]
                };
                Entry {
                    flags,
                    ..Entry::default()
                }
            }
            Node::Wrapper {
                child,
                flags,
                arity,
            } => {
                let mut child_entry = child.entry();
                match arity {
                    Arity::One => (),
                    Arity::ManyOrdered => child_entry.flags.push("Array".into()),
                    Arity::ManyUnordered => child_entry.flags.push("Set".into()),
                }
                if flags.contains(Flags::OPTIONAL) {
                    child_entry.flags.push("Optional".into());
                }
                if flags.contains(Flags::FLATTEN) && *arity == Arity::One {
                    child_entry.processing |= Processing::FLATTEN;
                }
                if flags.contains(Flags::HIDE) {
                    child_entry.processing |= Processing::HIDE;
                }
                child_entry
            }
            Node::Map { key, value } => {
                let mut entry = Entry::default();
                entry.text.push("Map:".to_owned());
                let mut key = key.entry();
                if !key.is_empty() {
                    key.caption = "Keys:".to_owned();
                    entry.sub.push(key);
                }
                let mut value = value.entry();
                if !value.is_empty() {
                    value.caption = "Values:".to_owned();
                    entry.sub.push(value);
                }
                entry
            }
            Node::Struct(fields) => Self::struct_from(fields),
            Node::Enum { variants, tagging } => {
                let mut variants = variants
                    .iter()
                    .map(|(name, variant)| variant.entry("Variant ", name))
                    .filter(|entry| !entry.processing.contains(Processing::HIDE))
                    .collect::<Vec<_>>();
                let (ty, flags, cap) = match tagging {
                    Tagging::Untagged => {
                        for (num, variant) in variants.iter_mut().enumerate() {
                            variant.caption = format!("Variant #{}", num + 1);
                        }
                        (
                            "Anonymous alternatives (inline structs to parent level)",
                            Processing::empty(),
                            String::new(),
                        )
                    }
                    Tagging::External => ("One-of", Processing::empty(), String::new()),
                    Tagging::Internal { tag } => (
                        "Alternatives (inline other fields)",
                        Processing::empty(),
                        format!("Field {}", tag),
                    ),
                    Tagging::Adjacent { tag, content } => {
                        for (num, var) in variants.iter_mut().enumerate() {
                            let cap = var.caption.replacen("Variant ", "Constant ", 1);
                            let mut old_text = Vec::new();
                            mem::swap(&mut old_text, &mut var.text);
                            var.caption = format!("Field {}", content);
                            var.text = Vec::new();
                            let tag_field = Entry {
                                caption: cap,
                                text: Vec::new(),
                                flags: vec!["Variant selector".into()],
                                sub: Vec::new(),
                                processing: Processing::empty(),
                            };
                            let mut tmp = Entry::default();
                            mem::swap(&mut tmp, var);
                            *var = Entry {
                                caption: format!("Variant #{}", num + 1),
                                text: old_text,
                                flags: vec!["Struct".into()],
                                sub: vec![tag_field, tmp],
                                processing: Processing::STRUCT,
                            };
                        }
                        ("Alternatives", Processing::empty(), tag.clone())
                    }
                };
                let inner = Entry {
                    caption: cap,
                    text: Vec::new(),
                    flags: vec![ty.into()],
                    sub: variants,
                    processing: flags | Processing::ENUM,
                };
                if inner.sub.iter().all(|sub| sub.sub.is_empty()) {
                    inner
                } else {
                    Entry {
                        caption: String::new(),
                        text: Vec::new(),
                        flags: vec!["Struct".into()],
                        sub: vec![inner],
                        processing: Processing::STRUCT,
                    }
                }
            }
        }
    }
}

/// A representation of documentation.
///
/// This carries the internal representation (tree) of a documentation. Note that currently this
/// does not support cycles or referencing other branches.
///
/// This can be either queried by the [`StructDoc`] trait, or manually constructed (which might be
/// needed in a manual implementation of the trait).
///
/// # TODO
///
/// Currently, the documentation can be formatted both with the [`Debug`][std::fmt::Debug] and
/// [`Display`][std::fmt::Display] traits, but doesn't offer any kind of customization. In the
/// future it should be possible to both traverse the structure manually and to customize the way
/// the documentation is formatted.
#[derive(Clone, Debug)]
pub struct Documentation(Node);

impl Documentation {
    /// Creates a leaf node of the documentation, without any description.
    pub fn leaf_empty() -> Documentation {
        Documentation(Node::Leaf(Text::default()))
    }

    /// Creates a leaf node with the given type.
    ///
    /// Note that an empty `ty` is equivalent to the [`leaf_empty`][Documentation::leaf_empty].
    pub fn leaf(ty: impl Into<Text>) -> Documentation {
        Documentation(Node::Leaf(ty.into()))
    }

    /// Adds a flag to this documentation node.
    pub fn set_flag(&mut self, flag: Flags) {
        self.0.set_flag(flag);
    }

    /// Wraps a node into an array or a set.
    ///
    /// This describes a homogeneous collection.
    pub fn with_arity(self, arity: Arity) -> Self {
        Documentation(Node::Wrapper {
            child: Box::new(self.0),
            arity,
            flags: Flags::empty(),
        })
    }

    /// Builds a map.
    ///
    /// Joins documentation of keys and values into a map. Note that all the keys and all the
    /// values are of the same type ‒ for heterogeneous things, you might want structs or enums.
    pub fn map(key: Documentation, value: Documentation) -> Self {
        Documentation(Node::Map {
            key: Box::new(key.0),
            value: Box::new(value.0),
        })
    }

    /// Builds a struct.
    ///
    /// Builds a structure, provided a list of fields.
    ///
    /// The iterator should yield pairs of (name, field).
    pub fn struct_(fields: impl IntoIterator<Item = (impl Into<Text>, Field)>) -> Self {
        Documentation(Node::Struct(
            fields.into_iter().map(|(t, f)| (t.into(), f)).collect(),
        ))
    }

    /// Builds an enum.
    ///
    /// Builds an enum from provided list of fields. The fields may be either leaves (without
    /// things inside ‒ created with eg. [`leaf_empty`][Documentation::leaf_empty]), newtypes
    /// (other leaves) or structs. The iterator should yield pairs of (name, variant).
    ///
    /// See the [serde documentation about enum
    /// representations](https://serde.rs/enum-representations.html) for `tagging`.
    pub fn enum_(
        variants: impl IntoIterator<Item = (impl Into<Text>, Field)>,
        tagging: Tagging,
    ) -> Self {
        Documentation(Node::Enum {
            variants: variants.into_iter().map(|(t, f)| (t.into(), f)).collect(),
            tagging,
        })
    }
}

impl Display for Documentation {
    fn fmt(&self, fmt: &mut Formatter) -> FmtResult {
        let mut entry = self.0.entry();
        entry.sort();
        entry.caption = "<root>".to_owned();
        let mut indent = String::new();
        entry.print(fmt, &mut indent)
    }
}

/// Types that can provide their own documentation at runtime.
///
/// It is provided for basic types and containers in the standard library. It should be possible to
/// derive for most of the rest.
///
/// # Examples
///
/// ```
/// # #![allow(dead_code)]
/// use structdoc::StructDoc;
///
/// #[derive(StructDoc)]
/// struct Point {
///     /// The horizontal coordinate.
///     x: i32,
///
///     /// The vertical coordinate.
///     y: i32,
/// }
///
/// let documentation = format!("{}", Point::document());
/// let expected = r#"<root> (Struct):
///   * Field x (Integer):
///     | The horizontal coordinate.
///   * Field y (Integer):
///     | The vertical coordinate.
/// "#;
///
/// assert_eq!(expected, documentation);
/// ```
///
/// # Deriving the trait
///
/// If the `structdoc-derive` feature is enabled (it is by default), it is possible to derive the
/// trait on structs and enums. The text of documentation is extracted from the doc comments.
/// Furthermore, it allows tweaking the implementation by attributes.
///
/// Because the primary aim of this crate is to provide user documentation for things fed to the
/// application a lot of such things are handled by the [`serde`] crate, our derive can use both
/// its own attributes and `serde` ones where it makes sense.
///
/// ## Ignoring fields and variants
///
/// They can be ignored by placing either `#[doc(hidden)]`, `#[serde(skip)]`,
/// `#[serde(skip_deserialize)]` or `#[structdoc(skip)]` attributes on them.
///
/// ## Stubbing out implementations
///
/// If a field's type doesn't implement the trait or if recursing into it is not wanted (or maybe
/// because the data structure is cyclic), it can be prefixed with the `#[structdoc(leaf)]` or
/// `#[structdoc(leag = "Type")]` attribute. It'll provide trivial implementation without any
/// explanation and the provided type in parenthesis, if one is provided.
///
/// Alternatively, a function `fn() -> Documentation` can be plugged in using the
/// `#[structdoc(with = "path::to::the_fn")]`. That can return an arbitrary implementation.
///
/// ## Renaming things
///
/// The `rename` and `rename_all` attributes are available, both in `serde` and `structdoc`
/// variants. They have the same meaning as withing serde.
///
/// ## Flattening
///
/// The `#[serde(flatten)]` and `#[structdoc(flatten)]` flattens structures inline.
///
/// ## Enum representations
///
/// The serde (and `structdoc` alternatives) of [tag representation] attributes are available.
///
/// [`serde`]: https://crates.io/crates/serde
/// [`tag representation]: https://serde.rs/container-attrs.html#tag
pub trait StructDoc {
    /// Returns the documentation for the type.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use structdoc::StructDoc;
    ///
    /// println!("Documentation: {}", Vec::<Option<String>>::document());
    /// ```
    fn document() -> Documentation;
}