54 lines
1 KiB
Rust
54 lines
1 KiB
Rust
use anyhow::bail;
|
|
use i2p::net::I2pSocketAddr;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::types::PeerId;
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
pub struct Peer {
|
|
id: PeerId,
|
|
name: String,
|
|
family: Vec<PeerId>,
|
|
}
|
|
|
|
impl Peer {
|
|
pub fn new(id: PeerId, name: String) -> Self {
|
|
Peer {
|
|
id: id,
|
|
name: name,
|
|
family: vec![],
|
|
}
|
|
}
|
|
|
|
pub fn addr(&self) -> I2pSocketAddr {
|
|
self.id.addr()
|
|
}
|
|
|
|
pub fn id(&self) -> PeerId {
|
|
self.id.clone()
|
|
}
|
|
|
|
pub fn name(&self) -> String {
|
|
self.name.clone()
|
|
}
|
|
}
|
|
|
|
impl TryFrom<&str> for Peer {
|
|
type Error = anyhow::Error;
|
|
|
|
fn try_from(value: &str) -> Result<Self, Self::Error> {
|
|
match serde_json::from_str(value) {
|
|
Ok(p) => Ok(p),
|
|
Err(e) => bail!(e),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl TryFrom<String> for Peer {
|
|
type Error = anyhow::Error;
|
|
|
|
fn try_from(value: String) -> Result<Self, Self::Error> {
|
|
Self::try_from(value.as_str())
|
|
}
|
|
}
|