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
//! Parses a release note for the specified version from a changelog.
//!
//! # Format
//!
//! By default, this crate is intended to support most markdown-based
//! changelogs that have the title of each release starts with the version.
//!
//! ### Headings
//!
//! The heading for each release must be Atx-style (1-6 `#`) or
//! Setext-style (`=` or `-` in a line under text), and the heading levels
//! must match with other releases.
//!
//! Atx-style headings:
//!
//! ```markdown
//! # 0.1.0
//! ```
//!
//! ```markdown
//! ## 0.1.0
//! ```
//!
//! Setext-style headings:
//!
//! ```markdown
//! 0.1.0
//! =====
//! ```
//!
//! ```markdown
//! 0.1.0
//! -----
//! ```
//!
//! ### Titles
//!
//! The title of each release must start with a text or a link text (text with `[` and `]`)
//! that starts with a valid version format. For example:
//!
//! ```markdown
//! # [0.2.0]
//!
//! description...
//!
//! # 0.1.0
//!
//! description...
//! ```
//!
//! You can also include characters before the version as prefix. For example:
//!
//! ```markdown
//! ## Version 0.1.0
//! ```
//!
//! By default only "v", "Version " and "Release " are allowed as prefix and
//! can be customized using the [`Parser::prefix_format`] method.
//!
//! You can freely include characters after the version (this crate
//! does not parse it). For example:
//!
//! ```markdown
//! # v0.1.0 - 2020-01-01
//! ```
//!
//! ### Versions
//!
//! The default version format is
//! `MAJOR.MINOR.PATCH(-PRE_RELEASE)?(+BUILD_METADATA)?`, and is
//! based on [Semantic Versioning][semver]. (Pre-release version and build
//! metadata are optional.)
//!
//! This is parsed using the following regular expression:
//!
//! ```text
//! ^\d+\.\d+\.\d+(-[\w\.-]+)?(\+[\w\.-]+)?
//! ```
//!
//! To customize the version format, use the [`Parser::version_format`] method.
//!
//! # Examples
//!
//! ```rust
//! let changelog = "\
//! ## 0.1.2 - 2020-03-01
//!
//! - Bug fixes.
//!
//! ## 0.1.1 - 2020-02-01
//!
//! - Added `Foo`.
//! - Added `Bar`.
//!
//! ## 0.1.0 - 2020-01-01
//!
//! Initial release
//! ";
//!
//! // Parse changelog.
//! let releases = parse_changelog::parse(changelog).unwrap();
//!
//! // Get the latest release.
//! assert_eq!(releases[0].version, "0.1.2");
//! assert_eq!(releases[0].title, "0.1.2 - 2020-03-01");
//! assert_eq!(releases[0].notes, "- Bug fixes.");
//!
//! // Get the specified release.
//! assert_eq!(releases["0.1.0"].title, "0.1.0 - 2020-01-01");
//! assert_eq!(releases["0.1.0"].notes, "Initial release");
//! assert_eq!(releases["0.1.1"].title, "0.1.1 - 2020-02-01");
//! assert_eq!(
//!     releases["0.1.1"].notes,
//!     "- Added `Foo`.\n\
//!      - Added `Bar`."
//! );
//! ```
//!
//! The key of the map returned does not include prefixes such as "v", "Version ", etc.
//!
//! ```rust
//! let changelog_a = "\
//! ## Version 0.1.0 - 2020-01-01
//! Initial release
//! ";
//! let changelog_b = "\
//! ## v0.1.0 - 2020-02-01
//! Initial release
//! ";
//!
//! let releases_a = parse_changelog::parse(changelog_a).unwrap();
//! let releases_b = parse_changelog::parse(changelog_b).unwrap();
//! // Not `releases["Version 0.1.0"]`
//! assert_eq!(releases_a["0.1.0"].version, "0.1.0");
//! assert_eq!(releases_a["0.1.0"].title, "Version 0.1.0 - 2020-01-01");
//! assert_eq!(releases_a["0.1.0"].notes, "Initial release");
//! // Not `releases["v0.1.0"]`
//! assert_eq!(releases_b["0.1.0"].version, "0.1.0");
//! assert_eq!(releases_b["0.1.0"].title, "v0.1.0 - 2020-02-01");
//! assert_eq!(releases_b["0.1.0"].notes, "Initial release");
//! ```
//!
//! [keepachangelog]: https://keepachangelog.com/en/1.0.0
//! [semver]: https://semver.org/spec/v2.0.0.html

