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
//! Macros used by `toad` for boilerplate reduction

#![doc(html_root_url = "https://docs.rs/toad-macros/0.2.0")]
#![cfg_attr(all(not(test), feature = "no_std"), no_std)]
#![cfg_attr(not(test), forbid(missing_debug_implementations, unreachable_pub))]
#![cfg_attr(not(test), deny(unsafe_code, missing_copy_implementations))]
#![cfg_attr(any(docsrs, feature = "docs"), feature(doc_cfg))]
#![deny(missing_docs)]

use proc_macro::TokenStream;
use quote::ToTokens;
use regex::Regex;
use syn::parse::Parse;
use syn::{parse_macro_input, LitStr};

struct DocSection(LitStr);

impl Parse for DocSection {
  fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
    Ok(Self(input.parse::<LitStr>()?))
  }
}

const RFC7252: &str = include_str!("./rfc7252.txt");

/// Give me a section of RFC7252 (e.g. `5.9.1.1` no trailing dot)
/// and I will scrape the rfc for that section then yield an inline `#[doc]` attribute containing that section.
///
/// ```
/// use toad_macros::rfc_7252_doc;
///
/// #[doc = rfc_7252_doc!("5.9.1.1")]
/// // Expands to:
/// /// # 2.04 Changed
/// /// [_generated from RFC7252 section 5.9.1.1_](<link to section at ietf.org>)
/// ///
/// /// This Response Code is like HTTP 204 "No Content" but only used in
/// /// response to POST and PUT requests.  The payload returned with the
/// /// response, if any, is a representation of the action result.
/// ///
/// /// This response is not cacheable.  However, a cache MUST mark any
/// /// stored response for the changed resource as not fresh.
/// struct Foo;
/// ```
#[proc_macro]
pub fn rfc_7252_doc(input: TokenStream) -> TokenStream {
  let DocSection(section_literal) = parse_macro_input!(input as DocSection);

  let sec = section_literal.value();
  let docstring = gen_docstring(sec, RFC7252);

  LitStr::new(&docstring, section_literal.span()).to_token_stream()
                                                 .into()
}

fn gen_docstring(sec: String, rfc: &'static str) -> String {
  // Match {beginning of line}{section number} then capture everything until beginning of next section
  let section_rx =
    Regex::new(format!(r"(?s)\n{}\.\s+(.*?)(\n\d|$)", sec.replace('.', "\\.")).as_str()).unwrap_or_else(|e| {
                                                                                      panic!("Section {} invalid: {:?}", sec, e)
                                                                                    });
  let rfc_section = section_rx.captures_iter(rfc)
                              .next()
                              .unwrap_or_else(|| panic!("Section {} not found", sec))
                              .get(1)
                              .unwrap_or_else(|| panic!("Section {} is empty", sec))
                              .as_str();

  let mut lines = trim_leading_ws(rfc_section);
  let line1 = lines.drain(0..1)
                   .next()
                   .unwrap_or_else(|| panic!("Section {} is empty", sec));
  let rest = lines.join("\n");

  format!(
          r"# {title}
[_generated from RFC7252 section {section}_](https://datatracker.ietf.org/doc/html/rfc7252#section-{section})

{body}",
          title = line1,
          section = sec,
          body = rest
  )
}

/// the RFC is formatted with 3-space indents in section bodies, with some addl
/// indentation on some text.
///
/// This strips all leading whitespaces, except within code fences (&#96;&#96;&#96;), where it just trims the 3-space indent.
///
/// Returns the input string split by newlines
fn trim_leading_ws(text: &str) -> Vec<String> {
  #[derive(Clone, Copy)]
  enum TrimStart {
    Yes,
    InCodeFence,
  }

  let trim_start = Regex::new(r"^ +").unwrap();
  let trim_indent = Regex::new(r"^   ").unwrap();

  text.split('\n')
      .fold((Vec::<String>::new(), TrimStart::Yes),
            |(mut lines, strip), s| {
              let trimmed = trim_start.replace(s, "").to_string();
              let dedented = trim_indent.replace(s, "").to_string();

              let is_fence = trimmed.starts_with("```");

              match (is_fence, strip) {
                | (false, TrimStart::Yes) => {
                  lines.push(trimmed);
                  (lines, strip)
                },
                | (false, TrimStart::InCodeFence) => {
                  lines.push(dedented);
                  (lines, strip)
                },
                | (true, TrimStart::Yes) => {
                  lines.push(trimmed);
                  (lines, TrimStart::InCodeFence)
                },
                | (true, TrimStart::InCodeFence) => {
                  lines.push(trimmed);
                  (lines, TrimStart::Yes)
                },
              }
            })
      .0
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn rfcdoc_works() {
    let rfc = r"
Table of Contents

   1.  Foo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
     1.1.  Bingus .  . . . . . . . . . . . . . . . . . . . . . . . . . 2
     1.2.  Terminology . . . . . . . . . . . . . . . . . . . . . . . . 3
   2.  Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

1. Foo
   bar baz quux

   ```text
   dingus bar
     foo
   ```
1.1.    Bingus
   lorem ipsum frisky gypsum

1.2. Terminology
   Bat: tool used for baseball
   Code: if (name === 'Jerry') {throw new Error('get out jerry!!1');}

2. Bar
   bingus
   o fart
   o poo";
    // preserves whitespace, finds end of section that is not last
    assert_eq!(
               gen_docstring("1".into(), rfc),
               r"# Foo
[_generated from RFC7252 section 1_](https://datatracker.ietf.org/doc/html/rfc7252#section-1)

bar baz quux

```text
dingus bar
  foo
```"
    );

    // finds end of section that is last
    assert_eq!(
               gen_docstring("2".into(), rfc),
               r"# Bar
[_generated from RFC7252 section 2_](https://datatracker.ietf.org/doc/html/rfc7252#section-2)

bingus
o fart
o poo"
    );
  }
}