verctl-core 0.9.6

Core configuration, manifest, fragment, changelog, and version domain logic for verctl
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
use std::collections::{BTreeMap, BTreeSet};
use std::ffi::OsStr;
use std::path::Path;

use ctl_core::validate;

use super::{
    Config, NAME_PLACEHOLDER, PLACEHOLDER, PackageSpec, PinPattern, Prepare, Prerelease,
    ReleaseCheck,
};
use crate::publisher_spec::{self as publisher, PublisherSpec, RELEASE_ONLY};

/// Where the version goes, said once: no placeholder is a spelling that tracks
/// nothing, two is a spelling with no single version to put anywhere.
pub(super) fn every_pattern_says_where_the_version_goes(
    patterns: &BTreeMap<String, PinPattern>,
    _: &Config,
) -> garde::Result {
    for (id, pattern) in patterns {
        let found = pattern.r#match.matches(PLACEHOLDER).count();
        if found != 1 {
            return Err(garde::Error::new(format!(
                "{id}: match says {PLACEHOLDER} {found} times, and must say it once"
            )));
        }
    }
    Ok(())
}

/// Where the version starts and ends, as the pattern declares it.
/// `{version}` is a version verctl writes: digits and dots, then an optional
/// candidate prerelease `-<tag>.<count>`. Without literal text on each side
/// that a version cannot contain, the pattern could end a version early or
/// start it late.
///
/// For example, `v{version}.5x` reads `v1.2.3.5x` as 1.2.3 and keeps `.5x`
/// after the new version. The file cannot settle it, because
/// `download/v{version}-linux` against `download/v1.2.3-linux-x64.tar.gz` is a
/// correct pin of 1.2.3.
pub(super) fn every_pattern_bounds_the_version(
    patterns: &BTreeMap<String, PinPattern>,
    _: &Config,
) -> garde::Result {
    for (id, pattern) in patterns {
        let Some((before, after)) = pattern.r#match.split_once(PLACEHOLDER) else {
            continue;
        };
        if after.contains(PLACEHOLDER) {
            continue;
        }
        if let Some(problem) = unbounded(before, after, pattern) {
            return Err(garde::Error::new(format!("{id}: {problem}")));
        }
    }
    Ok(())
}

/// What leaves one side of the placeholder open, if anything does.
///
/// A side's literal is the text between `{version}` and the pattern's edge on
/// that side. A version and its prerelease or build contain only digits, `.`,
/// `-`, and `+`. So the side is bounded when its literal contains some other
/// character. Under `whole_line` the line start or end bounds a side whose
/// literal is empty.
///
/// The character next to `{version}` also matters. A digit there, or a `.`
/// with a digit beyond it, lets the regex end a version early or start it
/// late. `v{version}.5x` reads `v1.2.3.5x` as 1.2.3, so that side is open.
fn unbounded(before: &str, after: &str, pattern: &PinPattern) -> Option<String> {
    let sides = [
        (
            before,
            before.chars().rev().collect::<String>(),
            "before",
            "starts",
        ),
        (after, after.to_owned(), "after", "ends"),
    ];
    for (text, outward, side, edge) in sides {
        if text.is_empty() {
            if pattern.whole_line {
                continue;
            }
            return Some(format!(
                "nothing {side} {PLACEHOLDER} marks where the version {edge}: add the text {side} it to match, or set whole_line: true"
            ));
        }
        if text
            .chars()
            .all(|next| matches!(next, '0'..='9' | '.' | '-' | '+'))
        {
            return Some(format!(
                "{text:?} {side} {PLACEHOLDER} contains only digits, '.', '-', and '+', which a version can continue into: add more of the text {side} it to match"
            ));
        }
        let mut nearest = outward.chars();
        if matches!(
            (nearest.next(), nearest.next()),
            (Some('0'..='9'), _) | (Some('.'), Some('0'..='9'))
        ) {
            return Some(format!(
                "{text:?} {side} {PLACEHOLDER} puts a digit, or a '.' and a digit, next to the version, so a longer version would match only in part: put a character other than a digit or '.' next to {PLACEHOLDER}"
            ));
        }
    }
    None
}

/// A declared pattern nothing lists is dead configuration instead of a spare.
/// It reads like a tracked spelling while no file is checked against it.
pub(super) fn every_pattern_is_listed(
    patterns: &BTreeMap<String, PinPattern>,
    config: &Config,
) -> garde::Result {
    let listed: BTreeSet<&str> = config
        .pins
        .iter()
        .flat_map(|pin| pin.pattern_ids.iter().map(String::as_str))
        .collect();
    for id in patterns.keys() {
        if !listed.contains(id.as_str()) {
            return Err(garde::Error::new(format!(
                "{id:?} is declared and no pin lists it"
            )));
        }
    }
    Ok(())
}

/// A name is only a name if something declares it. `none` names release-only
/// packages. A `publishers.none` recipe could never run, because every package
/// that names it uploads nothing.
pub(super) fn none_is_not_a_publisher(
    publishers: &BTreeMap<String, PublisherSpec>,
    _: &Config,
) -> garde::Result {
    if publishers.contains_key(RELEASE_ONLY) {
        return Err(garde::Error::new(
            "publishers.none is reserved for release-only packages; rename that publisher",
        ));
    }
    Ok(())
}

