#![allow(
dead_code,
clippy::missing_docs_in_private_items,
clippy::print_stderr,
reason = "Example"
)]
use ::neuer_error::{NeuErr, Result, traits::*};
#[derive(Debug)]
struct ToResponse(NeuErr);
impl IntoResponse for ToResponse {
fn into_response(self) -> (StatusCode, String) {
let status = self.0.attachment::<StatusCode>().copied().unwrap_or_default();
let message = format!("{}", self.0); (status, message)
}
}
impl<M> From<NeuErr<M>> for ToResponse {
fn from(err: NeuErr<M>) -> Self {
Self(err.remove_marker())
}
}
fn handle_request(user: &str) -> Result<(), ToResponse> {
match user {
"" => {
return Err(NeuErr::new("User must not be empty")
.attach(StatusCode::BadRequest)
.into());
}
"alice" => manipulate().context("Failed manipulating")?,
not_found => {
return Err(NeuErr::new(format!("User `{not_found}` was not found"))
.attach(StatusCode::NotFound)
.into());
}
}
Ok(())
}
fn manipulate() -> Result<()> {
unimplemented!()
}
fn main() {
let request = "bob";
let (status, message) = handle_request(request).into_response();
eprintln!("{status:?}: {message}");
}
trait IntoResponse {
fn into_response(self) -> (StatusCode, String);
}
impl IntoResponse for () {
fn into_response(self) -> (StatusCode, String) {
(StatusCode::Ok, String::new())
}
}
impl<T, E> IntoResponse for Result<T, E>
where
T: IntoResponse,
E: IntoResponse,
{
fn into_response(self) -> (StatusCode, String) {
match self {
Ok(v) => v.into_response(),
Err(e) => e.into_response(),
}
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, Default)]
enum StatusCode {
#[default]
InternalError,
BadRequest,
NotFound,
Ok,
}