callisto-model 0.3.3

Callisto Release Engine — Core domain primitives, SemVer grammars, and plan models.
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
use std::path::PathBuf;

use schemars::JsonSchema;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};

use crate::{
    CommitSha, ConfigKey, DepKind, Diagnostic, Ecosystem, GroupName, PackageId, PublishPlan,
    Severity, TagName, Version,
};

pub const SCHEMA_VERSION: u32 = 1;

/// Trait for all structured JSON report payloads.
pub trait Report: Serialize + DeserializeOwned + Send + Sync + 'static {
    const COMMAND: &'static str;
    fn schema_version(&self) -> u32;
    fn diagnostics(&self) -> &[Diagnostic];
}

impl Report for PublishPlan {
    const COMMAND: &'static str = "plan-publish";

    fn schema_version(&self) -> u32 {
        self.schema_version
    }

    fn diagnostics(&self) -> &[Diagnostic] {
        &self.diagnostics
    }
}

/// Publish execution report output from `callisto publish --format json`
/// (non-dry-run only). Distinct from [`PublishPlan`], which describes what
/// *would* be published (used both by `plan-publish` and by `publish
/// --dry-run`); [`PublishReport`] instead records what actually happened for
/// every package `publish` attempted to send to its registry.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct PublishReport {
    pub schema_version: u32,
    pub attempts: Vec<PublishAttempt>,

    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub diagnostics: Vec<Diagnostic>,
}

impl Report for PublishReport {
    const COMMAND: &'static str = "publish";

    fn schema_version(&self) -> u32 {
        self.schema_version
    }

    fn diagnostics(&self) -> &[Diagnostic] {
        &self.diagnostics
    }
}

/// The outcome of one package's actual publish attempt, as recorded in a
/// [`PublishReport`].
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct PublishAttempt {
    pub package: PackageId,
    pub version: Version,
    #[serde(flatten)]
    pub result: PublishAttemptResult,
}

/// Per-package result of a real (non-dry-run) publish attempt. Mirrors
/// [`crate::PublishOutcome`] for the success cases, plus a `Failed` case
/// carrying the registry error's message for packages that could not be
/// published.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "status", rename_all = "camelCase")]
pub enum PublishAttemptResult {
    /// The package/version was newly uploaded to the registry.
    Published,
    /// The package/version was already present on the registry.
    AlreadyPublished,
    /// The publish attempt failed; `error` is the registry error's message.
    Failed { error: String },
}

impl PublishAttemptResult {
    /// Returns `true` if this result represents a failure.
    pub fn is_failure(&self) -> bool {
        matches!(self, PublishAttemptResult::Failed { .. })
    }
}

impl PublishReport {
    /// Returns `true` if any attempt in this report resulted in a failure.
    pub fn has_failures(&self) -> bool {
        self.attempts.iter().any(|a| a.result.is_failure())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{PackageId, Version, VersionGrammar};

    fn pkg() -> PackageId {
        PackageId::parse("test-pkg").unwrap()
    }

    fn ver() -> Version {
        Version::parse("1.0.0", VersionGrammar::SemVer).unwrap()
    }

    fn attempt(result: PublishAttemptResult) -> PublishAttempt {
        PublishAttempt {
            package: pkg(),
            version: ver(),
            result,
        }
    }

    fn report(attempts: Vec<PublishAttempt>) -> PublishReport {
        PublishReport {
            schema_version: SCHEMA_VERSION,
            attempts,
            diagnostics: vec![],
        }
    }

    #[test]
    fn has_failures_returns_true_when_any_attempt_failed() {
        let r = report(vec![
            attempt(PublishAttemptResult::Published),
            attempt(PublishAttemptResult::Failed {
                error: "registry unavailable".to_string(),
            }),
        ]);
        assert!(r.has_failures());
    }

    #[test]
    fn has_failures_returns_true_when_all_attempts_failed() {
        let r = report(vec![
            attempt(PublishAttemptResult::Failed {
                error: "auth error".to_string(),
            }),
            attempt(PublishAttemptResult::Failed {
                error: "network error".to_string(),
            }),
        ]);
        assert!(r.has_failures());
    }

    #[test]
    fn has_failures_returns_false_when_all_attempts_succeeded() {
        let r = report(vec![
            attempt(PublishAttemptResult::Published),
            attempt(PublishAttemptResult::AlreadyPublished),
        ]);
        assert!(!r.has_failures());
    }

    #[test]
    fn has_failures_returns_false_for_empty_report() {
        let r = report(vec![]);
        assert!(!r.has_failures());
    }

    #[test]
    fn is_failure_is_true_only_for_failed_variant() {
        assert!(PublishAttemptResult::Failed {
            error: "oops".to_string()
        }
        .is_failure());
        assert!(!PublishAttemptResult::Published.is_failure());
        assert!(!PublishAttemptResult::AlreadyPublished.is_failure());
    }
}

/// Version report output from `callisto version --format json`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct VersionReport {
    pub schema_version: u32,
    pub bumps: Vec<BumpRecord>,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub lockfile_refresh_results: Option<Vec<LockfileRefreshResult>>,

    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub diagnostics: Vec<Diagnostic>,
}

impl Report for VersionReport {
    const COMMAND: &'static str = "version";

    fn schema_version(&self) -> u32 {
        self.schema_version
    }

