Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#491] Propagate error strings from iceoryx2 via CXX API #506

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 93 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,95 @@ 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(|| {
let variant_str = variant_ident.to_string();
variant_str
.chars()
.enumerate()
.map(|(_, c)| {
if c == '_' {
' '
} else {
c.to_ascii_lowercase()
}
})
.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/cxx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ target_include_directories(includes-only-cxx

add_library(iceoryx2-cxx-object-lib OBJECT
src/config.cpp
src/error_string.cpp
src/event_id.cpp
src/file_descriptor.cpp
src/header_publish_subscribe.cpp
Expand Down
3 changes: 3 additions & 0 deletions iceoryx2-ffi/cxx/include/iox2/config_creation_error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ enum class ConfigCreationError : uint8_t {
/// Parts of the config file could not be deserialized. Indicates some kind of syntax error.
UnableToDeserializeContents
};

auto error_string(const iox2::ConfigCreationError& error) -> const char*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use here the From approach like it is done here: https://github.com/eclipse-iceoryx/iceoryx2/blob/main/iceoryx2-ffi/cxx/include/iox2/enum_translation.hpp

Since it is a conversion from enum iox2::ConfigCreationError to const char*.

This applies to all error_string() free functions.


} // namespace iox2

#endif
4 changes: 4 additions & 0 deletions iceoryx2-ffi/cxx/include/iox2/connection_failure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ enum class ConnectionFailure : uint8_t {
/// Failures when mapping the corresponding data segment
UnableToMapPublishersDataSegment
};


auto error_string(const iox2::ConnectionFailure& error) -> const char*;

} // namespace iox2

#endif
Loading
Loading