Skip to content

Commit

Permalink
feat(display): implement Display for MessageHubError
Browse files Browse the repository at this point in the history
Signed-off-by: Riccardo Gallo <[email protected]>
  • Loading branch information
rgallor committed Jul 25, 2024
1 parent a3bb57a commit da8b9d0
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions rust/astarte-message-hub-proto/src/proto_message_hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use self::{astarte_data_type::Data, astarte_message::Payload};
use serde::Serialize;
use std::fmt::{Display, Formatter};
use uuid::Uuid;

include!("astarteplatform.msghub.rs");
Expand Down Expand Up @@ -280,6 +281,13 @@ impl MessageHubEvent {
})
}

pub fn take_error(self) -> Option<MessageHubError> {
self.event.and_then(|r| match r {
message_hub_event::Event::Message(_) => None,
message_hub_event::Event::Error(err) => Some(err),
})
}

pub fn from_error(error: &dyn std::error::Error) -> Self {
Self {
event: Some(message_hub_event::Event::Error(
Expand Down Expand Up @@ -317,6 +325,24 @@ impl MessageHubError {
}
}

impl Display for MessageHubError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}, source: [", self.description)?;

let Some((first, remaining)) = self.source.split_first() else {
return write!(f, "]");
};

write!(f, "{first}")?;
for s in remaining {
write!(f, ", {s}")?;
}
write!(f, "]")?;

Ok(())
}
}

#[cfg(test)]
mod test {
use std::collections::HashMap;
Expand Down

0 comments on commit da8b9d0

Please sign in to comment.