oakum 0.12.1

A polyglot release tool that derives dependent version bumps from the dependency graph
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
//! `toml_edit` is the right crate for Cargo.toml and still has two traps:
//! assigning through `Item` resets decor, and structural `to_string`
//! newlines are `\n` (multiline string interiors can keep `\r\n`).

use std::fmt;

use toml_edit::{value, DocumentMut, Item, Value};

/// Replace the string at `path`, restoring exclusive-CRLF originals.
///
/// Intermediate **standard** tables must already exist (not inline tables
/// or arrays of tables). Path segments are literal keys, not dotted
/// paths. A missing last key is inserted.
///
/// # Errors
///
/// Returns [`TomlEditError`] when the text is not TOML or a path segment
/// is missing or the wrong kind.
pub fn set_toml_string(text: &str, path: &[&str], next: &str) -> Result<String, TomlEditError> {
    let Some((last, parents)) = path.split_last() else {
        return Err(TomlEditError::EmptyPath);
    };
    if last.is_empty() || parents.iter().any(|segment| segment.is_empty()) {
        return Err(TomlEditError::EmptyPath);
    }
    let mut doc: DocumentMut = text.parse().map_err(TomlEditError::Parse)?;
    let mut cursor = doc.as_item_mut();
    let mut landed_on: Option<&str> = None;
    for segment in parents {
        if !cursor.is_table() {
            return Err(TomlEditError::NotTable {
                path: landed_on.unwrap_or(segment).to_string(),
            });
        }
        cursor = table_child(cursor, segment)?;
        landed_on = Some(segment);
    }
    if !cursor.is_table() {
        return Err(TomlEditError::NotTable {
            path: landed_on.unwrap_or(last).to_string(),
        });
    }
    if let Some(existing) = existing_key(cursor, last) {
        if existing.as_str().is_none() {
            return Err(TomlEditError::NotString {
                path: (*last).to_owned(),
            });
        }
    }
    set_preserving_decor(&mut cursor[last], next);
    Ok(emit_toml(&doc, text))
}

/// True for `version.workspace = true` or `version = { workspace = true }`.
///
/// # Errors
///
/// Returns [`TomlEditError::Parse`] when the text is not TOML.
pub fn cargo_package_version_inherits_workspace(text: &str) -> Result<bool, TomlEditError> {
    let doc: DocumentMut = text.parse().map_err(TomlEditError::Parse)?;
    let Some(version) = doc
        .get("package")
        .and_then(|package| package.get("version"))
    else {
        return Ok(false);
    };
    if version.as_str().is_some() {
        return Ok(false);
    }
    Ok(version.get("workspace").and_then(Item::as_bool) == Some(true))
}

/// What a Cargo manifest's `[package]` says its version is.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum DeclaredVersion {
    Literal(String),
    /// `version.workspace = true`: `[workspace.package]` holds it.
    Workspace,
    Absent,
}

/// # Errors
///
/// Returns [`TomlEditError::Parse`] when the text is not TOML.
pub(crate) fn cargo_declared_version(text: &str) -> Result<DeclaredVersion, TomlEditError> {
    if cargo_package_version_inherits_workspace(text)? {
        return Ok(DeclaredVersion::Workspace);
    }
    let doc: DocumentMut = text.parse().map_err(TomlEditError::Parse)?;
    Ok(doc
        .get("package")
        .and_then(|package| package.get("version"))
        .and_then(Item::as_str)
        .map_or(DeclaredVersion::Absent, |version| {
            DeclaredVersion::Literal(version.to_owned())
        }))
}

/// `[workspace.package].version`, what an inheriting member declares.
///
/// # Errors
///
/// Returns [`TomlEditError::Parse`] when the text is not TOML.
pub(crate) fn cargo_workspace_version(text: &str) -> Result<Option<String>, TomlEditError> {
    let doc: DocumentMut = text.parse().map_err(TomlEditError::Parse)?;
    Ok(doc
        .get("workspace")
        .and_then(|workspace| workspace.get("package"))
        .and_then(|package| package.get("version"))
        .and_then(Item::as_str)
        .map(str::to_owned))
}

/// `Table::get_mut` returns `None` for a missing key. `Item::get_mut`
/// inserts `Item::None` and looks occupied.
fn table_child<'a>(parent: &'a mut Item, segment: &str) -> Result<&'a mut Item, TomlEditError> {
    parent
        .as_table_mut()
        .and_then(|table| table.get_mut(segment))
        .ok_or_else(|| TomlEditError::Missing {
            path: segment.to_owned(),
        })
}