/// A key under a package that nothing reads. Ignoring `publsher: none` would
/// publish a package to its registry that the file meant to release on GitHub
/// only.
pub(super) fn no_unknown_package_keys(packages: &[PackageSpec], _: &Config) -> garde::Result {
    for package in packages {
        if !package.unknown.is_empty() {
            let keys: Vec<&str> = package.unknown.iter().map(String::as_str).collect();
            return Err(garde::Error::new(format!(
                "package {:?} has {} verctl does not read: {}; check the spelling",
                package.name,
                if keys.len() == 1 { "a key" } else { "keys" },
                keys.join(", ")
            )));
        }
    }
    Ok(())
}

/// The problem with an `owners` list, if it has one: a list with no login
/// proves nothing, and a login listed twice is a typo or a stale edit.
fn owners_problem(owners: &[String]) -> Option<String> {
    if owners.is_empty() {
        return Some(
            "must list at least one crates.io login; omit the key to expect no owners".to_owned(),
        );
    }
    let mut seen = BTreeSet::new();
    owners
        .iter()
        .find(|login| !seen.insert(login.as_str()))
        .map(|login| format!("lists {login:?} twice; remove one"))
}

/// The top-level `owners`, when set.
pub(super) fn owners_are_logins(owners: &[String], _: &Config) -> garde::Result {
    owners_problem(owners).map_or_else(|| Ok(()), |problem| Err(garde::Error::new(problem)))
}

/// Each package's own `owners`, named as `packages[<name>].owners`, because
/// the field is on one entry of the list.
pub(super) fn package_owners_are_logins(packages: &[PackageSpec], _: &Config) -> garde::Result {
    for package in packages {
        if let Some(problem) = package.owners.as_deref().and_then(owners_problem) {
            return Err(garde::Error::new(format!(
                "packages[{name}].owners {problem}",
                name = package.name
            )));
        }
    }
    Ok(())
}

/// `release_checks` names each check once: a repeated name is a typo or a
/// stale edit.
pub(super) fn each_release_check_once(checks: &[ReleaseCheck], _: &Config) -> garde::Result {
    let mut seen = BTreeSet::new();
    checks
        .iter()
        .find(|check| !seen.insert(check.name()))
        .map_or_else(
            || Ok(()),
            |check| {
                Err(garde::Error::new(format!(
                    "lists {:?} twice; remove one",
                    check.name()
                )))
            },
        )
}

/// `release_workflow` is the file name crates.io compares, so a path or a
/// file that is not a workflow could never match an entry.
pub(super) fn a_workflow_file_name(name: &str, config: &Config) -> garde::Result {
    if validate::one_file_name(name, config).is_err() {
        return Err(garde::Error::new(
            "must be a file name under .github/workflows/, such as release.yml, with no directory",
        ));
    }
    if !Path::new(name)
        .extension()
        .is_some_and(|extension| extension == "yml" || extension == "yaml")
    {
        return Err(garde::Error::new(
            "must name a workflow file, ending in .yml or .yaml",
        ));
    }
    Ok(())
}

/// A candidate tag that is not lowercase ASCII letters. `rc1` would make the
/// version `0.8.0-rc1.1`, which counts on no further, and `rc.1` would hide
/// the count inside the tag.
pub(super) fn a_prerelease_is_lowercase_letters(
    packages: &[PackageSpec],
    _: &Config,
) -> garde::Result {
    for package in packages {
        if let Some(tag) = package.prerelease.as_ref().filter(|tag| !tag.is_valid()) {
            return Err(garde::Error::new(format!(
                "package {:?} has prerelease {:?}, which is not a tag; {}",
                package.name,
                tag.as_str(),
                Prerelease::RULE
            )));
        }
    }
    Ok(())
}

/// `publisher: none`, declared or inferred from its version source, uploads
/// nothing. A registry or an upload command on the same package would be a
/// setting nothing reads.
pub(super) fn release_only_names_no_registry(
    packages: &[PackageSpec],
    config: &Config,
) -> garde::Result {
    for package in packages {
        if publisher::publisher_name(config, package) == RELEASE_ONLY
            && (package.registry.is_some() || package.publish.is_some())
        {
            return Err(garde::Error::new(format!(
                "package {:?} has publisher: none, which uploads to no registry; remove its registry and publish",
                package.name
            )));
        }
    }
    Ok(())
}

/// An option belongs to one publisher. One the package's publisher does not
/// have would be a setting nothing reads, so it fails here, in that
/// publisher's words.
pub(super) fn publish_options_fit_the_publisher(
    packages: &[PackageSpec],
    config: &Config,
) -> garde::Result {
    for package in packages {
        if let Err(problem) = publisher::check_options(config, package) {
            return Err(garde::Error::new(problem));
        }
    }
    Ok(())
}