#![forbid(unsafe_code)]
#![warn(future_incompatible, rust_2018_idioms, single_use_lifetimes, unreachable_pub)]
#![warn(missing_debug_implementations, missing_docs)]
#![warn(clippy::all, clippy::default_trait_access)]

use anyhow::bail;
use indexmap::IndexMap;
use once_cell::sync::Lazy;
use regex::Regex;
use std::{iter::Peekable, mem, str::Lines};

type Result<T, E = anyhow::Error> = std::result::Result<T, E>;

type Releases<'a> = IndexMap<&'a str, Release<'a>>;

/// Parses release notes from the given `text`.
///
/// This function uses the default version format. If you want to use another
/// version format, use [`Parser::version_format`].
///
/// See crate level documentation for changelog and version format supported
/// by default.
///
/// # Errors
///
/// Returns an error if any of the following:
///
/// - There are multiple release notes for one version.
/// - No release was found. This usually means that the changelog isn't
///   written in the supported format.
pub fn parse(text: &str) -> Result<Releases<'_>> {
    Parser::new().parse(text)
}

/// A release note for a version.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Release<'a> {
    /// The version of this release.
    pub version: &'a str,
    /// The title of this release.
    // `.trim()`-ed
    pub title: &'a str,
    /// The descriptions of this release.
    // not `.trim()`-ed
    pub notes: String,
}

impl Release<'_> {
    fn new() -> Self {
        Self { version: "", title: "", notes: String::new() }
    }
}

/// A changelog parser.
#[derive(Debug, Default)]
pub struct Parser {
    /// Version format. e.g., "0.1.0" in "# v0.1.0 (2020-01-01)".
    ///
    /// If `None`, `DEFAULT_VERSION_FORMAT` is used.
    version: Option<Regex>,
    /// Prefix format. e.g., "v" in "# v0.1.0 (2020-01-01)", "Version " in
    /// "# Version 0.1.0 (2020-01-01)".
    ///
    /// If `None`, `DEFAULT_PREFIX_FORMAT` is used.
    prefix: Option<Regex>,
}

static DEFAULT_PREFIX_FORMAT: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"^(v|Version |Release )?").unwrap());
static DEFAULT_VERSION_FORMAT: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"^\d+\.\d+\.\d+(-[\w\.-]+)?(\+[\w\.-]+)?").unwrap());

impl Parser {
    /// Creates a new changelog parser.
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the version format.
    ///
    /// ```text
    /// ## v0.1.0 -- 2020-01-01
    ///     ^^^^^
    /// ```
    ///
    /// The default version format is based on [Semantic Versioning][semver]
    /// and is the following regular expression:
    ///
    /// ```text
    /// ^\d+\.\d+\.\d+(-[\w\.-]+)?(\+[\w\.-]+)?
    /// ```
    ///
    /// **Note**: Most projects that adopt [Semantic Versioning][semver] do not
    /// need to change this.
    ///
    /// To customize the text before the version number (e.g., "v" in "# v0.1.0",
    /// "Version " in "# Version 0.1.0", etc.), use the [`prefix_format`] method
    /// instead of this method.
    ///
    /// # Errors
    ///
    /// Returns an error if any of the following:
    ///
    /// - The specified version format is not valid regular expression or
    ///   supported by [regex] crate.
    /// - The specified version format is empty or contains only
    ///   [whitespace](char::is_whitespace).
    ///
    /// [`parse`]: Self::parse
    /// [`prefix_format`]: Self::prefix_format
    /// [regex]: https://docs.rs/regex
    /// [semver]: https://semver.org/spec/v2.0.0.html
    pub fn version_format(&mut self, version_format: &str) -> Result<&mut Self> {
        if version_format.trim().is_empty() {
            bail!("empty or whitespace version format");
        }
        self.version = Some(Regex::new(version_format)?);
        Ok(self)
    }

