Massive refactoring
This commit is contained in:
parent
31dc4fd4a3
commit
a75c115761
31 changed files with 160 additions and 140 deletions
54
ubisync-lib/src/peer.rs
Normal file
54
ubisync-lib/src/peer.rs
Normal file
|
@ -0,0 +1,54 @@
|
|||
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())
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue