rpm-spec 0.3.4

Parser and pretty-printer for RPM .spec files
Documentation
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
//! Parser for `%changelog` sections.
//!
//! Entry header format:
//!
//! ```text
//! * Weekday Month Day Year Author [<email>] [- VERSION[-RELEASE]]
//! ```
//!
//! Body lines (everything between this header and the next `*`-headed
//! entry or section header) are stored as `Vec<Text>` with macros parsed.

use nom::{IResult, error::ErrorKind, error_position};

use crate::ast::{ChangelogDate, ChangelogEntry, Month, Section, Span, Text, Weekday};
use crate::parse_result::codes;

use super::input::{Input, span_between};
use super::section::peek_section_header;
use super::state::ParserState;
use super::text::parse_body_as_text;
use super::util::{line_terminator, physical_line, space0};

/// Parse a `%changelog` section header and body into
/// [`Section::Changelog`].
pub fn parse_changelog_section<'a>(
    state: &ParserState,
    input: Input<'a>,
) -> IResult<Input<'a>, Section<Span>> {
    let start = input;
    let (after_ws, _) = space0(input)?;
    let (after_kw, _) = nom::Input::take_split(&after_ws, "%changelog".len());
    let (after_header, _) = line_terminator(after_kw)?;

    let (after_body, entries) = collect_entries(state, after_header);
    let span = span_between(&start, &after_body);

    Ok((
        after_body,
        Section::Changelog {
            entries,
            data: span,
        },
    ))
}

fn collect_entries<'a>(
    state: &ParserState,
    input: Input<'a>,
) -> (Input<'a>, Vec<ChangelogEntry<Span>>) {
    let mut cursor = input;
    let mut entries: Vec<ChangelogEntry<Span>> = Vec::new();

    while !cursor.fragment().is_empty() {
        if peek_section_header(cursor).is_some() {
            break;
        }
        // RPM changelog grammar: each entry header MUST start with `*`
        // in column 1. Lines beginning with leading whitespace —
        // typical body bullets like ` * release notes` or `  - url` —
        // are entry continuations, not new headers. Don't consume
        // whitespace before the start-of-entry probe; the previous
        // logic mistakenly accepted indented `*` as header-starts
        // and triggered W0023 on innocent prose.
        let frag = *cursor.fragment();
        if frag.is_empty() {
            break;
        }
        if !frag.starts_with('*') {
            // Unexpected text before the first `*` — consume one physical
            // line so we keep making progress.
            let here = cursor;
            let (after, _) = match physical_line(here) {
                Ok(r) => r,
                Err(_) => break,
            };
            if after.location_offset() == here.location_offset() {
                break;
            }
            // Only complain about non-blank, non-bullet stray lines.
            // Body bullets ` * foo` / ` - foo` / `  text` were being
            // misread as malformed headers; we now treat them as
            // continuation noise and stay silent.
            if !line_is_blank(here.fragment()) && !is_indented_nonblank_line(here.fragment()) {
                state.push_warning_code(
                    codes::W_UNEXPECTED_LINE_IN_CHANGELOG,
                    "unexpected line outside a %changelog entry",
                    Some(span_between(&here, &after)),
                );
            }
            cursor = after;
            continue;
        }

        match parse_changelog_entry(state, cursor) {
            Ok((rest, entry)) => {
                if rest.location_offset() == cursor.location_offset() {
                    break;
                }
                entries.push(entry);
                cursor = rest;
            }
            Err(_) => {
                let here = cursor;
                let (after, _) = match physical_line(here) {
                    Ok(r) => r,
                    Err(_) => break,
                };
                if after.location_offset() == here.location_offset() {
                    break;
                }
                state.push_warning_code(
                    codes::W_MALFORMED_CHANGELOG_HEADER,
                    "malformed %changelog entry header",
                    Some(span_between(&here, &after)),
                );
                cursor = after;
            }
        }
    }

    (cursor, entries)
}

fn line_is_blank(s: &str) -> bool {
    let line = s.split(['\n', '\r']).next().unwrap_or(s);
    line.trim().is_empty()
}

