Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

elk-#8 Impl error trait for user facing errors #46

Merged
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
4 changes: 2 additions & 2 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ elk_cargo_fmt_and_clippy_template: &ELK_CARGO_FMT_AND_CLIPPY
- cargo clippy -- -D warnings

elk_common_build_debug_template: &ELK_COMMON_BUILD_DEBUG
build_script: cargo build --all-targets
build_script: cargo build --workspace --all-targets

elk_common_build_and_test_debug_template: &ELK_COMMON_BUILD_AND_TEST_DEBUG
<<: *ELK_COMMON_BUILD_DEBUG
test_script: cargo test --workspace --no-fail-fast

elk_common_build_release_template: &ELK_COMMON_BUILD_RELEASE
build_script: cargo build --release --all-targets
build_script: cargo build --release --workspace --all-targets

elk_common_build_and_test_no_doc_tests_release_template: &ELK_COMMON_BUILD_AND_TEST_NO_DOC_TESTS_RELEASE
<<: *ELK_COMMON_BUILD_RELEASE
Expand Down
76 changes: 32 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,28 +93,27 @@ while a subscriber efficiently receives and prints the data.
use elkodon::prelude::*;
use elkodon_bb_posix::signal::SignalHandler;

fn main() {
let service_name = ServiceName::new(b"My/Funk/ServiceName").unwrap();
fn main() -> Result<(), Box<dyn std::error::Error>> {
let service_name = ServiceName::new(b"My/Funk/ServiceName")?;

let service = zero_copy::Service::new(&service_name)
.publish_subscribe()
.open_or_create::<usize>()
.expect("failed to create/open service");
.open_or_create::<usize>()?;

let publisher = service
.publisher()
.create()
.expect("failed to create publisher");
let publisher = service.publisher().create()?;

while !SignalHandler::termination_requested() {
let mut sample = publisher.loan().expect("Failed to acquire sample");
unsafe { sample.write(1234); }
publisher.send(sample).expect("Failed to send sample");
let mut sample = publisher.loan()?;
unsafe {
sample.as_mut_ptr().write(1234);
}
publisher.send(sample)?;
elfenpiff marked this conversation as resolved.
Show resolved Hide resolved

std::thread::sleep(std::time::Duration::from_secs(1));
}
}

Ok(())
}
```

**subscriber.rs**
Expand All @@ -123,26 +122,24 @@ fn main() {
use elkodon::prelude::*;
use elkodon_bb_posix::signal::SignalHandler;

fn main() {
let service_name = ServiceName::new(b"My/Funk/ServiceName").unwrap();
fn main() -> Result<(), Box<dyn std::error::Error>> {
let service_name = ServiceName::new(b"My/Funk/ServiceName")?;

let service = zero_copy::Service::new(&service_name)
.publish_subscribe()
.open_or_create::<TransmissionData>()
.expect("failed to create/open service");
.open_or_create::<usize>()?;
elfenpiff marked this conversation as resolved.
Show resolved Hide resolved

let subscriber = service
.subscriber()
.create()
.expect("Failed to create subscriber");
let subscriber = service.subscriber().create()?;

while !SignalHandler::termination_requested() {
while let Some(sample) = subscriber.receive().unwrap() {
while let Some(sample) = subscriber.receive()? {
println!("received: {:?}", *sample);
}

std::thread::sleep(std::time::Duration::from_secs(1));
}

Ok(())
}
```

Expand Down Expand Up @@ -172,29 +169,25 @@ This minimal example showcases an event notification between two processes.
use elkodon::prelude::*;
use elkodon_bb_posix::signal::SignalHandler;