fn existing_key<'a>(parent: &'a Item, key: &str) -> Option<&'a Item> {
    parent.as_table().and_then(|table| table.get(key))
}

/// Assign `next` without dropping the item's trailing comment or padding.
///
/// `*item = value(v)` clones none of that trivia. Restore it here so every
/// call site is not its own copy of the trap (okm-299).
pub(crate) fn set_preserving_decor(item: &mut Item, next: impl Into<Value>) {
    let decor = item.as_value().map(Value::decor).cloned();
    *item = value(next);
    if let (Some(decor), Some(written)) = (decor, item.as_value_mut()) {
        *written.decor_mut() = decor;
    }
}

/// Serialize `doc`, restoring CRLF only when every original newline was CRLF.
///
/// `toml_edit` emits `\n` for structural newlines; multiline string
/// interiors can still hold `\r\n`. Without this, rewriting a CRLF
/// Cargo.toml turns the file LF. Convert only lone `\n` so an existing
/// `\r\n` in a string body cannot become `\r\r\n`.
pub(crate) fn emit_toml(doc: &DocumentMut, original: &str) -> String {
    let mut out = doc.to_string();
    if uses_only_crlf(original) {
        out = restore_lone_lf(&out);
    }
    if !original.ends_with('\n') {
        out = strip_trailing_newline(&out);
    }
    out
}

fn strip_trailing_newline(text: &str) -> String {
    text.strip_suffix("\r\n")
        .or_else(|| text.strip_suffix('\n'))
        .unwrap_or(text)
        .to_owned()
}

fn restore_lone_lf(text: &str) -> String {
    let bytes = text.as_bytes();
    let mut out = String::with_capacity(text.len());
    let mut start = 0;
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'\n' && bytes.get(i.wrapping_sub(1)) != Some(&b'\r') {
            out.push_str(&text[start..i]);
            out.push_str("\r\n");
            i += 1;
            start = i;
        } else {
            i += 1;
        }
    }
    out.push_str(&text[start..]);
    out
}

fn uses_only_crlf(text: &str) -> bool {
    let bytes = text.as_bytes();
    let mut saw_crlf = false;
    let mut i = 0;
    while i < bytes.len() {
        match bytes[i] {
            b'\r' if bytes.get(i + 1) == Some(&b'\n') => {
                saw_crlf = true;
                i += 2;
            }
            b'\n' => return false,
            _ => i += 1,
        }
    }
    saw_crlf
}

/// Failures from [`set_toml_string`].
#[derive(Debug)]
pub enum TomlEditError {
    Parse(toml_edit::TomlError),
    EmptyPath,
    Missing { path: String },
    NotTable { path: String },
    NotString { path: String },
}

impl fmt::Display for TomlEditError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Parse(err) => write!(f, "{err}"),
            Self::EmptyPath => {
                f.write_str("TOML edit path must not be empty or contain an empty segment")
            }
            Self::Missing { path } => write!(f, "TOML path `{path}` does not exist"),
            Self::NotTable { path } => {
                write!(f, "TOML path `{path}` is not a standard table")
            }
            Self::NotString { path } => write!(f, "TOML path `{path}` is not a string"),
        }
    }
}