/// `true` when the line starts with whitespace **and** has any
/// non-whitespace content — i.e. it's an indented body line of the
/// previous changelog entry (bullets like ` - foo`, ` * bar`, or plain
/// continuation prose). Header lines must put `*` in column 1, so
/// anything indented is by definition a body continuation. W0023 used
/// to flag these because the caller stripped leading whitespace before
/// probing for `*`.
fn is_indented_nonblank_line(s: &str) -> bool {
    let line = s.split(['\n', '\r']).next().unwrap_or(s);
    let trimmed = line.trim_start();
    !trimmed.is_empty() && trimmed.len() != line.len()
}

/// Parse one `%changelog` entry (header + body lines).
pub fn parse_changelog_entry<'a>(
    state: &ParserState,
    input: Input<'a>,
) -> IResult<Input<'a>, ChangelogEntry<Span>> {
    let start = input;
    let (after_ws, _) = space0(input)?;
    let frag = *after_ws.fragment();
    if !frag.starts_with('*') {
        return Err(nom::Err::Error(error_position!(input, ErrorKind::Tag)));
    }
    // Strip the leading `*` + whitespace.
    let after_star = advance(after_ws, 1)
        .ok_or_else(|| nom::Err::Error(error_position!(input, ErrorKind::Tag)))?;
    let (after_star_ws, _) = space0(after_star)?;

    let header_start = after_star_ws;
    let (after_header_line, header_input) = physical_line(after_star_ws)?;
    let header_span = span_between(&header_start, &after_header_line);

    let header_text = header_input.fragment();
    let (date, author, email, version) = parse_header_text(state, header_text, header_span)
        .ok_or_else(|| nom::Err::Error(error_position!(input, ErrorKind::Tag)))?;

    // Collect body lines.
    let mut cursor = after_header_line;
    let mut body: Vec<Text> = Vec::new();
    while !cursor.fragment().is_empty() {
        if peek_section_header(cursor).is_some() {
            break;
        }
        // Stop at the next entry header (line starting with `*` after ws).
        let (after_ws2, _) = space0(cursor)?;
        if after_ws2.fragment().starts_with('*') {
            break;
        }
        let here = cursor;
        let (after, line_input) = physical_line(here)?;
        if after.location_offset() == here.location_offset() {
            break;
        }
        body.push(parse_body_as_text(state, line_input.fragment()));
        cursor = after;
    }
    // Trim trailing empty body lines.
    while matches!(body.last(), Some(t) if text_is_empty(t)) {
        body.pop();
    }

    let span = span_between(&start, &cursor);
    Ok((
        cursor,
        ChangelogEntry {
            date,
            author,
            email,
            version,
            body,
            data: span,
        },
    ))
}

// ---------------------------------------------------------------------
// Header parsing helpers
// ---------------------------------------------------------------------

/// Lower bound of the plausible-year range for `%changelog` entries.
///
/// We trust the Unix epoch year (1970) as the floor: package
/// maintainers occasionally backdate entries, and rounding to 1970
/// keeps the rule easy to remember. Anything earlier is almost
/// certainly a typo.
const MIN_PLAUSIBLE_YEAR: u16 = 1970;
/// Upper bound of the plausible-year range; paired with
/// [`MIN_PLAUSIBLE_YEAR`]. Picked as a far-future sentinel — bump it
/// if RPM is still in use in the 23rd century.
const MAX_PLAUSIBLE_YEAR: u16 = 2200;

fn parse_header_text(
    state: &ParserState,
    header: &str,
    header_span: Span,
) -> Option<(ChangelogDate, Text, Option<Text>, Option<Text>)> {
    // Tokens: Weekday Month Day Year ...
    let mut tokens = header.split_whitespace();
    let weekday = parse_weekday(tokens.next()?)?;
    let month = parse_month(tokens.next()?)?;
    let day: u8 = tokens.next()?.parse().ok()?;
    let year: u16 = tokens.next()?.parse().ok()?;

    if !(1..=31).contains(&day) {
        state.push_warning_code(
            codes::W_IMPLAUSIBLE_CHANGELOG_DATE,
            format!("day-of-month `{day}` is out of range 1..=31"),
            Some(header_span),
        );
    }
    if !(MIN_PLAUSIBLE_YEAR..=MAX_PLAUSIBLE_YEAR).contains(&year) {
        state.push_warning_code(
            codes::W_IMPLAUSIBLE_CHANGELOG_DATE,
            format!(
                "year `{year}` is implausible (expected \
                 {MIN_PLAUSIBLE_YEAR}..={MAX_PLAUSIBLE_YEAR})"
            ),
            Some(header_span),
        );
    }

    let date = ChangelogDate {
        weekday,
        month,
        day,
        year,
    };

    // The remainder of `header` after the first four whitespace-separated
    // tokens is the author segment (with optional email and version).
    let rest = consume_first_four_tokens(header)?.trim();
    let (author_str, email_str, version_str) = split_author_email_version(rest);

    let author = parse_body_as_text(state, author_str.trim());
    let email = email_str.map(|e| parse_body_as_text(state, e.trim()));
    let version = version_str.map(|v| parse_body_as_text(state, v.trim()));

    Some((date, author, email, version))
}

