|
| 1 | +use std::fmt; |
| 2 | + |
| 3 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 4 | +pub enum UnrecognizedValue { |
| 5 | + UnknownResolver(String), |
| 6 | + UnknownEdition(String), |
| 7 | +} |
| 8 | + |
| 9 | +impl std::fmt::Display for UnrecognizedValue { |
| 10 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 11 | + match self { |
| 12 | + UnrecognizedValue::UnknownResolver(resolver) => { |
| 13 | + write!(f, "Unrecognized resolver version: {}", resolver) |
| 14 | + } |
| 15 | + UnrecognizedValue::UnknownEdition(edition) => { |
| 16 | + write!(f, "Unrecognized Rust edition: {}", edition) |
| 17 | + } |
| 18 | + } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +impl std::error::Error for UnrecognizedValue {} |
| 23 | + |
| 24 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 25 | +pub enum Error { |
| 26 | + UnrecognizedValue(UnrecognizedValue), |
| 27 | + TomlParseError(toml::de::Error), // Adjust for the specific Serde library in use (e.g., `serde_json` or `serde_yaml`) |
| 28 | +} |
| 29 | + |
| 30 | +impl fmt::Display for Error { |
| 31 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 32 | + match self { |
| 33 | + Error::UnrecognizedValue(msg) => write!(f, "{}", msg), |
| 34 | + Error::TomlParseError(err) => write!(f, "Failed to parse Cargo.toml: {}", err), |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl std::error::Error for Error { |
| 40 | + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 41 | + match self { |
| 42 | + Error::UnrecognizedValue(err) => Some(err), |
| 43 | + Error::TomlParseError(err) => Some(err), |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl From<UnrecognizedValue> for Error { |
| 49 | + fn from(value: UnrecognizedValue) -> Self { |
| 50 | + Error::UnrecognizedValue(value) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl From<toml::de::Error> for Error { |
| 55 | + fn from(err: toml::de::Error) -> Self { |
| 56 | + Error::TomlParseError(err) |
| 57 | + } |
| 58 | +} |
0 commit comments