impl std::error::Error for TomlEditError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Parse(err) => Some(err),
            Self::EmptyPath
            | Self::Missing { .. }
            | Self::NotTable { .. }
            | Self::NotString { .. } => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use toml_edit::{value, DocumentMut};

    use super::{
        cargo_declared_version, cargo_package_version_inherits_workspace, cargo_workspace_version,
        emit_toml, set_preserving_decor, set_toml_string, uses_only_crlf, DeclaredVersion,
        TomlEditError,
    };

    #[test]
    fn declared_versions_read_literal_inherited_and_absent() {
        assert_eq!(
            cargo_declared_version("[package]\nversion = \"0.4.1\"\n").expect("parse"),
            DeclaredVersion::Literal(String::from("0.4.1"))
        );
        assert_eq!(
            cargo_declared_version("[package]\nversion.workspace = true\n").expect("parse"),
            DeclaredVersion::Workspace
        );
        for absent in [
            "[workspace]\n",
            "[package]\nversion = 1\n",
            "[package]\nname = \"x\"\n",
        ] {
            assert_eq!(
                cargo_declared_version(absent).expect("parse"),
                DeclaredVersion::Absent,
                "{absent}"
            );
        }
        assert!(cargo_declared_version("[[ not toml").is_err());
        assert_eq!(
            cargo_workspace_version("[workspace.package]\nversion = \"0.4.1\"\n").expect("parse"),
            Some(String::from("0.4.1"))
        );
        assert_eq!(
            cargo_workspace_version("[workspace]\n").expect("parse"),
            None
        );
    }

    fn parse(src: &str) -> DocumentMut {
        src.parse()
            .unwrap_or_else(|err| panic!("document should parse: {err}"))
    }

    #[test]
    fn naive_assignment_drops_trailing_comment_and_padding() {
        let mut doc = parse("version = \"0.1.0\"   # keep me\n");
        doc["version"] = value("0.2.0");
        assert_eq!(doc.to_string(), "version = \"0.2.0\"\n");
    }

    #[test]
    fn helper_keeps_trailing_comment_and_padding() {
        let mut doc = parse("version = \"0.1.0\"   # keep me\n");
        set_preserving_decor(&mut doc["version"], "0.2.0");
        assert_eq!(doc.to_string(), "version = \"0.2.0\"   # keep me\n");
    }

    #[test]
    fn helper_keeps_padding_between_equals_and_value() {
        let mut doc = parse("version =    \"0.1.0\"\n");
        set_preserving_decor(&mut doc["version"], "0.2.0");
        assert_eq!(doc.to_string(), "version =    \"0.2.0\"\n");
    }

    #[test]
    fn helper_leaves_neighbor_keys_untouched() {
        let src = "\
[package]
name = \"demo\"  # name stays
version = \"0.1.0\"   # keep me
edition = \"2021\"
";
        let mut doc = parse(src);
        set_preserving_decor(&mut doc["package"]["version"], "0.2.0");
        assert_eq!(
            doc.to_string(),
            "\
[package]
name = \"demo\"  # name stays
version = \"0.2.0\"   # keep me
edition = \"2021\"
"
        );
    }

    #[test]
    fn helper_inserts_a_missing_key_with_default_spacing() {
        let mut doc = parse("[package]\nname = \"demo\"\n");
        set_preserving_decor(&mut doc["package"]["version"], "0.1.0");
        assert_eq!(
            doc.to_string(),
            "[package]\nname = \"demo\"\nversion = \"0.1.0\"\n"
        );
    }

    #[test]
    fn emit_toml_restores_crlf_and_leaves_lf_alone() {
        let crlf = "[package]\r\nversion = \"0.1.0\"\r\n";
        let mut doc = parse(crlf);
        set_preserving_decor(&mut doc["package"]["version"], "0.2.0");
        assert_eq!(
            emit_toml(&doc, crlf),
            "[package]\r\nversion = \"0.2.0\"\r\n"
        );

        let lf = "[package]\nversion = \"0.1.0\"\n";
        let mut doc = parse(lf);
        set_preserving_decor(&mut doc["package"]["version"], "0.2.0");
        assert_eq!(emit_toml(&doc, lf), "[package]\nversion = \"0.2.0\"\n");
    }

    #[test]
    fn emit_toml_keeps_a_missing_final_newline() {
        let lf = "[package]\nversion = \"0.1.0\"";
        let mut doc = parse(lf);
        set_preserving_decor(&mut doc["package"]["version"], "0.2.0");
        assert_eq!(emit_toml(&doc, lf), "[package]\nversion = \"0.2.0\"");

        let crlf = "[package]\r\nversion = \"0.1.0\"";
        let mut doc = parse(crlf);
        set_preserving_decor(&mut doc["package"]["version"], "0.2.0");
        assert_eq!(emit_toml(&doc, crlf), "[package]\r\nversion = \"0.2.0\"");
    }

    #[test]
    fn emit_toml_does_not_promote_mixed_endings() {
        let mixed = "[package]\nversion = \"0.1.0\"\r\nname = \"demo\"\n";
        let mut doc = parse(mixed);
        set_preserving_decor(&mut doc["package"]["version"], "0.2.0");
        let out = emit_toml(&doc, mixed);
        assert!(!out.contains("\r\n"), "{out:?}");
        assert_eq!(out, "[package]\nversion = \"0.2.0\"\nname = \"demo\"\n");
    }

    #[test]
    fn emit_toml_restores_crlf_around_a_multiline_string_body() {
        let src = "[package]\r\nversion = \"0.1.0\"\r\ndescription = \"\"\"\r\nkeep\r\nthis\r\n\"\"\"\r\n";
        let mut doc = parse(src);
        set_preserving_decor(&mut doc["package"]["version"], "0.2.0");
        let out = emit_toml(&doc, src);
        assert!(uses_only_crlf(&out), "expected exclusive CRLF, got {out:?}");
        assert!(out.contains("version = \"0.2.0\""));
        assert!(out.contains("keep\r\nthis"));
        assert!(!out.contains("\r\r\n"), "{out:?}");
    }

    #[test]
    fn emit_toml_keeps_utf8_when_restoring_crlf() {
        let src = "[package]\r\n# café\r\nversion = \"0.1.0\"\r\n";
        let mut doc = parse(src);
        set_preserving_decor(&mut doc["package"]["version"], "0.2.0");
        assert_eq!(
            emit_toml(&doc, src),
            "[package]\r\n# café\r\nversion = \"0.2.0\"\r\n"
        );
    }

    #[test]
    fn set_toml_string_empty_path_is_an_error() {
        let err =
            set_toml_string("[package]\nversion = \"0.1.0\"\n", &[], "0.2.0").expect_err("empty");
        assert!(matches!(err, TomlEditError::EmptyPath));
    }

    #[test]
    fn set_toml_string_empty_segment_is_an_error() {
        let src = "[package]\nversion = \"0.1.0\"\n";
        let last = set_toml_string(src, &["package", ""], "0.2.0").expect_err("empty last");
        assert!(matches!(last, TomlEditError::EmptyPath), "{last:?}");
        let mid = set_toml_string(src, &["", "version"], "0.2.0").expect_err("empty mid");
        assert!(matches!(mid, TomlEditError::EmptyPath), "{mid:?}");
    }

    #[test]
    fn set_toml_string_missing_table_is_an_error() {
        let err = set_toml_string("name = \"demo\"\n", &["package", "version"], "0.2.0")
            .expect_err("missing");
        assert!(
            matches!(err, TomlEditError::Missing { ref path } if path == "package"),
            "{err:?}"
        );
    }

    #[test]
    fn set_toml_string_scalar_parent_is_an_error() {
        let err = set_toml_string("name = \"demo\"\n", &["name", "version"], "0.2.0")
            .expect_err("scalar");
        assert!(
            matches!(err, TomlEditError::NotTable { ref path } if path == "name"),
            "{err:?}"
        );
    }

    #[test]
    fn set_toml_string_nested_scalar_parent_names_the_scalar() {
        let err = set_toml_string("name = \"demo\"\n", &["name", "sub", "version"], "0.2.0")
            .expect_err("nested scalar");
        assert!(
            matches!(err, TomlEditError::NotTable { ref path } if path == "name"),
            "{err:?}"
        );
    }

    #[test]
    fn set_toml_string_invalid_toml_is_a_parse_error() {
        let err = set_toml_string("this is not toml {", &["package", "version"], "0.2.0")
            .expect_err("parse");
        assert!(matches!(err, TomlEditError::Parse(_)), "{err:?}");
    }

    #[test]
    fn cargo_package_version_inherits_workspace_dotted_and_inline() {
        assert!(cargo_package_version_inherits_workspace(
            "[package]\nname = \"demo\"\nversion.workspace = true\n"
        )
        .unwrap());
        assert!(cargo_package_version_inherits_workspace(
            "[package]\nname = \"demo\"\nversion = { workspace = true }\n"
        )
        .unwrap());
        assert!(!cargo_package_version_inherits_workspace(
            "[package]\nname = \"demo\"\nversion = \"0.1.0\"\n"
        )
        .unwrap());
        assert!(!cargo_package_version_inherits_workspace("[package]\nname = \"demo\"\n").unwrap());
    }

    #[test]
    fn set_toml_string_table_last_key_is_an_error() {
        let err = set_toml_string("[package]\nversion = \"0.1.0\"\n", &["package"], "0.2.0")
            .expect_err("table");
        assert!(
            matches!(err, TomlEditError::NotString { ref path } if path == "package"),
            "{err:?}"
        );
    }
}