fn consume_first_four_tokens(s: &str) -> Option<&str> {
    let mut idx = 0;
    let bytes = s.as_bytes();
    let mut tokens_consumed = 0;
    while idx < bytes.len() && tokens_consumed < 4 {
        // Skip whitespace.
        while idx < bytes.len() && matches!(bytes[idx], b' ' | b'\t') {
            idx += 1;
        }
        if idx >= bytes.len() {
            return None;
        }
        // Skip token.
        while idx < bytes.len() && !matches!(bytes[idx], b' ' | b'\t') {
            idx += 1;
        }
        tokens_consumed += 1;
    }
    if tokens_consumed < 4 {
        return None;
    }
    Some(&s[idx..])
}

/// Split the remainder of a changelog header into author, optional
/// `<email>`, and optional trailing `- VERSION`.
fn split_author_email_version(rest: &str) -> (&str, Option<&str>, Option<&str>) {
    // 1. Find " - " separator for version (last occurrence wins so the
    //    author may contain hyphens).
    let (head, version) = match rfind_dash_separator(rest) {
        Some(idx) => (&rest[..idx], Some(&rest[idx + 3..])),
        None => (rest, None),
    };
    // 2. Inside head, find `<…>` for email.
    let (author, email) = match (head.find('<'), head.rfind('>')) {
        (Some(lt), Some(gt)) if gt > lt => {
            let author = head[..lt].trim_end();
            let email = &head[lt + 1..gt];
            (author, Some(email))
        }
        _ => (head.trim_end(), None),
    };
    (author, email, version)
}

/// Find the byte index of the rightmost ` - ` (space-dash-space) at the
/// top of `head`. Used to peel off `- VERSION` while tolerating hyphens
/// in author names.
fn rfind_dash_separator(s: &str) -> Option<usize> {
    let bytes = s.as_bytes();
    if bytes.len() < 3 {
        return None;
    }
    let mut i = bytes.len() - 3;
    loop {
        if &bytes[i..i + 3] == b" - " {
            return Some(i);
        }
        if i == 0 {
            return None;
        }
        i -= 1;
    }
}

fn parse_weekday(s: &str) -> Option<Weekday> {
    match s {
        "Mon" => Some(Weekday::Mon),
        "Tue" => Some(Weekday::Tue),
        "Wed" => Some(Weekday::Wed),
        "Thu" => Some(Weekday::Thu),
        "Fri" => Some(Weekday::Fri),
        "Sat" => Some(Weekday::Sat),
        "Sun" => Some(Weekday::Sun),
        _ => None,
    }
}

fn parse_month(s: &str) -> Option<Month> {
    match s {
        "Jan" => Some(Month::Jan),
        "Feb" => Some(Month::Feb),
        "Mar" => Some(Month::Mar),
        "Apr" => Some(Month::Apr),
        "May" => Some(Month::May),
        "Jun" => Some(Month::Jun),
        "Jul" => Some(Month::Jul),
        "Aug" => Some(Month::Aug),
        "Sep" => Some(Month::Sep),
        "Oct" => Some(Month::Oct),
        "Nov" => Some(Month::Nov),
        "Dec" => Some(Month::Dec),
        _ => None,
    }
}

fn text_is_empty(t: &Text) -> bool {
    t.segments
        .iter()
        .all(|s| matches!(s, crate::ast::TextSegment::Literal(s) if s.trim().is_empty()))
}

