chore (backend-rs, macro-rs): generate document from error messages

This commit is contained in:
naskya 2024-07-17 08:26:46 +09:00
parent c7505fc09a
commit a8a899a61e
No known key found for this signature in database
GPG key ID: 712D413B3A9FED5C
20 changed files with 69 additions and 17 deletions

View file

@ -14,7 +14,7 @@ pub enum Category {
Test,
}
#[derive(thiserror::Error, Debug)]
#[macros::errors]
pub enum Error {
#[error("failed to execute Redis command")]
Redis(#[from] RedisError),

View file

@ -82,7 +82,7 @@ async fn init_conn_pool() -> Result<(), RedisError> {
Ok(())
}
#[derive(thiserror::Error, Debug)]
#[macros::errors]
pub enum RedisConnError {
#[error("failed to initialize Redis connection pool")]
Redis(RedisError),

View file

@ -7,7 +7,7 @@ use crate::{database::db_conn, model::entity::user};
use sea_orm::prelude::*;
use std::sync::Mutex;
#[derive(thiserror::Error, Debug)]
#[macros::errors]
pub enum Error {
#[error(transparent)]
#[doc = "database error"]

View file

@ -7,7 +7,7 @@ use isahc::AsyncReadResponseExt;
use serde::Deserialize;
/// Errors that can occur while fetching NodeInfo from a remote server
#[derive(thiserror::Error, Debug)]
#[macros::errors]
pub enum Error {
#[error("failed to acquire an HTTP client")]
HttpClient(#[from] http_client::Error),

View file

@ -153,7 +153,7 @@ pub async fn nodeinfo_2_0() -> Result<Nodeinfo20, DbErr> {
}
#[cfg(any(test, doctest, feature = "napi"))]
#[derive(thiserror::Error, Debug)]
#[macros::errors]
pub enum Error {
#[doc = "database error"]
#[error(transparent)]

View file

@ -2,7 +2,7 @@
// We may want to (re)implement these functions in the `federation` module
// in a Rusty way (e.g., traits of actor type) if needed.
#[derive(thiserror::Error, Debug)]
#[macros::errors]
pub enum Error {
#[doc = "UTS #46 process has failed"]
#[error(transparent)]

View file

@ -5,7 +5,7 @@ use nom_exif::{parse_jpeg_exif, EntryValue, ExifTag};
use std::io::Cursor;
use tokio::sync::Mutex;
#[derive(thiserror::Error, Debug)]
#[macros::errors]
pub enum Error {
#[error("Redis cache operation has failed")]
Cache(#[from] cache::Error),

View file

@ -4,7 +4,7 @@ use crate::{database::cache, util::http_client};
use isahc::AsyncReadResponseExt;
use serde::Deserialize;
#[derive(thiserror::Error, Debug)]
#[macros::errors]
pub enum Error {
#[error("Redis cache operation has failed")]
Cache(#[from] cache::Error),

View file

@ -15,7 +15,7 @@ pub fn hash_password(password: &str) -> Result<String, password_hash::errors::Er
.to_string())
}
#[derive(thiserror::Error, Debug)]
#[macros::errors]
pub enum Error {
#[error("failed to verify password against bcrypt hash")]
Bcrypt(#[from] bcrypt::BcryptError),

View file

@ -53,7 +53,7 @@ pub fn count_reactions(reactions: &HashMap<String, u32>) -> HashMap<String, u32>
res
}
#[derive(thiserror::Error, Debug)]
#[macros::errors]
pub enum Error {
#[doc = "UTS #46 process has failed"]
#[error(transparent)]

View file

@ -6,7 +6,7 @@ use crate::{
};
use sea_orm::{DbErr, EntityTrait, QuerySelect, SelectColumns};
#[derive(thiserror::Error, Debug)]
#[macros::errors]
pub enum Error {
#[doc = "database error"]
#[error(transparent)]

View file

@ -6,7 +6,7 @@ use crate::{
};
use sea_orm::{prelude::*, QuerySelect};
#[derive(thiserror::Error, Debug)]
#[macros::errors]
pub enum AntennaCheckError {
#[doc = "database error"]
#[error(transparent)]

View file

@ -13,7 +13,7 @@ use crate::{
use redis::{streams::StreamMaxlen, AsyncCommands, RedisError};
use sea_orm::prelude::*;
#[derive(thiserror::Error, Debug)]
#[macros::errors]
pub enum Error {
#[doc = "database error"]
#[error(transparent)]

View file

@ -13,7 +13,7 @@ use sea_orm::prelude::*;
use serde::Deserialize;
use web_push::*;
#[derive(thiserror::Error, Debug)]
#[macros::errors]
pub enum Error {
#[doc = "database error"]
#[error(transparent)]

View file

@ -62,7 +62,7 @@ pub enum ChatEvent {
Typing,
}
#[derive(thiserror::Error, Debug)]
#[macros::errors]
pub enum Error {
#[error("failed to execute a Redis command")]
Redis(#[from] RedisError),

View file

@ -34,7 +34,7 @@ mod unit_test {
#[error("unexpected string '{0}'")]
struct InnerError2(String);
#[derive(thiserror::Error, Debug)]
#[macros::errors]
enum ErrorVariants {
#[error("error 1 occured")]
Error1(#[from] InnerError1),

View file

@ -5,7 +5,7 @@ use isahc::{config::*, HttpClient};
use once_cell::sync::OnceCell;
use std::time::Duration;
#[derive(thiserror::Error, Debug)]
#[macros::errors]
pub enum Error {
#[error("HTTP request failed")]
Isahc(#[from] isahc::Error),

View file

@ -0,0 +1,41 @@
use proc_macro2::TokenStream;
use quote::quote;
pub fn error_variants(_attr: TokenStream, item: TokenStream) -> TokenStream {
match error_variants_impl(item) {
Ok(tokens) => tokens,
Err(error) => error.to_compile_error(),
}
}
fn error_variants_impl(item: TokenStream) -> syn::Result<TokenStream> {
let items: syn::ItemEnum = syn::parse2(item.clone())?;
let mut new_item = items.clone();
new_item.variants = items
.variants
.into_iter()
.map(|mut variant| {
// check if doc attribute is alredy there
if variant.attrs.iter().any(|attr| attr.path().is_ident("doc")) {
return variant;
}
let msg = variant.attrs.iter().find_map(|attr| {
if !attr.path().is_ident("error") {
return None;
}
let lit: syn::LitStr = attr.parse_args().ok()?;
Some(lit.value())
});
if let Some(msg) = msg {
variant.attrs.push(syn::parse_quote! { #[doc = #msg] });
}
variant
})
.collect();
Ok(quote! { #new_item })
}

View file

@ -1,4 +1,5 @@
#![allow(clippy::items_after_test_module)]
pub mod error;
pub mod napi;
mod util;

View file

@ -75,10 +75,20 @@ define_wrapper_proc_macro_attributes! {
#[cfg(any(test, doctest))]
#item
}
/// When applied to error variant enums, this macro generates a document
/// based on error messages unless there is a doc comment
errors(attr, item) {
#[derive(::thiserror::Error, ::std::fmt::Debug)]
#[macros::error_variants(#attr, #item)]
#item
}
}
reexport_proc_macro_attributes! {
/// Creates an extra wrapper function for [napi_derive](https://docs.rs/napi-derive/latest/napi_derive/).
/// See [macros_impl::napi::napi] for details.
macros_impl::napi::napi as napi
macros_impl::error::error_variants as error_variants
}