Skip to content

Commit

Permalink
Validate provided version
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwalker committed Jun 12, 2023
1 parent a835748 commit d3ef36d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ categories = ["development-tools::testing"]
rust-version = "1.60.0"

[dependencies]
lenient_semver = { version = "0.4.2", default-features = false }
testcontainers = { version = "0.14.0" }

[dev-dependencies]
Expand Down
43 changes: 40 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,20 @@ impl Neo4j {

/// Set the Neo4j version to use.
/// The value must be an existing Neo4j version tag.
pub fn with_version(mut self, version: impl Into<String>) -> Self {
pub fn with_version(
mut self,
version: impl Into<String>,
) -> Result<Self, Box<dyn std::error::Error + Sync + Send + 'static>> {
let version: String = version.into();

let version_valid =
lenient_semver::parse_into::<'_, ValidateVersion>(&version).unwrap_or(false);
if !version_valid {
return Err(format!("Invalid version: {}", version).into());
}

self.version = Value::Value(version);
self
Ok(self)
}

/// Set the username to use.
Expand All @@ -146,7 +156,7 @@ impl Neo4j {
#[deprecated(since = "0.2.0", note = "Use `from_env().with_version()` instead.")]
#[must_use]
pub fn from_version(version: &str) -> Self {
Self::from_env().with_version(version)
Self::from_env().with_version(version).unwrap()
}

/// Create a new instance of a Neo4j image with the version and given user and password.
Expand All @@ -158,6 +168,7 @@ impl Neo4j {
pub fn from_auth_and_version(version: &str, user: &str, pass: &str) -> Self {
Self::from_env()
.with_version(version)
.unwrap()
.with_user(user)
.with_password(pass)
}
Expand Down Expand Up @@ -193,6 +204,32 @@ enum Value {
Value(String),
}

struct ValidateVersion(bool);

impl<'a> lenient_semver::VersionBuilder<'a> for ValidateVersion {
type Out = bool;

fn new() -> Self {
Self(true)
}

fn build(self) -> Self::Out {
self.0
}

fn add_additional(&mut self, _num: u64) {
self.0 = false;
}

fn add_pre_release(&mut self, _pre_release: &'a str) {
self.0 = false;
}

fn add_build(&mut self, _build: &'a str) {
self.0 = false;
}
}

impl Default for Neo4j {
fn default() -> Self {
Self::from_env()
Expand Down

0 comments on commit d3ef36d

Please sign in to comment.