    fn diagnostics(&self) -> &[Diagnostic] {
        &self.diagnostics
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct BumpRecord {
    pub package: PackageId,
    pub from: Version,
    pub to: Version,
    pub severity: Severity,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub governed_by: Option<ConfigKey>,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reason: Option<BumpReason>,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "kind", rename_all = "camelCase")]
#[non_exhaustive]
pub enum BumpReason {
    Changeset {
        changesets: Vec<String>,
    },
    Inference {
        commits: usize,
        remapped: bool,
    },
    FixedGroupUnion {
        group: GroupName,
    },
    LinkedGroupUnion {
        group: GroupName,
    },
    Cascade {
        via: PackageId,
        dep_kind: DepKind,
        spec: String,
        dependency_to: Version,
    },
    PeerEscalation {
        via: PackageId,
        spec: String,
    },
    PreRelease {
        tag: String,
    },
    NewGroupMember {
        group: GroupName,
    },
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct LockfileRefreshResult {
    pub filename: PathBuf,
    pub refresh_command: String,
    pub success: bool,
    pub exit_code: Option<i32>,
}

/// Status report output from `callisto status --format json`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct StatusReport {
    pub schema_version: u32,
    pub packages: Vec<StatusPackageRecord>,

    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub diagnostics: Vec<Diagnostic>,
}

impl Report for StatusReport {
    const COMMAND: &'static str = "status";

    fn schema_version(&self) -> u32 {
        self.schema_version
    }

    fn diagnostics(&self) -> &[Diagnostic] {
        &self.diagnostics
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct StatusPackageRecord {
    pub package: PackageId,
    pub current_version: Version,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last_tag: Option<TagName>,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub pending_severity: Option<Severity>,

    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub changed_since_last_tag: bool,

    pub pending_changesets: Vec<String>,
}

/// Snapshot report output from `callisto snapshot --format json`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct SnapshotReport {
    pub schema_version: u32,
    pub snapshot_tag: String,
    pub bumps: Vec<BumpRecord>,

    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub diagnostics: Vec<Diagnostic>,
}

impl Report for SnapshotReport {
    const COMMAND: &'static str = "snapshot";

    fn schema_version(&self) -> u32 {
        self.schema_version
    }

    fn diagnostics(&self) -> &[Diagnostic] {
        &self.diagnostics
    }
}

/// Compose PR body report output from `callisto compose-pr-body --format json`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ComposePrBodyReport {
    pub schema_version: u32,
    pub pr_body: String,

    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub diagnostics: Vec<Diagnostic>,
}

impl Report for ComposePrBodyReport {
    const COMMAND: &'static str = "compose-pr-body";

    fn schema_version(&self) -> u32 {
        self.schema_version
    }

    fn diagnostics(&self) -> &[Diagnostic] {
        &self.diagnostics
    }
}

/// Validate report output from `callisto validate --format json`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ValidateReport {
    pub schema_version: u32,
    pub valid: bool,

    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub diagnostics: Vec<Diagnostic>,
}

impl Report for ValidateReport {
    const COMMAND: &'static str = "validate";

    fn schema_version(&self) -> u32 {
        self.schema_version
    }

    fn diagnostics(&self) -> &[Diagnostic] {
        &self.diagnostics
    }
}

/// Tag report output from `callisto tag --format json`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct TagReport {
    pub schema_version: u32,
    pub created_tags: Vec<CreatedTag>,

    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub diagnostics: Vec<Diagnostic>,
}

impl Report for TagReport {
    const COMMAND: &'static str = "tag";

    fn schema_version(&self) -> u32 {
        self.schema_version
    }

    fn diagnostics(&self) -> &[Diagnostic] {
        &self.diagnostics
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct CreatedTag {
    pub package: PackageId,
    pub tag_name: TagName,
    pub sha: CommitSha,
}

/// Init report output from `callisto init --format json`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct InitReport {
    pub schema_version: u32,
    /// `true` only on a first run, when `callisto.toml` did not exist yet and
    /// was written directly. `false` on every re-run (§18 Q5.4 mechanism 1),
    /// including a re-run that applies detected drift — that case is
    /// reported through `diff`, not this flag.
    pub initialized: bool,
    pub config_path: PathBuf,
    /// Drift between the currently-discovered workspace state and what is
    /// already recorded in `callisto.toml`, and whether that drift was
    /// applied this run (docs/00-design.md §18 Q5.4 mechanism 1: re-running
    /// `init` is the reconcile flow — it re-detects, reports a diff, and
    /// applies only with confirmation). Empty/`applied: false` when there is
    /// nothing to reconcile, including on a first run.
    #[serde(default)]
    pub diff: InitDiff,

    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub diagnostics: Vec<Diagnostic>,
}

impl Report for InitReport {
    const COMMAND: &'static str = "init";

    fn schema_version(&self) -> u32 {
        self.schema_version
    }

    fn diagnostics(&self) -> &[Diagnostic] {
        &self.diagnostics
    }
}

/// The reconcile diff computed by a `callisto init` re-run (§18 Q5.4
/// mechanism 1). Carries *what would change*, not just whether something
/// changed, so a wrapper (CLI text renderer, `--format json` consumer) can
/// narrate the drift instead of a bare boolean.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct InitDiff {
    /// Ecosystems present in the discovered workspace but not yet recorded
    /// against the existing `callisto.toml` (e.g. a `package.json` added to
    /// a previously Cargo-only workspace, or `napi.targets` appearing).
    /// Sorted for determinism. Empty when there is no drift to reconcile.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub new_ecosystems: Vec<Ecosystem>,
    /// `true` when `new_ecosystems` was non-empty and was written to
    /// `callisto.toml` this run (`InitOptions::yes`). `false` when the diff
    /// was only reported (dry-preview) or when there was no diff to apply.
    #[serde(default)]
    pub applied: bool,
}