fn advance<'a>(input: Input<'a>, n: usize) -> Option<Input<'a>> {
    if input.fragment().len() < n {
        return None;
    }
    let (rest, _) = nom::Input::take_split(&input, n);
    Some(rest)
}

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

    fn parse(src: &str) -> Section<Span> {
        let state = ParserState::new();
        let inp = Input::new(src);
        let (_rest, sec) = parse_changelog_section(&state, inp).unwrap();
        sec
    }

    fn entries(sec: &Section<Span>) -> &Vec<ChangelogEntry<Span>> {
        match sec {
            Section::Changelog { entries, .. } => entries,
            _ => panic!(),
        }
    }

    #[test]
    fn one_entry_with_version() {
        let src = "%changelog\n* Wed May 14 2025 Maintainer <m@example.org> - 1.0-1\n- initial packaging\n";
        let sec = parse(src);
        let es = entries(&sec);
        assert_eq!(es.len(), 1);
        let e = &es[0];
        assert_eq!(e.date.weekday, Weekday::Wed);
        assert_eq!(e.date.month, Month::May);
        assert_eq!(e.date.day, 14);
        assert_eq!(e.date.year, 2025);
        assert_eq!(e.author.literal_str(), Some("Maintainer"));
        assert_eq!(
            e.email.as_ref().unwrap().literal_str(),
            Some("m@example.org")
        );
        assert_eq!(e.version.as_ref().unwrap().literal_str(), Some("1.0-1"));
        assert_eq!(e.body.len(), 1);
        assert_eq!(e.body[0].literal_str(), Some("- initial packaging"));
    }

    #[test]
    fn entry_without_version() {
        let src = "%changelog\n* Mon Jan 01 2024 Alice <a@example.com>\n- something\n";
        let sec = parse(src);
        let es = entries(&sec);
        assert!(es[0].version.is_none());
        assert_eq!(
            es[0].email.as_ref().unwrap().literal_str(),
            Some("a@example.com")
        );
    }

    #[test]
    fn entry_without_email() {
        let src = "%changelog\n* Tue Feb 02 2024 Bob - 2.0-1\n- something\n";
        let sec = parse(src);
        let es = entries(&sec);
        assert!(es[0].email.is_none());
        assert_eq!(es[0].author.literal_str(), Some("Bob"));
        assert_eq!(es[0].version.as_ref().unwrap().literal_str(), Some("2.0-1"));
    }

    #[test]
    fn two_entries() {
        let src = "\
%changelog
* Wed May 14 2025 A <a@x.org> - 1.0-1
- newer
* Mon Jan 01 2024 B <b@x.org> - 0.9-1
- older
";
        let sec = parse(src);
        let es = entries(&sec);
        assert_eq!(es.len(), 2);
        assert_eq!(es[0].date.year, 2025);
        assert_eq!(es[1].date.year, 2024);
    }

    #[test]
    fn entry_with_multiline_body() {
        let src = "\
%changelog
* Wed May 14 2025 A <a@x.org> - 1.0-1
- first
- second
- third
";
        let sec = parse(src);
        let es = entries(&sec);
        assert_eq!(es[0].body.len(), 3);
    }

    #[test]
    fn entry_with_macro_in_version() {
        let src = "%changelog\n* Wed May 14 2025 A <a@x.org> - 1.0-1%{?dist}\n- x\n";
        let sec = parse(src);
        let es = entries(&sec);
        let ver = es[0].version.as_ref().unwrap();
        // The version should contain the literal "1.0-1" plus a macro for dist.
        assert!(
            ver.segments
                .iter()
                .any(|s| matches!(s, crate::ast::TextSegment::Macro(m) if m.name == "dist"))
        );
    }

    #[test]
    fn changelog_stops_at_next_section_header() {
        let src = "%changelog\n* Wed May 14 2025 A <a@x.org> - 1.0-1\n- body\n%files\n";
        let sec = parse(src);
        let es = entries(&sec);
        assert_eq!(es.len(), 1);
        // Body should not contain "%files".
        for line in &es[0].body {
            assert!(!line.literal_str().unwrap_or("").contains("%files"));
        }
    }

    #[test]
    fn author_with_hyphen() {
        // Author name itself contains a hyphen; version must still be peeled.
        let src = "%changelog\n* Wed May 14 2025 Foo-Bar Baz <fb@example.org> - 1.0-1\n- x\n";
        let sec = parse(src);
        let es = entries(&sec);
        assert_eq!(es[0].author.literal_str(), Some("Foo-Bar Baz"));
        assert_eq!(es[0].version.as_ref().unwrap().literal_str(), Some("1.0-1"));
    }
}