roneocratesroneo-coresrcerror.rs
0518da6beb3fd562b82c5b5c2798b72e0d1591b2- use std::fmt;
- use std::path::PathBuf;
- pub type Result<T> = std::result::Result<T, RoneoError>;
- #[derive(Debug)]
- #[non_exhaustive]
- pub enum RoneoError {
- Io {
- path: Option<PathBuf>,
- source: std::io::Error,
- },
- Yaml(serde_yaml_ng::Error),
- Json(serde_json::Error),
- TemplateRender {
- context: String,
- source: minijinja::Error,
- },
- InvalidConfig(String),
- IncompatibleCopierVersion {
- required: String,
- supported: String,
- },
- DowngradeNotSupported {
- from: String,
- to: String,
- },
- InvalidAnswer {
- question: String,
- message: String,
- },
- TemplateSource(String),
- Vcs(String),
- DirtyDestination(PathBuf),
- NotAGitRepo(PathBuf),
- MissingAnswersFile(PathBuf),
- UnsafeTemplate(String),
- TaskFailed {
- command: String,
- status: Option<i32>,
- stderr: String,
- },
- Conflict(String),
- Plugin(String),
- UserAborted,
- }
- impl RoneoError {
- pub fn io(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
- Self::Io {
- path: Some(path.into()),
- source,
- }
- }
- }
- impl fmt::Display for RoneoError {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- match self {
- Self::Io {
- path: Some(p),
- source,
- } => write!(f, "I/O error at {}: {source}", p.display()),
- Self::Io { path: None, source } => write!(f, "I/O error: {source}"),
- Self::Yaml(e) => write!(f, "YAML error: {e}"),
- Self::Json(e) => write!(f, "JSON error: {e}"),
- Self::TemplateRender { context, source } => {
- write!(f, "Template rendering error ({context}): {source}")
- }
- Self::InvalidConfig(msg) => write!(f, "Invalid template configuration: {msg}"),
- Self::IncompatibleCopierVersion {
- required,
- supported,
- } => write!(
- f,
- "Template requires Copier >= {required}, but Roneo targets Copier {supported} compatibility"
- ),
- Self::DowngradeNotSupported { from, to } => write!(
- f,
- "Cannot downgrade template from '{from}' to '{to}' during `update` (downgrades are not supported; use `recopy` instead)"
- ),
- Self::InvalidAnswer { question, message } => {
- write!(f, "Invalid answer for '{question}': {message}")
- }
- Self::TemplateSource(msg) => write!(f, "Template source error: {msg}"),
- Self::Vcs(msg) => write!(f, "Git error: {msg}"),
- Self::DirtyDestination(p) => write!(
- f,
- "Destination repository '{}' has uncommitted changes. Commit or stash them before running `update`.",
- p.display()
- ),
- Self::NotAGitRepo(p) => write!(
- f,
- "Destination '{}' is not a git repository. Smart `update` requires git; use `recopy` instead.",
- p.display()
- ),
- Self::MissingAnswersFile(p) => write!(
- f,
- "No answers file found in '{}'. Cannot update or recopy without an existing answers file.",
- p.display()
- ),
- Self::UnsafeTemplate(msg) => write!(
- f,
- "Unsafe template: {msg}. Re-run with `--trust` if you trust this template."
- ),
- Self::TaskFailed {
- command,
- status: Some(code),
- stderr,
- } => write!(
- f,
- "Task failed (`{command}`) with exit code {code}: {stderr}"
- ),
- Self::TaskFailed {
- command,
- status: None,
- stderr,
- } => write!(f, "Task failed (`{command}`): {stderr}"),
- Self::Conflict(msg) => write!(f, "Merge conflict while updating: {msg}"),
- Self::Plugin(msg) => write!(f, "Plugin error: {msg}"),
- Self::UserAborted => f.write_str("Operation aborted by user"),
- }
- }
- }
- impl std::error::Error for RoneoError {
- fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
- match self {
- Self::Io { source, .. } => Some(source),
- Self::Yaml(e) => Some(e),
- Self::Json(e) => Some(e),
- Self::TemplateRender { source, .. } => Some(source),
- _ => None,
- }
- }
- }
- impl From<std::io::Error> for RoneoError {
- fn from(source: std::io::Error) -> Self {
- Self::Io { path: None, source }
- }
- }
- impl From<serde_yaml_ng::Error> for RoneoError {
- fn from(e: serde_yaml_ng::Error) -> Self {
- Self::Yaml(e)
- }
- }
- impl From<serde_json::Error> for RoneoError {
- fn from(e: serde_json::Error) -> Self {
- Self::Json(e)
- }
- }