quickdash/error.rs
1/* Copyright [2021] [Cerda]
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.
14 */
15
16/// Enum representing each way the appication can fail.
17#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
18pub enum Error {
19 /// No errors occured, everything executed correctly.
20 NoError,
21 /// Parsing of command-line options failed.
22 OptionParsingError,
23 /// Selected and saved hash lengths differ.
24 HashLengthDiffers,
25 /// Parsing the hashes file failed.
26 HashesFileParsingFailure,
27 /// The specified amount of files do not match.
28 NFilesDiffer(i32),
29}
30
31impl Error {
32 /// Get the executable exit value from an `Error` instance.
33 pub fn exit_value(&self) -> i32 {
34 match *self {
35 Error::NoError => 0,
36 Error::OptionParsingError => 1,
37 Error::HashLengthDiffers => 2,
38 Error::HashesFileParsingFailure => 3,
39 Error::NFilesDiffer(i) => i + 3,
40 }
41 }
42}
43
44impl From<i32> for Error {
45 fn from(i: i32) -> Self {
46 match i {
47 0 => Error::NoError,
48 1 => Error::OptionParsingError,
49 2 => Error::HashLengthDiffers,
50 3 => Error::HashesFileParsingFailure,
51 i => Error::NFilesDiffer(i - 3),
52 }
53 }
54}