|
| 1 | +// smbc is library wrapping libsmbclient from Samba project |
| 2 | +// Copyright (c) 2016 Konstantin Gribov |
| 3 | +// |
| 4 | +// This file is part of smbc. |
| 5 | +// |
| 6 | +// smbc is free software: you can redistribute it and/or modify |
| 7 | +// it under the terms of the GNU General Public License as published by |
| 8 | +// the Free Software Foundation, either version 3 of the License, or |
| 9 | +// (at your option) any later version. |
| 10 | +// |
| 11 | +// smbc is distributed in the hope that it will be useful, |
| 12 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +// GNU General Public License for more details. |
| 15 | +// |
| 16 | +// You should have received a copy of the GNU General Public License |
| 17 | +// along with smbc. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +use std::error; |
| 20 | +use std::ffi; |
| 21 | +use std::fmt; |
| 22 | +use std::io; |
| 23 | +use std::result; |
| 24 | + |
| 25 | +pub type Result<T> = result::Result<T, Error>; |
| 26 | + |
| 27 | +#[derive(Debug)] |
| 28 | +pub enum Error { |
| 29 | + NewContext(io::Error), |
| 30 | + InitContext(io::Error), |
| 31 | + AuthCallbackPaniced(Box<error::Error>), |
| 32 | + NulInPath(ffi::NulError), |
| 33 | + Io(io::Error), |
| 34 | +} |
| 35 | + |
| 36 | +impl fmt::Display for Error { |
| 37 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 38 | + match *self { |
| 39 | + Error::NewContext(ref err) => write!(f, "New context error: {}", err), |
| 40 | + Error::InitContext(ref err) => write!(f, "Init context error: {}", err), |
| 41 | + Error::Io(ref err) => write!(f, "IO error: {}", err), |
| 42 | + Error::NulInPath(ref err) => write!(f, "NUL in path: {}", err), |
| 43 | + Error::AuthCallbackPaniced(ref err) => write!(f, "Auth callback paniced last time: {}", err) |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl error::Error for Error { |
| 49 | + fn description(&self) -> &str { |
| 50 | + match *self { |
| 51 | + Error::NewContext(ref err) => err.description(), |
| 52 | + Error::InitContext(ref err) => err.description(), |
| 53 | + Error::Io(ref err) => err.description(), |
| 54 | + Error::NulInPath(ref err) => err.description(), |
| 55 | + Error::AuthCallbackPaniced(ref _err) => "panic in auth callback", |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + fn cause(&self) -> Option<&error::Error> { |
| 60 | + match *self { |
| 61 | + Error::NewContext(ref err) => Some(err), |
| 62 | + Error::InitContext(ref err) => Some(err), |
| 63 | + Error::Io(ref err) => Some(err), |
| 64 | + Error::NulInPath(ref err) => Some(err), |
| 65 | + Error::AuthCallbackPaniced(ref err) => Some(err.as_ref()), |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl From<io::Error> for Error { |
| 71 | + fn from(err: io::Error) -> Self { |
| 72 | + Error::Io(err) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +impl From<ffi::NulError> for Error { |
| 77 | + fn from(err: ffi::NulError) -> Self { |
| 78 | + Error::NulInPath(err) |
| 79 | + } |
| 80 | +} |
0 commit comments