/// `marker: true` shows where the version is, so a `driver` or driver fields
/// beside it would be a second answer nothing reads. `after` is the
/// exception: it is what runs after the bump, not where the version is.
pub(super) fn a_marker_names_no_driver(packages: &[PackageSpec], _: &Config) -> garde::Result {
    for package in packages {
        let spec = &package.spec;
        if package.marker
            && (package.driver.is_some()
                || spec.format.is_some()
                || spec.keys.is_some()
                || spec.read.is_some()
                || spec.write.is_some())
        {
            return Err(garde::Error::new(format!(
                "package {:?} has marker: true, which reads the version from its marker line; remove its driver, format, keys, read, and write",
                package.name
            )));
        }
    }
    Ok(())
}

/// verctl has no stock Python publisher, so a `pyproject.toml` package that
/// names none would release a tag while nothing reached the Python Package
/// Index. It names one, through `publisher`, `publish`, or a `publishers`
/// entry under its own name, or says `publisher: none` to release on GitHub
/// only.
pub(super) fn python_names_its_publisher(
    packages: &[PackageSpec],
    config: &Config,
) -> garde::Result {
    for package in packages {
        if package.path.file_name() == Some(OsStr::new("pyproject.toml"))
            && package.publisher.is_none()
            && package.publish_command().is_none()
            && !config.publishers.contains_key(&package.name)
        {
            return Err(garde::Error::new(format!(
                "package {:?} needs a publisher: verctl has no stock Python publisher; set publisher to a publishers entry, or to none to release on GitHub only",
                package.name
            )));
        }
    }
    Ok(())
}

pub(super) fn each_id_is_declared(ids: &[String], config: &Config) -> garde::Result {
    for id in ids {
        if !config.patterns.contains_key(id) {
            return Err(garde::Error::new(format!("no patterns.{id}")));
        }
    }
    Ok(())
}

/// Listing a spelling twice does not set a count: arity belongs to the pattern.
pub(super) fn no_id_is_listed_twice(ids: &[String], _: &Config) -> garde::Result {
    let mut seen = BTreeSet::new();
    for id in ids {
        if !seen.insert(id.as_str()) {
            return Err(garde::Error::new(format!("lists {id:?} twice")));
        }
    }
    Ok(())
}

/// A pin with neither a `tool` nor a pattern names a file and asks for nothing.
pub(super) fn something_to_rewrite(
    tool: Option<&str>,
) -> impl Fn(&[String], &Config) -> garde::Result + '_ {
    move |ids, _| {
        if tool.is_some() || !ids.is_empty() {
            return Ok(());
        }
        Err(garde::Error::new("needs a tool or a pattern to rewrite"))
    }
}

/// A tag template may only say `{version}` and `{name}`, must say at least one
/// of them, and must say `{version}`. A name with no version cannot date a
/// release.
pub(super) fn tag_template_placeholders(template: &str, _: &Config) -> garde::Result {
    let mut saw_version = false;
    let mut saw_name = false;
    let mut rest = template;
    while let Some(start) = rest.find('{') {
        let after = &rest[start + 1..];
        let Some(end) = after.find('}') else {
            return Err(garde::Error::new("has an opening '{' with no closing '}'"));
        };
        let name = &after[..end];
        match name {
            "version" => saw_version = true,
            "name" => saw_name = true,
            _ => {
                return Err(garde::Error::new(format!(
                    "names {{{name}}}, and the only placeholders are {PLACEHOLDER} and {NAME_PLACEHOLDER}"
                )));
            }
        }
        rest = &after[end + 1..];
    }
    if !saw_version && !saw_name {
        return Err(garde::Error::new(format!(
            "has no placeholder — put {PLACEHOLDER} where the version goes"
        )));
    }
    if !saw_version {
        return Err(garde::Error::new(format!(
            "must say {PLACEHOLDER} — a tag with only {NAME_PLACEHOLDER} cannot date a release"
        )));
    }
    let leftover = template
        .replace(PLACEHOLDER, "")
        .replace(NAME_PLACEHOLDER, "");
    if leftover.contains('{') || leftover.contains('}') {
        return Err(garde::Error::new(format!(
            "has a '{{' or '}}' that is not {PLACEHOLDER} or {NAME_PLACEHOLDER}"
        )));
    }
    let _ = saw_name;
    Ok(())
}

/// `prepare.version_label` labeled the Version PR until nothing read the
/// label; a file that still sets it is told to delete it.
pub(super) fn no_version_label(prepare: &Prepare, _: &Config) -> garde::Result {
    if prepare.version_label {
        return Err(garde::Error::new(
            "prepare.version_label is removed: the Version PR is no longer labeled; delete this key",
        ));
    }
    Ok(())
}

/// A declared hook argv must name at least a non-empty program.
pub(super) fn non_empty_hook_argv(argv: &[String], _: &Config) -> garde::Result {
    if argv.is_empty() || argv[0].trim().is_empty() {
        return Err(garde::Error::new(
            "must name a program (at least one non-empty argument), or be omitted",
        ));
    }
    Ok(())
}