use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct PeerId(String);
impl PeerId {
pub fn new_with_rng<R: rand::Rng>(rng: &mut R) -> Self {
let id: u64 = rng.random();
Self(format!("peer-{id}"))
}
pub fn from_string(s: String) -> Self {
Self(s)
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn into_string(self) -> String {
self.0
}
}
impl fmt::Display for PeerId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<String> for PeerId {
fn from(s: String) -> Self {
PeerId(s)
}
}
impl From<&str> for PeerId {
fn from(s: &str) -> Self {
PeerId(s.to_string())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PeerIdError {
InvalidFormat,
}
impl fmt::Display for PeerIdError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PeerIdError::InvalidFormat => write!(f, "Invalid peer ID format"),
}
}
}
impl std::error::Error for PeerIdError {}