Skip to content

Commit

Permalink
pep440: some minor refactoring, mostly around error types (#780)
Browse files Browse the repository at this point in the history
This PR does a bit of refactoring to the pep440 crate, and in
particular around the erorr types. This PR is meant to be a precursor
to another PR that does some surgery (both in parsing and in `Version`
representation) that benefits somewhat from this refactoring.

As usual, please review commit-by-commit.
  • Loading branch information
BurntSushi authored Jan 4, 2024
1 parent 1cc3250 commit d7c9b15
Show file tree
Hide file tree
Showing 8 changed files with 710 additions and 262 deletions.
28 changes: 3 additions & 25 deletions crates/pep440-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@

pub use {
version::{LocalSegment, Operator, PreRelease, Version},
version_specifier::{parse_version_specifiers, VersionSpecifier, VersionSpecifiers},
version_specifier::{
parse_version_specifiers, VersionSpecifier, VersionSpecifiers, VersionSpecifiersParseError,
},
};

#[cfg(feature = "pyo3")]
Expand All @@ -55,30 +57,6 @@ pub use version::PyVersion;
mod version;
mod version_specifier;

/// Error with span information (unicode width) inside the parsed line
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct Pep440Error {
/// The actual error message
pub message: String,
/// The string that failed to parse
pub line: String,
/// First character for underlining (unicode width)
pub start: usize,
/// Number of characters to underline (unicode width)
pub width: usize,
}

impl std::fmt::Display for Pep440Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "Failed to parse version:")?;
writeln!(f, "{}", self.line)?;
writeln!(f, "{}{}", " ".repeat(self.start), "^".repeat(self.width))?;
Ok(())
}
}

impl std::error::Error for Pep440Error {}

/// Python bindings shipped as `pep440_rs`
#[cfg(feature = "pyo3")]
#[pymodule]
Expand Down
Loading

0 comments on commit d7c9b15

Please sign in to comment.