fn main() {
let event_name = ServiceName::new(b"MyEventName").unwrap();
fn main() -> Result<(), Box<dyn std::error::Error>> {
let event_name = ServiceName::new(b"MyEventName")?;

let event = zero_copy::Service::new(&event_name)
.event()
.open_or_create()
.expect("failed to create/open event");
.open_or_create()?;

let notifier = event
.notifier()
.create()
.expect("failed to create notifier");
let notifier = event.notifier().create()?;

let mut counter: u64 = 0;
while !SignalHandler::termination_requested() {
counter += 1;
notifier
.notify_with_custom_trigger_id(EventId::new(counter))
.expect("failed to trigger event");
notifier.notify_with_custom_event_id(EventId::new(counter))?;

println!("Trigger event with id {} ...", counter);
std::thread::sleep(std::time::Duration::from_secs(1));
}

Ok(())
}
```

Expand All @@ -204,27 +197,22 @@ fn main() {
use elkodon::prelude::*;
use elkodon_bb_posix::signal::SignalHandler;

fn main() {
let event_name = ServiceName::new(b"MyEventName").unwrap();
fn main() -> Result<(), Box<dyn std::error::Error>> {
let event_name = ServiceName::new(b"MyEventName")?;

let event = zero_copy::Service::new(&event_name)
.event()
.open_or_create()
.expect("failed to create/open event");
.open_or_create()?;

let mut listener = event
.listener()
.create()
.expect("failed to create listener");
let mut listener = event.listener().create()?;

while !SignalHandler::termination_requested() {
for event_id in listener
.timed_wait(std::time::Duration::from_secs(1))
.expect("failed to wait on listener")
{
for event_id in listener.timed_wait(std::time::Duration::from_secs(1))? {
println!("event was triggered with id: {:?}", event_id);
}
}

Ok(())
}
```

Expand Down
8 changes: 8 additions & 0 deletions elkodon/src/global_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ pub enum ConfigCreationError {
UnableToDeserializeContents,
}

impl std::fmt::Display for ConfigCreationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::write!(f, "{}::{:?}", std::stringify!(Self), self)
}
}

impl std::error::Error for ConfigCreationError {}

#[derive(Serialize, Deserialize, Default, Debug, Clone)]
pub struct Service {
pub directory: String,
Expand Down
8 changes: 8 additions & 0 deletions elkodon/src/port/details/publisher_connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ enum_gen! { ConnectionFailure
SharedMemoryOpenError to UnableToMapPublishersDataSegment
}

impl std::fmt::Display for ConnectionFailure {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::write!(f, "{}::{:?}", std::stringify!(Self), self)
}
}

impl std::error::Error for ConnectionFailure {}

#[derive(Debug)]
pub(crate) struct Connection<'global_config, Service: service::Details<'global_config>> {
pub(crate) receiver:
Expand Down
8 changes: 8 additions & 0 deletions elkodon/src/port/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ pub enum ListenerCreateError {
ResourceCreationFailed,
}

impl std::fmt::Display for ListenerCreateError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::write!(f, "{}::{:?}", std::stringify!(Self), self)
}
}

impl std::error::Error for ListenerCreateError {}

#[derive(Debug)]
pub struct Listener<'a, 'global_config: 'a, Service: service::Details<'global_config>> {
_dynamic_config_guard: Option<UniqueIndex<'a>>,
Expand Down
8 changes: 8 additions & 0 deletions elkodon/src/port/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ pub enum DegrationAction {
Fail,
}

impl std::fmt::Display for DegrationAction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::write!(f, "{}::{:?}", std::stringify!(Self), self)
}
}

impl std::error::Error for DegrationAction {}

tiny_fn! {
pub struct DegrationCallback = Fn(service: service::static_config::StaticConfig, publisher_id: UniquePublisherId, subscriber_id: UniqueSubscriberId) -> DegrationAction;
}
Expand Down
16 changes: 16 additions & 0 deletions elkodon/src/port/notifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,27 @@ pub enum NotifierCreateError {
ExceedsMaxSupportedNotifiers,
}

impl std::fmt::Display for NotifierCreateError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::write!(f, "{}::{:?}", std::stringify!(Self), self)
}
}

impl std::error::Error for NotifierCreateError {}

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum NotifierConnectionUpdateFailure {
OnlyPartialUpdate,
}

impl std::fmt::Display for NotifierConnectionUpdateFailure {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::write!(f, "{}::{:?}", std::stringify!(Self), self)
}
}

