From 839ff02591fe0f8b2036b8bd3d7a425617ba075b Mon Sep 17 00:00:00 2001 From: Olivier Lacroix Date: Thu, 4 Apr 2024 22:15:48 +1100 Subject: [PATCH] Try and match display --- src/project/manifest/error.rs | 24 ++++++++++++++---------- src/project/manifest/pyproject.rs | 2 +- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/project/manifest/error.rs b/src/project/manifest/error.rs index 93447a14f..2b982fc1d 100644 --- a/src/project/manifest/error.rs +++ b/src/project/manifest/error.rs @@ -74,16 +74,16 @@ pub enum RequirementConversionError { #[derive(Error, Debug, Clone)] pub enum TomlError { - #[error("failed to parse project manifest")] - Error(#[from] toml_edit::TomlError), - #[error("'pyproject.toml' should contain a [project] table")] - NoProjectTable(std::ops::Range), - #[error("The [project] table should contain a 'name'")] + #[error("{0}")] + Error(toml_edit::TomlError), + #[error("Missing field `project`")] + NoProjectTable, + #[error("Missing field `name`")] NoProjectName(Option>), } impl TomlError { - pub fn to_fancy(&self, file_name: &str, contents: impl Into) -> Result { + pub fn to_fancy(self, file_name: &str, contents: impl Into) -> Result { if let Some(span) = self.clone().span() { Err(miette::miette!( labels = vec![LabeledSpan::at(span, self.message())], @@ -98,21 +98,25 @@ impl TomlError { fn span(self) -> Option> { match self { TomlError::Error(e) => e.span(), - TomlError::NoProjectTable(span) => Some(span), + TomlError::NoProjectTable => Some(0..1), TomlError::NoProjectName(span) => span, } } fn message(&self) -> &str { match self { TomlError::Error(e) => e.message(), - TomlError::NoProjectTable(_) => "Missing field `project`", + TomlError::NoProjectTable => "Missing field `project`", TomlError::NoProjectName(_) => "Missing field `name`", } } } - impl From for TomlError { fn from(e: toml_edit::de::Error) -> Self { - toml_edit::TomlError::from(e).into() + TomlError::Error(e.into()) + } +} +impl From for TomlError { + fn from(e: toml_edit::TomlError) -> Self { + TomlError::Error(e) } } diff --git a/src/project/manifest/pyproject.rs b/src/project/manifest/pyproject.rs index 4af382281..008f68eaa 100644 --- a/src/project/manifest/pyproject.rs +++ b/src/project/manifest/pyproject.rs @@ -40,7 +40,7 @@ impl PyProjectManifest { // This will ensure project.name is defined // TODO: do we want to Err if tool.pixi.name is defined? if manifest.project.is_none() { - return Err(TomlError::NoProjectTable(0..1)); + return Err(TomlError::NoProjectTable); } Ok(manifest)