cargo-rail 0.12.0

Graph-aware testing, dependency unification, and crate extraction for Rust monorepos
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//! Error handling with contextual messages and CI-friendly exit codes.
//!
//! Exit code semantics: 0 = success, 1 = check mode found changes, 2 = error.
//! Errors carry optional help text that guides users toward resolution.

use std::fmt;
use std::io;
use std::path::PathBuf;

/// Exit codes for cargo-rail
///
/// Exit codes follow CI-friendly semantics:
/// - 0 = Success (or check passed with no changes needed)
/// - 1 = Check mode found changes would be made (not an error, but actionable)
/// - 2 = Error occurred (actual failure)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExitCode {
  /// Success - everything is good
  Success,
  /// Check failed - changes would be made (use in --check mode)
  CheckFailed,
  /// Error occurred (user or system error)
  Error,
  /// Custom exit code (e.g., propagated from subprocess)
  Custom(i32),
}

impl ExitCode {
  /// Convert to i32 for process exit
  pub fn as_i32(self) -> i32 {
    match self {
      ExitCode::Success => 0,
      ExitCode::CheckFailed => 1,
      ExitCode::Error => 2,
      ExitCode::Custom(code) => code,
    }
  }
}

/// Main error type for cargo-rail
#[derive(Debug)]
pub enum RailError {
  /// Configuration errors
  Config(ConfigError),

  /// Git operation errors
  Git(GitError),

  /// I/O errors
  Io(io::Error),

  /// Generic error with message and optional context
  Message {
    /// Error message
    message: String,
    /// Additional context about the error
    context: Option<String>,
    /// Help text to guide the user
    help: Option<String>,
  },

  /// Check mode found pending changes (not an error, but exits with code 1)
  ///
  /// Used by --check commands to signal that changes would be made.
  /// This is not a failure, but CI should treat it as "action needed".
  CheckHasPendingChanges,

  /// Exit with specific code (no error message printed)
  ///
  /// Used for:
  /// - Propagating subprocess exit codes (e.g., cargo test failures)
  /// - Silent exits after JSON output has been written
  /// - Any case where we need a specific exit code without error output
  ExitWithCode {
    /// The exit code to use
    code: i32,
  },
}

impl RailError {
  /// Create a simple error message
  pub fn message(msg: impl Into<String>) -> Self {
    RailError::Message {
      message: msg.into(),
      context: None,
      help: None,
    }
  }

  /// Create an error with help text
  pub fn with_help(msg: impl Into<String>, help: impl Into<String>) -> Self {
    RailError::Message {
      message: msg.into(),
      context: None,
      help: Some(help.into()),
    }
  }

  /// Add context to an existing error
  pub fn context(self, ctx: impl Into<String>) -> Self {
    let ctx_str = ctx.into();
    match self {
      RailError::Message { message, context, help } => RailError::Message {
        message,
        context: Some(context.map(|c| format!("{}\n{}", ctx_str, c)).unwrap_or(ctx_str)),
        help,
      },
      _ => self,
    }
  }

  /// Get the appropriate exit code for this error
  pub fn exit_code(&self) -> ExitCode {
    match self {
      RailError::CheckHasPendingChanges => ExitCode::CheckFailed,
      RailError::ExitWithCode { code } => ExitCode::Custom(*code),
      _ => ExitCode::Error,
    }
  }

  /// Get contextual help message for this error
  pub fn help_message(&self) -> Option<String> {
    match self {
      RailError::Config(e) => e.help_message(),
      RailError::Git(e) => e.help_message(),
      RailError::Message { help, .. } => help.clone(),
      _ => None,
    }
  }
}

impl fmt::Display for RailError {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    match self {
      RailError::Config(e) => write!(f, "{}", e),
      RailError::Git(e) => write!(f, "{}", e),
      RailError::Io(e) => write!(f, "{}", e),
      RailError::Message { message, context, .. } => {
        write!(f, "{}", message)?;
        if let Some(ctx) = context {
          write!(f, "\n{}", ctx)?;
        }
        Ok(())
      }
      RailError::CheckHasPendingChanges => Ok(()), // Silent - CI signal
      RailError::ExitWithCode { .. } => Ok(()),    // Silent - exit code only
    }
  }
}

impl std::error::Error for RailError {
  fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
    match self {
      RailError::Io(e) => Some(e),
      _ => None,
    }
  }
}

impl From<io::Error> for RailError {
  fn from(err: io::Error) -> Self {
    RailError::Io(err)
  }
}

impl From<String> for RailError {
  fn from(msg: String) -> Self {
    RailError::message(msg)
  }
}

impl From<&str> for RailError {
  fn from(msg: &str) -> Self {
    RailError::message(msg)
  }
}

impl From<toml_edit::TomlError> for RailError {
  fn from(err: toml_edit::TomlError) -> Self {
    RailError::message(format!("invalid TOML: {}", err))
  }
}

