Skip to content

Commit

Permalink
WIP: Propagate errors from iceoryx2
Browse files Browse the repository at this point in the history
  • Loading branch information
orecham committed Oct 25, 2024
1 parent 473ab55 commit b303366
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 250 deletions.
95 changes: 94 additions & 1 deletion iceoryx2-bb/derive-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extern crate proc_macro;

use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, Data, DeriveInput, Fields};
use syn::{parse_macro_input, Data, DeriveInput, Expr, ExprLit, Fields, Lit};

/// Implements the [`iceoryx2_bb_elementary::placement_default::PlacementDefault`] trait when all
/// fields of the struct implement it.
Expand Down Expand Up @@ -99,3 +99,96 @@ pub fn placement_default_derive(input: TokenStream) -> TokenStream {

TokenStream::from(expanded)
}

#[proc_macro_derive(StaticStringRepresentation, attributes(StaticString))]
pub fn as_static_string_derive(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();

let static_string_impl = match input.data {
Data::Enum(ref data_enum) => {
let match_arms = data_enum.variants.iter().map(|variant| {
let variant_ident = &variant.ident;

// Get the StaticString attribute if it exists
let static_string = variant
.attrs
.iter()
.find_map(|attr| {
if !attr.path().is_ident("StaticString") {
return None;
}

match attr.meta.require_name_value() {
Ok(meta) => {
if let Expr::Lit(ExprLit {
lit: Lit::Str(lit), ..
}) = &meta.value
{
Some(lit.value())
} else {
None
}
}
_ => None,
}
})
.unwrap_or_else(|| {
// Default to converting variant name to snake_case with spaces
let variant_str = variant_ident.to_string();
variant_str
.chars()
.enumerate()
.map(|(i, c)| {
if i > 0 && c.is_uppercase() {
format!(" {}", c.to_lowercase())
} else {
c.to_lowercase().to_string()
}
})
.collect::<String>()
});

match &variant.fields {
Fields::Unit => {
quote! {
Self::#variant_ident => concat!(#static_string, "\0")
}
}
Fields::Unnamed(_) => {
quote! {
Self::#variant_ident(..) => concat!(#static_string, "\0")
}
}
Fields::Named(_) => {
quote! {
Self::#variant_ident{..} => concat!(#static_string, "\0")
}
}
}
});

quote! {
fn as_static_str(&self) -> &'static str {
match self {
#(#match_arms,)*
}
}
}
}
_ => {
let err =
syn::Error::new_spanned(&input, "AsStaticString can only be derived for enums");
return err.to_compile_error().into();
}
};

let expanded = quote! {
impl #impl_generics AsStaticString for #name #ty_generics #where_clause {
#static_string_impl
}
};

TokenStream::from(expanded)
}
15 changes: 15 additions & 0 deletions iceoryx2-bb/elementary/src/as_static_string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) 2024 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache Software License 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
// which is available at https://opensource.org/licenses/MIT.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

pub trait AsStaticString {
fn as_static_str(&self) -> &'static str;
}
5 changes: 4 additions & 1 deletion iceoryx2-bb/elementary/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
#[macro_use]
pub mod enum_gen;

/// A strong type that represents the alignment part of [`std::alloc::Layout`]
mod as_static_string;
pub use as_static_string::*;

pub mod alignment;
pub mod allocator;
/// A strong type that represents the alignment part of [`std::alloc::Layout`]
pub mod bump_allocator;
pub mod lazy_singleton;
pub mod math;
Expand Down
1 change: 1 addition & 0 deletions iceoryx2-ffi/ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ cbindgen = { workspace = true }
[dependencies]
iceoryx2 = { workspace = true }
iceoryx2-bb-container = { workspace = true }
iceoryx2-bb-derive-macros = { workspace = true }
iceoryx2-bb-elementary = { workspace = true }
iceoryx2-bb-log = { workspace = true }
iceoryx2-bb-system-types = { workspace = true }
Expand Down
144 changes: 0 additions & 144 deletions iceoryx2-ffi/ffi/src/api/error.rs

This file was deleted.

2 changes: 0 additions & 2 deletions iceoryx2-ffi/ffi/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use iceoryx2_bb_container::semantic_string::SemanticStringError;
use core::ffi::{c_int, c_void};

mod config;
mod error;
mod event_id;
mod file_descriptor;
mod iceoryx2_settings;
Expand Down Expand Up @@ -59,7 +58,6 @@ mod waitset_builder;
mod waitset_guard;

pub use config::*;
pub use error::*;
pub use event_id::*;
pub use file_descriptor::*;
pub use iceoryx2_settings::*;
Expand Down
Loading

0 comments on commit b303366

Please sign in to comment.