impl std::error::Error for NotifierConnectionUpdateFailure {}

#[derive(Debug, Default)]
struct ListenerConnections<'global_config, Service: service::Details<'global_config>> {
#[allow(clippy::type_complexity)]
Expand Down
24 changes: 24 additions & 0 deletions elkodon/src/port/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,43 @@ pub enum PublisherCreateError {
UnableToCreateDataSegment,
}

impl std::fmt::Display for PublisherCreateError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::write!(f, "{}::{:?}", std::stringify!(Self), self)
}
}

impl std::error::Error for PublisherCreateError {}

#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
pub enum LoanError {
OutOfMemory,
ExceedsMaxLoanedChunks,
InternalFailure,
}

impl std::fmt::Display for LoanError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::write!(f, "{}::{:?}", std::stringify!(Self), self)
}
}

impl std::error::Error for LoanError {}

enum_gen! { SendCopyError
mapping:
LoanError to LoanError,
ZeroCopyCreationError to ConnectionError
}

impl std::fmt::Display for SendCopyError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::write!(f, "{}::{:?}", std::stringify!(Self), self)
}
}

impl std::error::Error for SendCopyError {}

pub(crate) fn data_segment_name(publisher_id: UniquePublisherId) -> FileName {
let msg = "The system does not support the required file name length for the publishers data segment.";
let origin = "data_segment_name()";
Expand Down
16 changes: 16 additions & 0 deletions elkodon/src/port/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,27 @@ pub enum ReceiveError {
ConnectionFailure(ConnectionFailure),
}

impl std::fmt::Display for ReceiveError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::write!(f, "{}::{:?}", std::stringify!(Self), self)
}
}

impl std::error::Error for ReceiveError {}

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum SubscriberCreateError {
ExceedsMaxSupportedSubscribers,
}

impl std::fmt::Display for SubscriberCreateError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::write!(f, "{}::{:?}", std::stringify!(Self), self)
}
}

impl std::error::Error for SubscriberCreateError {}

#[derive(Debug)]
pub struct Subscriber<
'a,
Expand Down
24 changes: 24 additions & 0 deletions elkodon/src/service/builder/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ pub enum EventOpenError {
UnableToOpenDynamicServiceInformation,
}

impl std::fmt::Display for EventOpenError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::write!(f, "{}::{:?}", std::stringify!(Self), self)
}
}

impl std::error::Error for EventOpenError {}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EventCreateError {
Corrupted,
Expand All @@ -31,13 +39,29 @@ pub enum EventCreateError {
UnableToCreateStaticServiceInformation,
}

impl std::fmt::Display for EventCreateError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::write!(f, "{}::{:?}", std::stringify!(Self), self)
}
}

impl std::error::Error for EventCreateError {}

enum_gen! {
EventOpenOrCreateError
mapping:
EventOpenError,
EventCreateError
}

impl std::fmt::Display for EventOpenOrCreateError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::write!(f, "{}::{:?}", std::stringify!(Self), self)
}
}

impl std::error::Error for EventOpenOrCreateError {}

#[derive(Debug)]
pub struct Builder<'global_config, ServiceType: service::Details<'global_config>> {
base: builder::BuilderWithServiceType<'global_config, ServiceType>,
Expand Down
16 changes: 16 additions & 0 deletions elkodon/src/service/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,29 @@ enum_gen! {
DynamicStorageOpenError
}

impl std::fmt::Display for OpenDynamicStorageFailure {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::write!(f, "{}::{:?}", std::stringify!(Self), self)
}
}

impl std::error::Error for OpenDynamicStorageFailure {}

enum_gen! {
ReadStaticStorageFailure
mapping:
StaticStorageOpenError,
StaticStorageReadError
}

impl std::fmt::Display for ReadStaticStorageFailure {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::write!(f, "{}::{:?}", std::stringify!(Self), self)
}
}

impl std::error::Error for ReadStaticStorageFailure {}

#[derive(Debug)]
pub struct Builder<S: Service> {
name: ServiceName,
Expand Down
Loading
Loading