util-macros (0.0.1)

Published 2026-02-02 22:47:52 +00:00 by irith

Installation

[registries.forgejo]
index = "sparse+" # Sparse index
# index = "" # Git

[net]
git-fetch-with-cli = true
cargo add util-macros@0.0.1 --registry forgejo

About this package

util-macros

Some helper Rust macros.

AutoNew usage

#[derive(AutoNew)]
pub struct Alpha {
    foo: usize,
    bar: String,
}

gives you:

pub struct Alpha {
    foo: usize,
    bar: String,
}

impl Alpha {
    pub fn new(foo: usize, bar: String) -> Self {
        Self {foo, bar}
    }
}

AutoIntoResponse + auto_into_response() usage

#[derive(AutoIntoResponse)]
#[auto_into_response(StatusCode::UNPROCESSABLE_ENTITY, true)]
pub struct Alpha(String);

pub enum Bravo {
    #[auto_into_response(StatusCode::INTERNAL_SERVER_ERROR, false)]
    Foo(String),

    #[auto_into_response(StatusCode::CONFLICT, true)]
    Bar(String),
}

gives you:

pub struct Alpha(String);

impl IntoResponse for Alpha {
    fn into_response(self) -> Response {
        let mut error = self.to_string();
        error!("{}", error);
        let status_code = {
            let status_code = StatusCode::UNPROCESSABLE_ENTITY;
            error = status_code.canonical_reason()
                               .expect("StatusCode had no canonical reason?")
                               .to_string();
            status_code
        };
        let response = HTTPErrorResponse::new(error);
        (status_code, Json(response)).into_response()
    }
}

pub enum Bravo {
    Foo(String),
    Bar(String, String),
}

impl IntoResponse for Bravo {
    fn into_response(self) -> Response {
        let mut error = self.to_string();
        error!("{}", error);
        let status_code = match self {
            Self::Foo(_) => StatusCode::INTERNAL_SERVER_ERROR,
            Self::Bar(_, _) => {
                let status_code = StatusCode::CONFLICT;
                error = status_code.canonical_reason()
                                   .expect("StatusCode had no canonical reason?")
                                   .to_string();
                status_code
            },
        };
        let response = HTTPErrorResponse::new(error);
        (status_code, Json(response)).into_response()
    }
}

where the following is relied on in the client code:

  • http::StatusCode
  • tracing::error
  • axum::response::IntoResponse
  • axum::response::Response
  • axum::extract::Json
  • HTTPErrorResponse

AutoDeserialize + auto_deserialize() usage

pub struct Alpha {
    #[auto_deserialize(String)]
    foo: Foo,

    #[auto_deserialize(String)]
    bar: Bar,
}

gives you:

pub struct Alpha {
    foo: Foo,
    bar: Bar,
}

#[derive(Debug, Deserialize)]
pub struct RawAlpha {
    foo: String,
    bar: String,
}

impl TryFrom<RawAlpha> for Alpha {
    type Error = ErrorResponse;
    fn try_from(raw: RawAlpha) -> Result<Self, Self::Error> {
        let foo = Foo::new(&raw.foo)?;
        let bar = Bar::new(&raw.bar)?;
        Ok(Self::new(foo, bar))
    }
}

where the following is relied on in the client code:

  • serde::Deserialize
  • axum::response::ErrorResponse

Dependencies

ID Version
proc-macro-error ^1.0
proc-macro2 ^1.0
quote ^1.0
syn ^2.0
Details
Cargo
2026-02-02 22:47:52 +00:00
0
6.2 KiB
Assets (1)
Versions (4) View all
0.1.0 2026-02-03
0.0.3 2026-02-03
0.0.2 2026-02-03
0.0.1 2026-02-02