forgeFRENBrowse the repositories

roneocratesroneo-coresrcerror.rs

0518da6beb3fd562b82c5b5c2798b72e0d1591b2

At commit 0518da6

154 lines · 4.8 KiBRustRaw
  1. use std::fmt;
  2. use std::path::PathBuf;
  3. pub type Result<T> = std::result::Result<T, RoneoError>;
  4. #[derive(Debug)]
  5. #[non_exhaustive]
  6. pub enum RoneoError {
  7. Io {
  8. path: Option<PathBuf>,
  9. source: std::io::Error,
  10. },
  11. Yaml(serde_yaml_ng::Error),
  12. Json(serde_json::Error),
  13. TemplateRender {
  14. context: String,
  15. source: minijinja::Error,
  16. },
  17. InvalidConfig(String),
  18. IncompatibleCopierVersion {
  19. required: String,
  20. supported: String,
  21. },
  22. DowngradeNotSupported {
  23. from: String,
  24. to: String,
  25. },
  26. InvalidAnswer {
  27. question: String,
  28. message: String,
  29. },
  30. TemplateSource(String),
  31. Vcs(String),
  32. DirtyDestination(PathBuf),
  33. NotAGitRepo(PathBuf),
  34. MissingAnswersFile(PathBuf),
  35. UnsafeTemplate(String),
  36. TaskFailed {
  37. command: String,
  38. status: Option<i32>,
  39. stderr: String,
  40. },
  41. Conflict(String),
  42. Plugin(String),
  43. UserAborted,
  44. }
  45. impl RoneoError {
  46. pub fn io(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
  47. Self::Io {
  48. path: Some(path.into()),
  49. source,
  50. }
  51. }
  52. }
  53. impl fmt::Display for RoneoError {
  54. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  55. match self {
  56. Self::Io {
  57. path: Some(p),
  58. source,
  59. } => write!(f, "I/O error at {}: {source}", p.display()),
  60. Self::Io { path: None, source } => write!(f, "I/O error: {source}"),
  61. Self::Yaml(e) => write!(f, "YAML error: {e}"),
  62. Self::Json(e) => write!(f, "JSON error: {e}"),
  63. Self::TemplateRender { context, source } => {
  64. write!(f, "Template rendering error ({context}): {source}")
  65. }
  66. Self::InvalidConfig(msg) => write!(f, "Invalid template configuration: {msg}"),
  67. Self::IncompatibleCopierVersion {
  68. required,
  69. supported,
  70. } => write!(
  71. f,
  72. "Template requires Copier >= {required}, but Roneo targets Copier {supported} compatibility"
  73. ),
  74. Self::DowngradeNotSupported { from, to } => write!(
  75. f,
  76. "Cannot downgrade template from '{from}' to '{to}' during `update` (downgrades are not supported; use `recopy` instead)"
  77. ),
  78. Self::InvalidAnswer { question, message } => {
  79. write!(f, "Invalid answer for '{question}': {message}")
  80. }
  81. Self::TemplateSource(msg) => write!(f, "Template source error: {msg}"),
  82. Self::Vcs(msg) => write!(f, "Git error: {msg}"),
  83. Self::DirtyDestination(p) => write!(
  84. f,
  85. "Destination repository '{}' has uncommitted changes. Commit or stash them before running `update`.",
  86. p.display()
  87. ),
  88. Self::NotAGitRepo(p) => write!(
  89. f,
  90. "Destination '{}' is not a git repository. Smart `update` requires git; use `recopy` instead.",
  91. p.display()
  92. ),
  93. Self::MissingAnswersFile(p) => write!(
  94. f,
  95. "No answers file found in '{}'. Cannot update or recopy without an existing answers file.",
  96. p.display()
  97. ),
  98. Self::UnsafeTemplate(msg) => write!(
  99. f,
  100. "Unsafe template: {msg}. Re-run with `--trust` if you trust this template."
  101. ),
  102. Self::TaskFailed {
  103. command,
  104. status: Some(code),
  105. stderr,
  106. } => write!(
  107. f,
  108. "Task failed (`{command}`) with exit code {code}: {stderr}"
  109. ),
  110. Self::TaskFailed {
  111. command,
  112. status: None,
  113. stderr,
  114. } => write!(f, "Task failed (`{command}`): {stderr}"),
  115. Self::Conflict(msg) => write!(f, "Merge conflict while updating: {msg}"),
  116. Self::Plugin(msg) => write!(f, "Plugin error: {msg}"),
  117. Self::UserAborted => f.write_str("Operation aborted by user"),
  118. }
  119. }
  120. }
  121. impl std::error::Error for RoneoError {
  122. fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
  123. match self {
  124. Self::Io { source, .. } => Some(source),
  125. Self::Yaml(e) => Some(e),
  126. Self::Json(e) => Some(e),
  127. Self::TemplateRender { source, .. } => Some(source),
  128. _ => None,
  129. }
  130. }
  131. }
  132. impl From<std::io::Error> for RoneoError {
  133. fn from(source: std::io::Error) -> Self {
  134. Self::Io { path: None, source }
  135. }
  136. }
  137. impl From<serde_yaml_ng::Error> for RoneoError {
  138. fn from(e: serde_yaml_ng::Error) -> Self {
  139. Self::Yaml(e)
  140. }
  141. }
  142. impl From<serde_json::Error> for RoneoError {
  143. fn from(e: serde_json::Error) -> Self {
  144. Self::Json(e)
  145. }
  146. }