Skip to content

Commit

Permalink
Minor clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
95th committed Aug 18, 2019
1 parent 3a86191 commit 806c357
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 19 deletions.
44 changes: 44 additions & 0 deletions bencode/src/error.rs
@@ -0,0 +1,44 @@
use std::fmt;
use std::io;

#[derive(Debug)]
pub enum Error {
IO,
EOF,
ParseInt,
ParseBytes,
ParseString,
ParseList,
ParseDict,
InvalidChar(u8),
IncorrectType(String),
}

pub type Result<T> = std::result::Result<T, Error>;

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}

impl std::error::Error for Error {}

impl From<Error> for io::Error {
fn from(e: Error) -> Self {
use Error::*;
match e {
IO => io::ErrorKind::Other.into(),
EOF => io::ErrorKind::UnexpectedEof.into(),
ParseInt => io::Error::new(io::ErrorKind::InvalidData, "Unable to parse int"),
ParseBytes => io::Error::new(io::ErrorKind::InvalidData, "Unable to parse bytes"),
ParseString => io::Error::new(io::ErrorKind::InvalidData, "Unable to parse string"),
ParseList => io::Error::new(io::ErrorKind::InvalidData, "Unable to parse list"),
ParseDict => io::Error::new(io::ErrorKind::InvalidData, "Unable to parse dictionary"),
InvalidChar(v) => {
io::Error::new(io::ErrorKind::InvalidData, format!("Unexpected {}", v))
}
IncorrectType(s) => io::Error::new(io::ErrorKind::InvalidData, s),
}
}
}
16 changes: 2 additions & 14 deletions bencode/src/lib.rs
Expand Up @@ -2,21 +2,9 @@ use std::collections::BTreeMap;
use std::fmt;
use std::io;
use std::io::Cursor;
use error::{Error, Result};

#[derive(Debug)]
pub enum Error {
IO,
EOF,
ParseInt,
ParseBytes,
ParseString,
ParseList,
ParseDict,
InvalidChar(u8),
IncorrectType(String),
}

pub type Result<T> = std::result::Result<T, Error>;
pub mod error;

#[derive(Debug)]
pub enum Value {
Expand Down
4 changes: 2 additions & 2 deletions dht/src/protocol.rs
Expand Up @@ -21,15 +21,15 @@ impl Protocol {
}
}

fn get_refresh_ids(&mut self) -> Vec<Id> {
pub fn get_refresh_ids(&mut self) -> Vec<Id> {
self.router
.stale_buckets()
.iter()
.map(|bucket| Id::ranged_random(&bucket.lower, &bucket.upper))
.collect()
}

fn welcome_if_new(&mut self, node: Rc<Node>) {
pub fn welcome_if_new(&mut self, node: Rc<Node>) {
if !self.router.is_new_node(&node) {
return;
}
Expand Down
12 changes: 9 additions & 3 deletions dht/src/routing.rs
Expand Up @@ -95,15 +95,21 @@ impl RoutingTable {
.enumerate()
.find(|(_, b)| node.id < b.upper)
.map(|(i, _)| i)
.unwrap() // A node always has a bucket. So, unwrapping is OK.
.expect("Node expected in bucket")
}

fn bucket_of(&self, node: &Node) -> &Bucket {
self.buckets.iter().find(|b| node.id < b.upper).unwrap() // A node always has a bucket. So, unwrapping is OK.
self.buckets
.iter()
.find(|b| node.id < b.upper)
.expect("Node expected in bucket")
}

fn bucket_of_mut(&mut self, node: &Node) -> &mut Bucket {
self.buckets.iter_mut().find(|b| node.id < b.upper).unwrap() // A node always has a bucket. So, unwrapping is OK.
self.buckets
.iter_mut()
.find(|b| node.id < b.upper)
.expect("Node expected in bucket")
}
}

Expand Down

0 comments on commit 806c357

Please sign in to comment.