Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: better parse error message #222

Merged
merged 1 commit into from
Jun 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions crates/rattler_conda_types/src/platform.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use itertools::Itertools;
use serde::{Deserializer, Serializer};
use std::cmp::Ordering;
use std::fmt::Display;
use std::{fmt, fmt::Formatter, str::FromStr};
use strum::{EnumIter, IntoEnumIterator};
use thiserror::Error;
Expand Down Expand Up @@ -192,12 +194,22 @@ impl Platform {

/// An error that can occur when parsing a platform from a string.
#[derive(Debug, Error, Clone, Eq, PartialEq)]
#[error("'{string}' is not a known platform")]
pub struct ParsePlatformError {
/// The platform string that could not be parsed.
pub string: String,
}

impl Display for ParsePlatformError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(
f,
"'{}' is not a known platform. Valid platforms are {}",
self.string,
Platform::all().map(|p| format!("'{p}'")).join(", ")
)
}
}

impl FromStr for Platform {
type Err = ParsePlatformError;

Expand Down Expand Up @@ -418,7 +430,8 @@ mod tests {

#[test]
fn test_parse_platform_error() {
assert!("foo".parse::<Platform>().is_err());
let err = "foo".parse::<Platform>().unwrap_err();
println!("{err}");
}

#[test]
Expand Down