fix(parser/unchecked/parse_structure): Accept attributes on fields

This commit is contained in:
Benedikt Peetz 2024-03-27 09:59:35 +01:00
parent fae3de8b49
commit 7a8dc66876
Signed by: bpeetz
GPG Key ID: A5E94010C3A642AD
2 changed files with 187 additions and 1 deletions

View File

@ -330,7 +330,7 @@ impl Parser {
self.expect(token![CurlyBracketOpen])?; self.expect(token![CurlyBracketOpen])?;
let mut contents = vec![]; let mut contents = vec![];
if self.expect_peek(token![Ident]) { if self.expect_peek(token![Ident]) || self.expect_peek(token![#]) {
contents.push(self.parse_doc_named_type()?); contents.push(self.parse_doc_named_type()?);
} }
while self.expect_peek(token![Comma]) { while self.expect_peek(token![Comma]) {

View File

@ -0,0 +1,186 @@
# Host files
File path: `out/dir/api.rs`
```rust
// Host code
/* Rust API */
#[derive(Debug)]
pub enum Commands {
Test(test::Test),
}
pub mod test {
/// This struct will be used later on (and this is also a struct doc comment test)
#[allow(non_camel_case_types)]
#[derive(Debug)]
pub struct Callback {
/// Very important field
pub func: fn(String) -> String,
/// Very important field for keeping time constant
pub timeout: u32,
}
impl trixy::types::traits::convert_trait::Convertible for Callback {
type Ptr = crate::test_c::Callback_c;
fn into_ptr(self) -> Self::Ptr {
Self::Ptr {
func: self.func.into(),
timeout: self.timeout.into(),
}
}
fn from_ptr(
ptr: Self::Ptr,
) -> Result<Self, trixy::types::error::TypeConversionError> {
todo!()
}
}
impl TryFrom<crate::test_c::Callback_c> for Callback {
type Error = trixy::types::error::TypeConversionError;
fn try_from(value: crate::test_c::Callback_c) -> Result<Self, Self::Error> {
Ok(Self {
func: value.func.try_into()?,
timeout: value.timeout.try_into()?,
})
}
}
/// Same thing as above (and a enum doc comment test)
#[allow(non_camel_case_types)]
#[derive(Debug)]
pub enum CallbackPriority {
/// Callback **now**
High,
/// Maybe do a callback
Medium,
/// Calling back is ..
/// really not important ..
/// like not even think about doing it
Low,
}
impl From<crate::test_c::CallbackPriority_c> for CallbackPriority {
fn from(value: crate::test_c::CallbackPriority_c) -> Self {
match value {
CallbackPriority_c::High => Self::High,
CallbackPriority_c::Medium => Self::Medium,
CallbackPriority_c::Low => Self::Low,
}
}
}
#[derive(Debug)]
pub enum Test {
#[allow(non_camel_case_types)]
execute_callback {
callback: crate::test::Callback,
priority: crate::test::CallbackPriority,
},
}
}
/* C API */
pub mod test_c {
/// Same thing as above (and a enum doc comment test)
#[allow(non_camel_case_types)]
#[repr(C)]
#[derive(Debug)]
pub enum CallbackPriority_c {
/// Callback **now**
High,
/// Maybe do a callback
Medium,
/// Calling back is ..
/// really not important ..
/// like not even think about doing it
Low,
}
impl From<crate::test::CallbackPriority> for CallbackPriority_c {
fn from(value: crate::test::CallbackPriority) -> Self {
match value {
CallbackPriority::High => Self::High,
CallbackPriority::Medium => Self::Medium,
CallbackPriority::Low => Self::Low,
}
}
}
/// This struct will be used later on (and this is also a struct doc comment test)
#[allow(non_camel_case_types)]
#[repr(C)]
#[derive(Debug)]
pub struct Callback_c {
/// Very important field
pub func: fn(String) -> String,
/// Very important field for keeping time constant
pub timeout: trixy::types::u32,
}
}
#[no_mangle]
pub extern "C" fn test_execute_callback(
callback: crate::test::Callback,
priority: crate::test::CallbackPriority,
) -> core::ffi::c_int {
callback_function(test_execute_callback);
return 1;
}
// vim: filetype=rust
```
# Auxiliary files
File path: `dist/interface.h`
```c
#if !defined TRIXY_MAIN_HEADER
#define TRIXY_MAIN_HEADER
#include "errno.h"
#include "string.h"
#include "vec.h"
/**
Same thing as above (and a enum doc comment test)
*/
enum CallbackPriority
{
/**
Callback **now**
*/
High,
/**
Maybe do a callback
*/
Medium,
/**
Calling back is ..
*/
/**
really not important ..
*/
/**
like not even think about doing it
*/
Low,
};
/**
This struct will be used later on (and this is also a struct doc comment test)
*/
Callback
{
/**
Very important field
*/ const char *(*name) (const char *name)func; /**
Very important field for keeping time constant
*/
uint32_t timeout;
};
extern int test_execute_callback (struct Callback callback,
enum CallbackPriority priority);
struct test
{
int (*execute_callback) ();
};
const struct test test = {
.execute_callback = test_execute_callback,
};
#endif // if !defined TRIXY_MAIN_HEADER
// vim: filetype=c
```