    /// Sets the prefix format.
    ///
    /// "Prefix" means the range from the first non-whitespace character after
    /// heading to the character before the version (including whitespace
    /// characters). For example:
    ///
    /// ```text
    /// ## Version 0.1.0 -- 2020-01-01
    ///    ^^^^^^^^
    /// ```
    /// ```text
    /// ## v0.1.0 -- 2020-01-01
    ///    ^
    /// ```
    ///
    /// The default prefix format is the following regular expression:
    ///
    /// ```text
    /// ^(v|Version |Release )?
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if any of the following:
    ///
    /// - The specified prefix format is not valid regular expression or
    ///   supported by [regex] crate.
    /// - The specified prefix format is empty or contains only
    ///   [whitespace](char::is_whitespace).
    ///
    /// [`parse`]: Self::parse
    /// [`version_format`]: Self::version_format
    /// [regex]: https://docs.rs/regex
    pub fn prefix_format(&mut self, prefix_format: &str) -> Result<&mut Self> {
        if prefix_format.trim().is_empty() {
            bail!("empty or whitespace prefix format");
        }
        self.prefix = Some(Regex::new(prefix_format)?);
        Ok(self)
    }

    fn get_version_format(&self) -> &Regex {
        self.version.as_ref().unwrap_or(&DEFAULT_VERSION_FORMAT)
    }

    fn get_prefix_format(&self) -> &Regex {
        self.prefix.as_ref().unwrap_or(&DEFAULT_PREFIX_FORMAT)
    }

    /// Parses release notes from the given `text`.
    ///
    /// See crate level documentation for changelog and version format supported
    /// by default.
    ///
    /// # Errors
    ///
    /// Returns an error if any of the following:
    ///
    /// - There are multiple release notes for one version.
    /// - No release was found. This usually means that the changelog isn't
    ///   written in the supported format, or that the specified version format
    ///   is wrong if you specify your own version format.
    pub fn parse<'a>(&self, text: &'a str) -> Result<Releases<'a>> {
        parse_inner(self, text)
    }
}

