java_locator/
errors.rs

1// Copyright 2019 astonbitecode
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14use std::error::Error;
15use std::{fmt, result};
16
17use glob;
18
19pub type Result<T> = result::Result<T, JavaLocatorError>;
20
21#[derive(Debug)]
22pub struct JavaLocatorError {
23    description: String,
24}
25
26impl JavaLocatorError {
27    pub(crate) fn new(description: String) -> JavaLocatorError {
28        JavaLocatorError { description }
29    }
30}
31
32impl fmt::Display for JavaLocatorError {
33    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
34        write!(f, "{}", self.description)
35    }
36}
37
38impl Error for JavaLocatorError {
39    fn description(&self) -> &str {
40        self.description.as_str()
41    }
42}
43
44impl From<std::io::Error> for JavaLocatorError {
45    fn from(err: std::io::Error) -> JavaLocatorError {
46        JavaLocatorError {
47            description: format!("{:?}", err),
48        }
49    }
50}
51
52impl From<std::str::Utf8Error> for JavaLocatorError {
53    fn from(err: std::str::Utf8Error) -> JavaLocatorError {
54        JavaLocatorError {
55            description: format!("{:?}", err),
56        }
57    }
58}
59
60impl From<glob::PatternError> for JavaLocatorError {
61    fn from(err: glob::PatternError) -> JavaLocatorError {
62        JavaLocatorError {
63            description: format!("{:?}", err),
64        }
65    }
66}