feat(status): add status type

This commit was merged in pull request #69.
This commit is contained in:
2026-09-17 14:42:32 -04:00
parent 37539682d4
commit 49cf043035
10 changed files with 188 additions and 14 deletions

84
src/status/status.rs Normal file
View File

@@ -0,0 +1,84 @@
pub type Result<T> = std::result::Result<T, Status>;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Code {
InvalidArgument,
NotFound,
PermissionDenied,
Internal,
}
#[derive(Debug)]
pub struct Status {
code: Code,
message: String,
source: Option<Box<dyn std::error::Error + Send + Sync + 'static>>,
}
impl Status {
pub fn new(code: Code, message: impl Into<String>) -> Self {
Self {
code,
message: message.into(),
source: None,
}
}
pub fn not_found(message: impl Into<String>) -> Self {
Self::new(Code::NotFound, message)
}
pub fn invalid_argument(message: impl Into<String>) -> Self {
Self::new(Code::InvalidArgument, message)
}
pub fn permission_denied(message: impl Into<String>) -> Self {
Self::new(Code::PermissionDenied, message)
}
pub fn internal(message: impl Into<String>) -> Self {
Self::new(Code::Internal, message)
}
pub fn with_source<E>(mut self, source: E) -> Self
where
E: std::error::Error + Send + Sync + 'static,
{
self.source = Some(Box::new(source));
self
}
pub fn code(&self) -> Code {
self.code
}
pub fn message(&self) -> &str {
&self.message
}
}
impl std::fmt::Display for Status {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(formatter, "{:?}: {}", self.code, self.message)
}
}
impl std::error::Error for Status {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.source
.as_deref()
.map(|source| source as &(dyn std::error::Error + 'static))
}
}
impl From<std::io::Error> for Status {
fn from(error: std::io::Error) -> Self {
let code = match error.kind() {
std::io::ErrorKind::NotFound => Code::NotFound,
std::io::ErrorKind::PermissionDenied => Code::PermissionDenied,
_ => Code::Internal,
};
Self::new(code, error.to_string()).with_source(error)
}
}