Skip to content

Commit

Permalink
connection: implement the error trait
Browse files Browse the repository at this point in the history
  • Loading branch information
mathstuf committed Sep 27, 2015
1 parent 79f0fba commit a0ecf45
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use std::collections::VecDeque;
use std::env;
use std::error;
use std::fmt;
use std::net::{TcpStream,ToSocketAddrs};
use std::io;
use std::io::{Read,Write};
Expand Down Expand Up @@ -109,6 +111,33 @@ impl From<ParseIntError> for Error {
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::Disconnected => write!(f, "disconnected"),
Error::IOError(ref ioerr) => write!(f, "i/o error: {}", ioerr),
Error::DemarshalError(ref dmerr) => write!(f, "demarshall error: {}", dmerr),
Error::AddressError(ref addrerr) => write!(f, "address error: {:?}", addrerr),
Error::BadData => write!(f, "bad data"),
Error::AuthFailed => write!(f, "authentication failed"),
Error::NoEnvironment => write!(f, "no environment"),
}
}
}

impl error::Error for Error {
fn description(&self) -> &str {
"D-Bus error"
}

fn cause(&self) -> Option<&error::Error> {
match *self {
Error::IOError(ref ioerr) => Some(ioerr),
_ => None,
}
}
}

fn read_exactly(sock: &mut StreamSocket, buf: &mut Vec<u8>, len: usize) -> Result<(),Error> {
buf.truncate(0);
buf.reserve(len);
Expand Down
16 changes: 16 additions & 0 deletions src/demarshal.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fmt;
use std::collections::HashMap;
use std::mem::transmute;

Expand All @@ -13,6 +14,21 @@ pub enum DemarshalError {
MismatchedParens,
}

impl fmt::Display for DemarshalError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let msg = match *self {
DemarshalError::MessageTooShort => "message too short",
DemarshalError::CorruptedMessage => "corrupted message",
DemarshalError::BadUTF8 => "bad utf-8",
DemarshalError::BadSignature => "bad signature",
DemarshalError::ElementTooBig => "element too big",
DemarshalError::MismatchedParens => "mismatched parens",
};

write!(f, "{}", msg)
}
}

pub fn get_alignment(sig: char) -> usize {
match sig {
'y' => 1,
Expand Down

0 comments on commit a0ecf45

Please sign in to comment.