impl From<cargo_metadata::Error> for RailError {
  fn from(err: cargo_metadata::Error) -> Self {
    RailError::message(format!("cargo metadata failed: {}", err))
  }
}

impl From<std::num::ParseIntError> for RailError {
  fn from(err: std::num::ParseIntError) -> Self {
    RailError::message(format!("invalid number: {}", err))
  }
}

impl From<toml_edit::de::Error> for RailError {
  fn from(err: toml_edit::de::Error) -> Self {
    RailError::message(format!("invalid TOML: {}", err))
  }
}

impl From<toml_edit::ser::Error> for RailError {
  fn from(err: toml_edit::ser::Error) -> Self {
    RailError::message(format!("TOML serialization failed: {}", err))
  }
}

impl From<serde_json::Error> for RailError {
  fn from(err: serde_json::Error) -> Self {
    RailError::message(format!("invalid JSON: {}", err))
  }
}

impl From<std::str::Utf8Error> for RailError {
  fn from(err: std::str::Utf8Error) -> Self {
    RailError::message(format!("invalid UTF-8: {}", err))
  }
}

impl From<std::string::FromUtf8Error> for RailError {
  fn from(err: std::string::FromUtf8Error) -> Self {
    RailError::message(format!("invalid UTF-8: {}", err))
  }
}

impl From<std::path::StripPrefixError> for RailError {
  fn from(err: std::path::StripPrefixError) -> Self {
    RailError::message(format!("path error: {}", err))
  }
}

impl From<std::env::VarError> for RailError {
  fn from(err: std::env::VarError) -> Self {
    RailError::message(format!("environment variable error: {}", err))
  }
}

/// Configuration-related errors
#[derive(Debug)]
pub enum ConfigError {
  /// rail.toml not found
  NotFound {
    /// Workspace root where config was searched
    workspace_root: PathBuf,
  },

  /// Config file exists but failed to parse
  ParseError {
    /// Path to the config file
    path: PathBuf,
    /// Parse error message
    message: String,
  },

  /// Missing required field
  MissingField {
    /// Name of the missing field
    field: String,
  },

  /// Crate not found in configuration
  CrateNotFound {
    /// Name of the crate that wasn't found
    name: String,
  },

  /// Invalid configuration value
  InvalidValue {
    /// Field name
    field: String,
    /// Error message
    message: String,
  },

  /// Invalid field configuration
  InvalidField {
    /// Field name
    field: String,
    /// Reason why it's invalid
    reason: String,
  },

  /// Invalid glob pattern
  InvalidGlobPattern {
    /// The invalid pattern
    pattern: String,
    /// Error message
    message: String,
  },
}

impl ConfigError {
  fn help_message(&self) -> Option<String> {
    match self {
      ConfigError::NotFound { .. } => Some("run 'cargo rail init' to create configuration".to_string()),
      ConfigError::ParseError { .. } => Some("check the config file syntax and fix the error".to_string()),
      ConfigError::CrateNotFound { name } => Some(format!(
        "run 'cargo rail split --check' to list configured crates (did you mean '{}'?)",
        name
      )),
      ConfigError::InvalidValue { field, .. } => Some(format!("check the '{}' field in your config file", field)),
      ConfigError::InvalidField { field, .. } => Some(format!("check the '{}' field in your config file", field)),
      ConfigError::InvalidGlobPattern { pattern, .. } => {
        Some(format!("fix or remove the invalid glob pattern: '{}'", pattern))
      }
      ConfigError::MissingField { field } => Some(format!("add the required '{}' field to your config file", field)),
    }
  }
}

impl fmt::Display for ConfigError {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    match self {
      ConfigError::NotFound { workspace_root } => {
        write!(
          f,
          "no configuration found in {}\n       searched: rail.toml, .rail.toml, .cargo/rail.toml, .config/rail.toml",
          workspace_root.display()
        )
      }
      ConfigError::ParseError { path, message } => {
        write!(f, "failed to parse config file {}: {}", path.display(), message)
      }
      ConfigError::MissingField { field } => {
        write!(f, "missing required field: {}", field)
      }
      ConfigError::CrateNotFound { name } => {
        write!(f, "crate '{}' not found in configuration", name)
      }
      ConfigError::InvalidValue { field, message } => {
        write!(f, "invalid value for '{}': {}", field, message)
      }
      ConfigError::InvalidField { field, reason } => {
        write!(f, "invalid configuration for '{}': {}", field, reason)
      }
      ConfigError::InvalidGlobPattern { pattern, message } => {
        write!(f, "invalid glob pattern '{}': {}", pattern, message)
      }
    }
  }
}

/// Git operation errors
#[derive(Debug)]
pub enum GitError {
  /// Git command failed
  CommandFailed {
    /// Command that was executed
    command: String,
    /// Standard error output
    stderr: String,
  },