fn parse_inner<'a>(parser: &Parser, text: &'a str) -> Result<Releases<'a>> {
    const LN: char = '\n';

    let version_format = parser.get_version_format();
    let prefix_format = parser.get_prefix_format();

    let mut map = IndexMap::new();
    let mut insert_release = |mut cur_release: Release<'a>| {
        debug_assert!(!cur_release.version.is_empty());
        while cur_release.notes.ends_with(LN) {
            // Remove trailing newlines.
            cur_release.notes.pop();
        }
        if let Some(release) = map.insert(cur_release.version, cur_release) {
            bail!("multiple release notes for '{}'", release.version);
        }
        Ok(())
    };

    let lines = &mut text.lines().peekable();
    let mut cur_release = Release::new();
    // If `true`, we are in a release section.
    let mut on_release = false;
    // If `true`, we are in a code block ("```").
    let mut on_code_block = false;
    // If `true`, we are in a comment (`<!--` and `-->`).
    let mut on_comment = false;
    // The heading level of release sections.
    let mut level = None;

    while let Some(line) = lines.next() {
        let heading = heading(line, lines);
        if heading.is_none() || on_code_block || on_comment {
            if trim(line).starts_with("```") {
                on_code_block = !on_code_block;
            }

            if !on_code_block {
                const OPEN: &str = "<!--";
                const CLOSE: &str = "-->";
                let mut ll = Some(line);
                while let Some(l) = ll {
                    match (l.find(OPEN), l.find(CLOSE)) {
                        (None, None) => {}
                        // <!-- ...
                        (Some(_), None) => on_comment = true,
                        // ... -->
                        (None, Some(_)) => on_comment = false,
                        (Some(open), Some(close)) => {
                            if open < close {
                                // <!-- ... -->
                                on_comment = false;
                                ll = l.get(close + CLOSE.len()..);
                            } else {
                                // --> ... <!--
                                on_comment = true;
                                ll = l.get(open + OPEN.len()..);
                            }
                            continue;
                        }
                    }
                    break;
                }
            }

            // Non-heading lines are always considered part of the current
            // section.
            if on_release {
                cur_release.notes.push_str(line);
                cur_release.notes.push(LN);
            }
            continue;
        }
        let heading = heading.unwrap();

        let mut unlinked = unlink(heading.text);
        if let Some(m) = prefix_format.find(unlinked) {
            unlinked = unlink(&unlinked[m.end()..]);
        }
        let version = match version_format.find(unlinked) {
            Some(m) => &unlinked[m.start()..m.end()],
            None => {
                if level.map_or(true, |l| heading.level <= l) {
                    // Ignore non-release sections that have the same or higher
                    // heading levels as release sections.
                    on_release = false;
                } else if on_release {
                    // Otherwise, it is considered part of the current section.
                    cur_release.notes.push_str(line);
                    cur_release.notes.push(LN);
                }
                continue;
            }
        };

        if mem::replace(&mut on_release, true) {
            // end of prev release
            insert_release(mem::replace(&mut cur_release, Release::new()))?;
        }

        cur_release.version = version;
        cur_release.title = heading.text;
        level.get_or_insert(heading.level);

        if heading.style == HeadingStyle::Setext {
            // Remove an underline after a Setext-style heading.
            lines.next();
        }
        while let Some(next) = lines.peek() {
            if next.trim().is_empty() {
                // Remove newlines after a heading.
                lines.next();
            } else {
                break;
            }
        }
    }

    if !cur_release.version.is_empty() {
        insert_release(cur_release)?;
    }

    if map.is_empty() {
        bail!("no release was found");
    }

    Ok(map)
}

struct Heading<'a> {
    text: &'a str,
    level: usize,
    style: HeadingStyle,
}

#[derive(Eq, PartialEq)]
enum HeadingStyle {
    /// Atx-style headings use 1-6 `#` characters at the start of the line,
    /// corresponding to header levels 1-6.
    Atx,
    /// Setext-style headings are “underlined” using equal signs `=` (for
    /// first-level headings) and dashes `-` (for second-level headings).
    Setext,
}

fn heading<'a>(line: &'a str, lines: &mut Peekable<Lines<'_>>) -> Option<Heading<'a>> {
    static ALL_EQUAL_SIGNS: Lazy<Regex> = Lazy::new(|| Regex::new("^=+$").unwrap());
    static ALL_DASHES: Lazy<Regex> = Lazy::new(|| Regex::new("^-+$").unwrap());

    let line = trim(line);
    if line.starts_with('#') {
        let mut level = 0;
        while line[level..].starts_with('#') {
            level += 1;
        }
        if level <= 6 {
            Some(Heading { text: line[level..].trim(), level, style: HeadingStyle::Atx })
        } else {
            None
        }
    } else if let Some(next) = lines.peek() {
        let next = trim(next);
        if ALL_EQUAL_SIGNS.is_match(next) {
            Some(Heading { text: line, level: 1, style: HeadingStyle::Setext })
        } else if ALL_DASHES.is_match(next) {
            Some(Heading { text: line, level: 2, style: HeadingStyle::Setext })
        } else {
            None
        }
    } else {
        None
    }
}

fn trim(s: &str) -> &str {
    let mut cnt = 0;
    while s[cnt..].starts_with(' ') {
        cnt += 1;
    }
    // Indents less than 4 are ignored.
    if cnt < 4 { s[cnt..].trim_end() } else { s.trim_end() }
}

/// If a leading `[` exists, returns a string with it removed.
///
/// This is not a full "unlink" on markdown, but this is enough as this crate
/// does not parse a string at the end of headings.
fn unlink(s: &str) -> &str {
    s.strip_prefix('[').unwrap_or(s)
}