refactor: move firmware workspace under firmware

This commit is contained in:
Onur Özkan committed 2026-08-10 16:58:55 +03:00
1 parent 153b0bf262
commit f03fc119a4
222 files changed
+56 -13

No files matched your search

@@ -0,0 +1,20 @@
[package]
name = "kaptan-log-derive"
edition.workspace = true
license.workspace = true
rust-version.workspace = true
version.workspace = true
[lib]
proc-macro = true
[lints]
workspace = true
[dependencies]
proc-macro2 = "1"
quote = "1"
syn = { version = "2", features = ["full"] }
[dev-dependencies]
trybuild = "1"
@@ -0,0 +1,94 @@
//! Derive support for static Kaptan runtime-log messages.
use proc_macro::TokenStream;
use quote::quote;
use syn::{Data, DeriveInput, Error, Fields, parse_macro_input, parse_quote};
/// Implements [`kaptan_log::ErrorReport`], [`core::fmt::Display`], and
/// [`core::error::Error`] for enums.
///
/// Each enum variant receives one static message in the form
/// `module::Type::Variant`. The derive deliberately accepts no attributes:
/// text logging has no user-provided numeric catalog allocation.
#[proc_macro_derive(KaptanError)]
pub fn derive_kaptan_error(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
match expand(input) {
Ok(tokens) => tokens.into(),
Err(error) => error.into_compile_error().into(),
}
}
fn expand(input: DeriveInput) -> Result<proc_macro2::TokenStream, Error> {
let DeriveInput {
ident,
generics,
data,
..
} = input;
let Data::Enum(data) = data else {
let kind = match data {
Data::Struct(_) => "structs",
Data::Union(_) => "unions",
Data::Enum(_) => unreachable!("enum handled above"),
};
return Err(Error::new_spanned(
ident,
format!("KaptanError can only be derived for enums, not {kind}"),
));
};
let arms = data.variants.iter().map(|variant| {
let variant_ident = &variant.ident;
let pattern = match &variant.fields {
Fields::Unit => quote!(Self::#variant_ident),
Fields::Unnamed(_) => quote!(Self::#variant_ident(..)),
Fields::Named(_) => quote!(Self::#variant_ident { .. }),
};
quote! {
#pattern => {
static ERROR: ::kaptan_log::StaticError =
::kaptan_log::StaticError::new(concat!(
module_path!(),
"::",
stringify!(#ident),
"::",
stringify!(#variant_ident),
));
&ERROR
}
}
});
let (impl_generics, type_generics, where_clause) = generics.split_for_impl();
let mut error_generics = generics.clone();
error_generics
.make_where_clause()
.predicates
.push(parse_quote!(#ident #type_generics: ::core::fmt::Debug));
let (error_impl_generics, error_type_generics, error_where_clause) =
error_generics.split_for_impl();
Ok(quote! {
impl #impl_generics ::kaptan_log::ErrorReport for #ident #type_generics #where_clause {
fn static_error(&self) -> &'static ::kaptan_log::StaticError {
match self {
#(#arms),*
}
}
}
impl #impl_generics ::core::fmt::Display for #ident #type_generics #where_clause {
fn fmt(
&self,
formatter: &mut ::core::fmt::Formatter<'_>,
) -> ::core::fmt::Result {
formatter.write_str(
<Self as ::kaptan_log::ErrorReport>::static_error(self).path(),
)
}
}
impl #error_impl_generics ::core::error::Error
for #ident #error_type_generics #error_where_clause {}
})
}
@@ -0,0 +1,7 @@
#[test]
fn kaptan_error_expands_for_enum_shapes_only() {
let tests = trybuild::TestCases::new();
tests.pass("tests/ui/enum.rs");
tests.compile_fail("tests/ui/struct.rs");
tests.compile_fail("tests/ui/union.rs");
}
@@ -0,0 +1,62 @@
extern crate self as kaptan_log;
use kaptan_log_derive::KaptanError;
pub struct StaticError {
path: &'static str,
additional_information: &'static str,
}
impl StaticError {
pub const fn new(text: &'static str) -> Self {
Self {
path: text,
additional_information: "",
}
}
pub const fn path(&self) -> &'static str {
self.path
}
pub const fn additional_information(&self) -> &'static str {
self.additional_information
}
}
pub trait ErrorReport {
fn static_error(&self) -> &'static StaticError;
}
#[derive(Debug, KaptanError)]
enum Generic<'a, T>
where
T: Copy,
{
Unit,
Tuple(T, u8),
Named { label: &'a str },
}
fn main() {
fn assert_error<T: core::error::Error>() {}
assert_error::<Generic<'_, u8>>();
assert_eq!(
Generic::<u8>::Unit.static_error().path(),
concat!(module_path!(), "::Generic::Unit"),
);
assert_eq!(
Generic::Tuple(1_u8, 2).static_error().path(),
concat!(module_path!(), "::Generic::Tuple"),
);
let named: Generic<'_, u8> = Generic::Named { label: "test" };
assert_eq!(
named.to_string(),
concat!(module_path!(), "::Generic::Named"),
);
assert_eq!(
named.static_error().additional_information(),
"",
"derives emit identity only; callers own optional context",
);
}
@@ -0,0 +1,8 @@
use kaptan_log_derive::KaptanError;
#[derive(KaptanError)]
struct NotAnError {
code: u8,
}
fn main() {}
@@ -0,0 +1,5 @@
error: KaptanError can only be derived for enums, not structs
--> tests/ui/struct.rs:4:8
|
4 | struct NotAnError {
| ^^^^^^^^^^
@@ -0,0 +1,8 @@
use kaptan_log_derive::KaptanError;
#[derive(KaptanError)]
union NotAnError {
code: u8,
}
fn main() {}
@@ -0,0 +1,5 @@
error: KaptanError can only be derived for enums, not unions
--> tests/ui/union.rs:4:7
|
4 | union NotAnError {
| ^^^^^^^^^^