  /// Repository not found
  RepoNotFound {
    /// Path where repository was expected
    path: PathBuf,
  },

  /// Commit not found
  CommitNotFound {
    /// SHA of the missing commit
    sha: String,
  },

  /// Push failed
  PushFailed {
    /// Remote name
    remote: String,
    /// Branch name
    branch: String,
    /// Failure reason
    reason: String,
  },

  /// Worktree has uncommitted changes
  DirtyWorktree {
    /// List of dirty files
    files: Vec<String>,
  },
}

impl GitError {
  fn help_message(&self) -> Option<String> {
    match self {
      GitError::PushFailed { reason, .. } => {
        if reason.contains("non-fast-forward") {
          Some("pull first, or use --force".to_string())
        } else if reason.contains("permission denied") || reason.contains("403") {
          Some("check SSH key and repository permissions".to_string())
        } else {
          None
        }
      }
      GitError::RepoNotFound { path } => Some(format!("run 'git init {}' or verify the path", path.display())),
      GitError::DirtyWorktree { .. } => Some("commit or stash changes, or use --allow-dirty".to_string()),
      _ => None,
    }
  }
}

impl fmt::Display for GitError {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    match self {
      GitError::CommandFailed { command, stderr } => {
        let stderr = stderr.trim();
        if stderr.is_empty() {
          write!(f, "git {} failed", command)
        } else {
          write!(f, "git {} failed: {}", command, stderr)
        }
      }
      GitError::RepoNotFound { path } => {
        write!(f, "not a git repository: {}", path.display())
      }
      GitError::CommitNotFound { sha } => {
        write!(f, "commit not found: {}", sha)
      }
      GitError::PushFailed { remote, branch, reason } => {
        write!(f, "push to {}/{} failed: {}", remote, branch, reason.trim())
      }
      GitError::DirtyWorktree { files } => {
        let count = files.len();
        if count <= 5 {
          write!(f, "working tree has uncommitted changes:\n{}", files.join("\n"))
        } else {
          let shown: Vec<_> = files.iter().take(5).cloned().collect();
          write!(
            f,
            "working tree has uncommitted changes:\n{}\n  ... and {} more",
            shown.join("\n"),
            count - 5
          )
        }
      }
    }
  }
}

/// Result type alias for cargo-rail
pub type RailResult<T> = Result<T, RailError>;

/// Helper trait to add context to Results
pub trait ResultExt<T> {
  /// Add context to an error result
  fn context(self, ctx: impl Into<String>) -> RailResult<T>;

  /// Add context using a closure (lazy evaluation)
  fn with_context<F>(self, f: F) -> RailResult<T>
  where
    F: FnOnce() -> String;
}

impl<T, E> ResultExt<T> for Result<T, E>
where
  E: Into<RailError>,
{
  fn context(self, ctx: impl Into<String>) -> RailResult<T> {
    self.map_err(|e| e.into().context(ctx))
  }

  fn with_context<F>(self, f: F) -> RailResult<T>
  where
    F: FnOnce() -> String,
  {
    self.map_err(|e| e.into().context(f()))
  }
}

/// Structured JSON error for machine-readable output
#[derive(serde::Serialize)]
struct JsonError {
  error: bool,
  code: i32,
  message: String,
  #[serde(skip_serializing_if = "Option::is_none")]
  context: Option<String>,
  #[serde(skip_serializing_if = "Option::is_none")]
  help: Option<String>,
}

/// Print an error to stderr with optional help text
///
/// In JSON mode, outputs a structured JSON error object instead of text.
pub fn print_error(error: &RailError) {
  // These are not errors to display - they're exit code signals
  // CheckHasPendingChanges: CI signal for "changes would be made"
  // ExitWithCode: subprocess errors or silent exits after JSON output
  if matches!(
    error,
    RailError::CheckHasPendingChanges | RailError::ExitWithCode { .. }
  ) {
    return;
  }

  if crate::output::is_json_mode() {
    print_error_json(error);
  } else {
    crate::error!("{}", error);

    if let Some(help) = error.help_message() {
      crate::help!("{}", help);
    }
  }
}

/// Print error as structured JSON to stdout
fn print_error_json(error: &RailError) {
  let (message, context) = match error {
    RailError::Message { message, context, .. } => (message.clone(), context.clone()),
    _ => (error.to_string(), None),
  };

  let json_error = JsonError {
    error: true,
    code: error.exit_code().as_i32(),
    message,
    context,
    help: error.help_message(),
  };

  // JSON errors go to stdout for consistent machine parsing
  // (stderr may have other output mixed in)
  if let Ok(json) = serde_json::to_string_pretty(&json_error) {
    println!("{}", json);
  } else {
    // Fallback to text if JSON serialization fails
    crate::error!("{}", error);
  }
}