Skip to content

Commit

Permalink
pep440: move VersionSpecifiersError to version_specifiers module
Browse files Browse the repository at this point in the history
Since it's specifier to version specifier parsing, it makes sense to
have it live with the corresponding type. Putting it in lib.rs I think
makes it seem like an error type that applies to other things as well.
  • Loading branch information
BurntSushi committed Jan 4, 2024
1 parent 3793f0b commit bb31d74
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
34 changes: 3 additions & 31 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,36 +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 VersionSpecifiersParseError {
/// The actual error message
message: String,
/// The string that failed to parse
line: String,
/// The starting byte offset into the original string where the error
/// occurred.
start: usize,
/// The ending byte offset into the original string where the error
/// occurred.
end: usize,
}

impl std::fmt::Display for VersionSpecifiersParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use unicode_width::UnicodeWidthStr;

writeln!(f, "Failed to parse version: {}:", self.message)?;
writeln!(f, "{}", self.line)?;
let indent = self.line[..self.start].width();
let point = self.line[self.start..self.end].width();
writeln!(f, "{}{}", " ".repeat(indent), "^".repeat(point))?;
Ok(())
}
}

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

/// Python bindings shipped as `pep440_rs`
#[cfg(feature = "pyo3")]
#[pymodule]
Expand Down
32 changes: 31 additions & 1 deletion crates/pep440-rs/src/version_specifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use serde::{de, Deserialize, Deserializer, Serialize, Serializer};

#[cfg(feature = "pyo3")]
use crate::version::PyVersion;
use crate::{version, Operator, Version, VersionSpecifiersParseError};
use crate::{version, Operator, Version};

/// A thin wrapper around `Vec<VersionSpecifier>` with a serde implementation
///
Expand Down Expand Up @@ -183,6 +183,36 @@ impl Serialize for VersionSpecifiers {
}
}

/// Error with span information (unicode width) inside the parsed line
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct VersionSpecifiersParseError {
/// The actual error message
message: String,
/// The string that failed to parse
line: String,
/// The starting byte offset into the original string where the error
/// occurred.
start: usize,
/// The ending byte offset into the original string where the error
/// occurred.
end: usize,
}

impl std::fmt::Display for VersionSpecifiersParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use unicode_width::UnicodeWidthStr;

writeln!(f, "Failed to parse version: {}:", self.message)?;
writeln!(f, "{}", self.line)?;
let indent = self.line[..self.start].width();
let point = self.line[self.start..self.end].width();
writeln!(f, "{}{}", " ".repeat(indent), "^".repeat(point))?;
Ok(())
}
}

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

/// A version range such such as `>1.2.3`, `<=4!5.6.7-a8.post9.dev0` or `== 4.1.*`. Parse with
/// `VersionSpecifier::from_str`
///
Expand Down

0 comments on commit bb31d74

Please sign in to comment.