Skip to content

Commit

Permalink
Try and match display
Browse files Browse the repository at this point in the history
  • Loading branch information
olivier-lacroix committed Apr 4, 2024
1 parent 98ea377 commit 839ff02
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
24 changes: 14 additions & 10 deletions src/project/manifest/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize>),
#[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<std::ops::Range<usize>>),
}

impl TomlError {
pub fn to_fancy<T>(&self, file_name: &str, contents: impl Into<String>) -> Result<T, Report> {
pub fn to_fancy<T>(self, file_name: &str, contents: impl Into<String>) -> Result<T, Report> {

Check failure on line 86 in src/project/manifest/error.rs

View workflow job for this annotation

GitHub Actions / Cargo Lint

methods with the following characteristics: (`to_*` and `self` type is not `Copy`) usually take `self` by reference
if let Some(span) = self.clone().span() {
Err(miette::miette!(
labels = vec![LabeledSpan::at(span, self.message())],
Expand All @@ -98,21 +98,25 @@ impl TomlError {
fn span(self) -> Option<std::ops::Range<usize>> {
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<toml_edit::de::Error> for TomlError {
fn from(e: toml_edit::de::Error) -> Self {
toml_edit::TomlError::from(e).into()
TomlError::Error(e.into())
}
}
impl From<toml_edit::TomlError> for TomlError {
fn from(e: toml_edit::TomlError) -> Self {
TomlError::Error(e)
}
}
2 changes: 1 addition & 1 deletion src/project/manifest/pyproject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 839ff02

Please sign in to comment.