Skip to content

Commit

Permalink
refactor!: move ActorIDFromBytesError to error module
Browse files Browse the repository at this point in the history
  • Loading branch information
tqwewe committed Oct 7, 2024
1 parent 463e3a7 commit 816eb93
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
29 changes: 1 addition & 28 deletions src/actor/id.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::error;
use std::sync::atomic::Ordering;
use std::{fmt, sync::atomic::AtomicU64};

use internment::Intern;
use libp2p::identity::ParseError;
use libp2p::PeerId;
use serde::{Deserialize, Serialize};

use crate::error::ActorIDFromBytesError;
use crate::remote::ActorSwarm;

static ACTOR_COUNTER: AtomicU64 = AtomicU64::new(0);
Expand Down Expand Up @@ -208,29 +207,3 @@ impl<'de> Deserialize<'de> for ActorID {
})
}
}

/// Errors that can occur when deserializing an `ActorID` from bytes.
#[derive(Debug)]
pub enum ActorIDFromBytesError {
/// The byte slice doesn't contain enough data for the `sequence_id`.
MissingSequenceID,
/// An error occurred while parsing the `PeerId`.
ParsePeerID(ParseError),
}

impl From<ParseError> for ActorIDFromBytesError {
fn from(err: ParseError) -> Self {
ActorIDFromBytesError::ParsePeerID(err)
}
}

impl fmt::Display for ActorIDFromBytesError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ActorIDFromBytesError::MissingSequenceID => write!(f, "missing instance ID"),
ActorIDFromBytesError::ParsePeerID(err) => err.fmt(f),
}
}
}

impl error::Error for ActorIDFromBytesError {}
29 changes: 28 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::{
};

use libp2p::TransportError;
use libp2p_identity::ParseError;
use serde::{Deserialize, Serialize};
use tokio::{
sync::{mpsc, oneshot},
Expand Down Expand Up @@ -620,7 +621,33 @@ impl fmt::Display for PanicError {
}
}

/// An infallible error type, similar to std::convert::Infallible.
/// Errors that can occur when deserializing an `ActorID` from bytes.
#[derive(Debug)]
pub enum ActorIDFromBytesError {
/// The byte slice doesn't contain enough data for the `sequence_id`.
MissingSequenceID,
/// An error occurred while parsing the `PeerId`.
ParsePeerID(ParseError),
}

impl From<ParseError> for ActorIDFromBytesError {
fn from(err: ParseError) -> Self {
ActorIDFromBytesError::ParsePeerID(err)
}
}

impl fmt::Display for ActorIDFromBytesError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ActorIDFromBytesError::MissingSequenceID => write!(f, "missing instance ID"),
ActorIDFromBytesError::ParsePeerID(err) => err.fmt(f),
}
}
}

impl error::Error for ActorIDFromBytesError {}

/// An infallible error type, similar to [std::convert::Infallible].
///
/// Kameo provides its own Infallible type in order to implement Serialize/Deserialize for it.
#[derive(Copy, Serialize, Deserialize)]
Expand Down

0 comments on commit 816eb93

Please sign in to comment.