Skip to content

Commit

Permalink
Use semver crate
Browse files Browse the repository at this point in the history
  • Loading branch information
tellet-q committed Jan 10, 2025
1 parent a42ba77 commit a7c4cbb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 29 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ tokio = { version = "1.40.0", features = ["rt-multi-thread"] }
futures-util = { version = "0.3.31", optional = true }
derive_builder = { version = "0.20.2" }
thiserror = "1.0.64"
semver = "1.0.24"

[dev-dependencies]
tonic-build = { version = "0.12.3", features = ["prost"] }
Expand Down
42 changes: 13 additions & 29 deletions src/qdrant_client/version_check.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
use std::error::Error;
use std::fmt;
use semver::Version;

#[derive(Debug, Clone)]
pub struct Version {
pub major: u32,
pub minor: u32,
}

impl Version {
pub fn parse(version: &str) -> Result<Version, VersionParseError> {
if version.is_empty() {
return Err(VersionParseError::EmptyVersion);
}
let parts: Vec<&str> = version.split('.').collect();
if parts.len() < 2 {
return Err(VersionParseError::InvalidFormat(version.to_string()));
}

let major = parts[0]
.parse::<u32>()
.map_err(|_| VersionParseError::InvalidFormat(version.to_string()))?;
let minor = parts[1]
.parse::<u32>()
.map_err(|_| VersionParseError::InvalidFormat(version.to_string()))?;

Ok(Version { major, minor })
pub fn parse(version: &str) -> Result<Version, VersionParseError> {
if version.is_empty() {
return Err(VersionParseError::EmptyVersion);
}
match Version::parse(version) {
Ok(v) => Ok(v),
Err(_) => Err(VersionParseError::InvalidFormat(version.to_string())),
}
}

Expand Down Expand Up @@ -68,8 +52,8 @@ pub fn is_compatible(client_version: Option<&str>, server_version: Option<&str>)
}

match (
Version::parse(client_version),
Version::parse(server_version),
parse(client_version),
parse(server_version),
) {
(Ok(client), Ok(server)) => {
let major_dif = (client.major as i32 - server.major as i32).abs();
Expand All @@ -92,12 +76,12 @@ mod tests {
#[test]
fn test_is_compatible() {
let test_cases = vec![
(Some("1.9.3.dev0"), Some("2.8.1.dev12-something"), false),
(Some("1.9.3.dev0"), Some("2.8.1-dev12"), false),
(Some("1.9"), Some("2.8"), false),
(Some("1"), Some("2"), false),
(Some("1.9.0"), Some("2.9.0"), false),
(Some("1.1.0"), Some("1.2.9"), true),
(Some("1.2.7"), Some("1.1.8.dev0"), true),
(Some("1.2.7"), Some("1.1.8-dev0"), true),
(Some("1.2.1"), Some("1.2.29"), true),
(Some("1.2.0"), Some("1.2.0"), true),
(Some("1.2.0"), Some("1.4.0"), false),
Expand Down Expand Up @@ -138,7 +122,7 @@ mod tests {
];

for (input, expected_error) in test_cases {
let result = Version::parse(input);
let result = parse(input);
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), expected_error.to_string());
}
Expand Down

0 comments on commit a7c4cbb

Please sign in to comment.