feat(status): add status type
This commit was merged in pull request #69.
This commit is contained in:
14
src/status/BUILD
Normal file
14
src/status/BUILD
Normal file
@@ -0,0 +1,14 @@
|
||||
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
|
||||
|
||||
rust_library(
|
||||
name = "status",
|
||||
crate_name = "status",
|
||||
srcs = ["status.rs"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
rust_test(
|
||||
name = "status_test",
|
||||
srcs = ["status_test.rs"],
|
||||
deps = [":status"],
|
||||
)
|
||||
9
src/status/Cargo.toml
Normal file
9
src/status/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "status"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[lib]
|
||||
path = "status.rs"
|
||||
|
||||
[dependencies]
|
||||
84
src/status/status.rs
Normal file
84
src/status/status.rs
Normal 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)
|
||||
}
|
||||
}
|
||||
67
src/status/status_test.rs
Normal file
67
src/status/status_test.rs
Normal file
@@ -0,0 +1,67 @@
|
||||
use std::error::Error;
|
||||
|
||||
use status::{Code, Status};
|
||||
|
||||
#[test]
|
||||
fn constructors_set_code_and_message() {
|
||||
let cases = [
|
||||
(Status::invalid_argument("bad input"), Code::InvalidArgument),
|
||||
(Status::not_found("missing input"), Code::NotFound),
|
||||
(Status::permission_denied("denied"), Code::PermissionDenied),
|
||||
(Status::internal("failed"), Code::Internal),
|
||||
];
|
||||
|
||||
for (status, code) in cases {
|
||||
assert_eq!(code, status.code());
|
||||
assert!(!status.message().is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn display_includes_code_and_message() {
|
||||
let status = Status::not_found("missing input");
|
||||
|
||||
assert_eq!("NotFound: missing input", status.to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn with_source_preserves_source_error() {
|
||||
let status = Status::internal("read failed").with_source(std::io::Error::new(
|
||||
std::io::ErrorKind::PermissionDenied,
|
||||
"denied",
|
||||
));
|
||||
|
||||
assert_eq!("denied", status.source().unwrap().to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn io_error_maps_not_found() {
|
||||
let status = Status::from(std::io::Error::new(
|
||||
std::io::ErrorKind::NotFound,
|
||||
"missing",
|
||||
));
|
||||
|
||||
assert_eq!(Code::NotFound, status.code());
|
||||
assert_eq!("missing", status.message());
|
||||
assert_eq!("missing", status.source().unwrap().to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn io_error_maps_permission_denied() {
|
||||
let status = Status::from(std::io::Error::new(
|
||||
std::io::ErrorKind::PermissionDenied,
|
||||
"denied",
|
||||
));
|
||||
|
||||
assert_eq!(Code::PermissionDenied, status.code());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn io_error_maps_other_errors_to_internal() {
|
||||
let status = Status::from(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidData,
|
||||
"invalid data",
|
||||
));
|
||||
|
||||
assert_eq!(Code::Internal, status.code());
|
||||
}
|
||||
@@ -4,6 +4,7 @@ rust_library(
|
||||
name = "xatu_lib",
|
||||
crate_name = "xatu",
|
||||
srcs = ["xatu.rs"],
|
||||
deps = ["//src/status:status"],
|
||||
)
|
||||
|
||||
rust_binary(
|
||||
@@ -13,6 +14,7 @@ rust_binary(
|
||||
deps = [
|
||||
":xatu_lib",
|
||||
"//src/monorepo:monorepo",
|
||||
"//src/status:status",
|
||||
"//third_party/crates:clap",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -11,6 +11,6 @@ name = "xatu"
|
||||
path = "xatu_main.rs"
|
||||
|
||||
[dependencies]
|
||||
anyhow = { workspace = true }
|
||||
clap = { workspace = true, features = ["derive"] }
|
||||
monorepo = { workspace = true }
|
||||
status = { workspace = true }
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub fn run(files: Vec<SourceFile>) -> Result<()> {
|
||||
pub fn run(files: Vec<SourceFile>) -> status::Result<()> {
|
||||
for file in files {
|
||||
println!("{}", file.path.display());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub type Result<T> = std::result::Result<T, std::io::Error>;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SourceFile {
|
||||
pub path: PathBuf,
|
||||
|
||||
@@ -25,7 +25,7 @@ fn is_excluded_dir(path: &Path) -> bool {
|
||||
})
|
||||
}
|
||||
|
||||
fn collect_source_files(path: &Path) -> xatu::Result<Vec<SourceFile>> {
|
||||
fn collect_source_files(path: &Path) -> status::Result<Vec<SourceFile>> {
|
||||
let metadata = std::fs::symlink_metadata(path)?;
|
||||
|
||||
if metadata.file_type().is_dir() {
|
||||
@@ -59,7 +59,7 @@ struct Args {
|
||||
paths: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
fn main() -> xatu::Result<()> {
|
||||
fn main() -> status::Result<()> {
|
||||
let args = Args::parse();
|
||||
|
||||
let paths: Vec<PathBuf> = args
|
||||
@@ -71,7 +71,7 @@ fn main() -> xatu::Result<()> {
|
||||
let files: Vec<SourceFile> = paths
|
||||
.iter()
|
||||
.map(|path| collect_source_files(path))
|
||||
.collect::<xatu::Result<Vec<_>>>()?
|
||||
.collect::<status::Result<Vec<_>>>()?
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.collect();
|
||||
|
||||
Reference in New Issue
Block a user