Skip to main content

cargo_pants/
error.rs

1// Copyright 2019 Glenn Mohre, Sonatype.
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//! Error types used by this crate
15
16#[cfg(feature = "chrono")]
17use chrono;
18use std::str::Utf8Error;
19
20#[derive(Debug, thiserror::Error)]
21pub enum Error {
22    #[error("bad parameter")]
23    BadParam,
24
25    /// An error occurred performing an I/O operation (e.g. network, file)
26    #[error("I/O operation failed")]
27    Io(#[from] std::io::Error),
28
29    /// Couldn't parse response data
30    #[error("couldn't parse data as UTF-8")]
31    ParseUtf8(Utf8Error),
32
33    #[cfg(feature = "chrono")]
34    /// Couldn't parse response data
35    #[error("couldn't parse data")]
36    ParseChrono(#[from] chrono::ParseError),
37
38    /// A Cargo Metadata error occurred
39    #[error("couldn't get cargo metadata")]
40    CargoMetadataError(#[from] cargo_metadata::Error),
41
42    #[error(r#"couldn't open the Cargo.lock file: "{lock_file}""#)]
43    LockFileOpen {
44        lock_file: String,
45        open_error: std::io::Error,
46    },
47
48    /// Error processing the Cargo.toml file
49    #[error(r#"couldn't parse the Cargo.toml file: "{lock_file}""#)]
50    ParseCargoLockToml { lock_file: String },
51}
52
53impl Error {
54    pub fn from_file_open(lock_file: &str, open_error: std::io::Error) -> Self {
55        Self::LockFileOpen {
56            lock_file: lock_file.to_owned(),
57            open_error,
58        }
59    }
60
61    pub fn from_cargo_toml(lock_file: &str) -> Self {
62        Self::ParseCargoLockToml {
63            lock_file: lock_file.to_owned(),
64        }
65    }
66}