From d93dd92fda2c714c5c65d262411899bf620fc85d Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 25 Oct 2022 23:32:12 +0200 Subject: [PATCH] completion and fixes --- fsrc-core/src/events.rs | 46 +++++++++++++++++++++++++++-------- fsrc-core/tests/pus_events.rs | 16 ++++++------ 2 files changed, 44 insertions(+), 18 deletions(-) diff --git a/fsrc-core/src/events.rs b/fsrc-core/src/events.rs index 2523a2e..12d71a4 100644 --- a/fsrc-core/src/events.rs +++ b/fsrc-core/src/events.rs @@ -225,6 +225,16 @@ macro_rules! try_from_impls { }; } +macro_rules! const_from_fn { + ($from_fn_name: ident, $TypedIdent: ident, $SevIdent: ident) => { + pub const fn $from_fn_name(event: $TypedIdent<$SevIdent>) -> Self { + Self { + base: event.event.base + } + } + } +} + #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] pub struct EventU32 { base: EventBase, @@ -280,16 +290,10 @@ impl EventU32 { } } - pub const fn const_from_info(event: EventU32TypedSev) -> Self { - Self { - base: event.event.base - } - } - pub const fn const_from_medium(event: EventU32TypedSev) -> Self { - Self { - base: event.event.base - } - } + const_from_fn!(const_from_info, EventU32TypedSev, SeverityInfo); + const_from_fn!(const_from_low, EventU32TypedSev, SeverityLow); + const_from_fn!(const_from_medium, EventU32TypedSev, SeverityMedium); + const_from_fn!(const_from_high, EventU32TypedSev, SeverityHigh); } impl EventU32TypedSev { @@ -431,6 +435,10 @@ impl EventU16 { }, } } + const_from_fn!(const_from_info, EventU16TypedSev, SeverityInfo); + const_from_fn!(const_from_low, EventU16TypedSev, SeverityLow); + const_from_fn!(const_from_medium, EventU16TypedSev, SeverityMedium); + const_from_fn!(const_from_high, EventU16TypedSev, SeverityHigh); } impl EventU16TypedSev { /// Generate a small event. The raw representation of a small event has 16 bits. @@ -532,6 +540,9 @@ mod tests { const HIGH_SEV_EVENT_SMALL: EventU16TypedSev = EventU16TypedSev::const_new(0x3F, 0xff); + /// This working is a test in itself. + const INFO_REDUCED: EventU32 = EventU32::const_from_info(INFO_EVENT); + #[test] fn test_normal_from_raw_conversion() { let conv_from_raw = EventU32TypedSev::::try_from(INFO_EVENT.raw()) @@ -693,4 +704,19 @@ mod tests { let invalid = Severity::HIGH as u8 + 1; assert!(Severity::try_from(invalid).is_err()); } + + #[test] + fn reduction() { + let event = EventU32TypedSev::::const_new(1, 1); + let raw = event.raw(); + let reduced: EventU32 = event.into(); + assert_eq!(reduced.group_id(), 1); + assert_eq!(reduced.unique_id(), 1); + assert_eq!(raw, reduced.raw()); + } + + #[test] + fn const_reducation() { + assert_eq!(INFO_REDUCED.raw(), INFO_EVENT.raw()); + } } diff --git a/fsrc-core/tests/pus_events.rs b/fsrc-core/tests/pus_events.rs index f0edbe2..cb7e1e1 100644 --- a/fsrc-core/tests/pus_events.rs +++ b/fsrc-core/tests/pus_events.rs @@ -40,7 +40,7 @@ const SOME_LOW_SEV_EVENT: EventU32TypedSev = EventU32TypedSev::cons //#[event_group] const TEST_GROUP_NAME: u16 = 1; // Auto-generated? -const TEST_GROUP_NAME_NAME: &'static str = "TEST_GROUP_NAME"; +const TEST_GROUP_NAME_NAME: &str = "TEST_GROUP_NAME"; //#[event(desc="Some medium severity event")] const MEDIUM_SEV_EVENT_IN_OTHER_GROUP: EventU32TypedSev = @@ -60,21 +60,21 @@ const MEDIUM_SEV_EVENT_IN_OTHER_GROUP_INTROSPECTION: EventIntrospection = }; const CONST_SLICE: &'static [u8] = &[0, 1, 2, 3]; -const INTROSPECTION_FOR_TEST_GROUP_0: [&'static EventIntrospection; 2] = [ +const INTROSPECTION_FOR_TEST_GROUP_0: [&EventIntrospection; 2] = [ &INFO_EVENT_0_INTROSPECTION, &INFO_EVENT_0_INTROSPECTION ]; -const INTROSPECTION_FOR_TABLE: &'static [&EventIntrospection] = &INTROSPECTION_FOR_TEST_GROUP_0; +//const INTROSPECTION_FOR_TABLE: &'static [&EventIntrospection] = &INTROSPECTION_FOR_TEST_GROUP_0; -const INTROSPECTION_FOR_TEST_GROUP_NAME: [&'static EventIntrospection; 1] = [ +const INTROSPECTION_FOR_TEST_GROUP_NAME: [&EventIntrospection; 1] = [ &MEDIUM_SEV_EVENT_IN_OTHER_GROUP_INTROSPECTION ]; -const BLAH: &'static [&EventIntrospection] = &INTROSPECTION_FOR_TEST_GROUP_NAME; +//const BLAH: &'static [&EventIntrospection] = &INTROSPECTION_FOR_TEST_GROUP_NAME; -const ALL_EVENTS: [&'static [&EventIntrospection]; 2] = [ - INTROSPECTION_FOR_TABLE, - BLAH +const ALL_EVENTS: [&[&EventIntrospection]; 2] = [ + &INTROSPECTION_FOR_TEST_GROUP_0, + &INTROSPECTION_FOR_TEST_GROUP_NAME ]; #[test]