I am working on simplifying the flow of specifying low level return codes in Rust by using procedural macros.
This is some of the library code
/// Simple [u16] based result code type which also allows to group related resultcodes.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ResultU16 {
group_id: u8,
unique_id: u8,
}
use serde::{Deserialize, Serialize};
use serde_hex::{SerHex, StrictCapPfx};
#[derive(Debug, Copy, Clone, Serialize)]
pub struct ResultU16Info {
pub name: &'static str,
pub result: &'static ResultU16,
pub group_str: &'static str,
pub info: &'static str,
}
Right now, the code, as written by the user, looks something like this:
#[resultcode(info = "This is a test result where the first parameter is foo")]
const TEST_RESULT: ResultU16 = ResultU16::new(0, 1);
// Create named reference of auto-generated struct, which can be used by IDEs etc.
const TEST_RESULT_EXT_REF: &ResultU16Info = &TEST_RESULT_EXT;
I basically implemented a basic form of introspecition which later allows to generate something like CSVs with all these returncodes. The *_EXT
structure is generated automatically by the resultcode
attribute macro. Not sure if that is the cleanest/most rusty approach here, but this has worked for me.
When using and scaling this mechanism, I usually keep a static slice of ResultU16Info
s
for easier usage in my CSV generators, for example something like this
pub const HK_ERR_RESULTS: &[ResultU16Info] = &[
TARGET_ID_MISSING_EXT,
UNKNOWN_TARGET_ID_EXT,
UNKNOWN_TARGET_ID_EXT,
COLLECTION_INTERVAL_MISSING_EXT,
];
I now always have to update this list as well. It might seem a minor inconvenience, but it's quite annoying and it is also easy to forget updating these lists when adding new returncodes.
What would be some possible ways to automatically add auto-generated structures to some list in some shape or form?
Kind Regards
RM