use thiserror::Error;
#[derive(Error, Debug)]
pub enum ReleaseError {
#[error("no release.toml found\nhint: run `jj-release init` to get started")]
MissingConfig,
#[error("{0} CLI not found on PATH\nhint: run `jj-release init` to check your setup")]
MissingTool(String),
#[error("{0}")]
Message(String),
#[error(transparent)]
Io(#[from] std::io::Error),
#[error(transparent)]
Toml(#[from] toml::de::Error),
#[error(transparent)]
SemVer(#[from] semver::Error),
#[cfg(feature = "gitea")]
#[error(transparent)]
Ureq(#[from] ureq::Error),
#[error(transparent)]
CargoMetadata(#[from] cargo_metadata::Error),
#[error(transparent)]
SerdeJson(#[from] serde_json::Error),
#[error(transparent)]
Utf8(#[from] std::string::FromUtf8Error),
}
#[derive(Debug)]
pub enum ExitState {
Silent(i32),
NoTrigger,
Error { error: ReleaseError, code: i32 },
}
impl ExitState {
#[must_use]
pub const fn silent(code: i32) -> Self {
Self::Silent(code)
}
#[must_use]
pub const fn no_trigger() -> Self {
Self::NoTrigger
}
#[must_use]
pub const fn missing_config() -> Self {
Self::Error {
error: ReleaseError::MissingConfig,
code: 2,
}
}
#[must_use]
pub fn missing_tool(name: &str) -> Self {
Self::Error {
error: ReleaseError::MissingTool(name.to_string()),
code: 3,
}
}
pub fn message(msg: impl Into<String>) -> Self {
Self::Error {
error: ReleaseError::Message(msg.into()),
code: 101,
}
}
}
#[must_use]
pub fn report(result: Result<(), ExitState>) -> i32 {
match result {
Ok(()) | Err(ExitState::NoTrigger) => 0,
Err(ExitState::Silent(code)) => code,
Err(ExitState::Error { error, code }) => {
eprintln!("error: {error}");
code
}
}
}
pub type Result<T, E = ReleaseError> = std::result::Result<T, E>;