Skip to content

Commit

Permalink
Switch to ensure macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Techassi committed Nov 27, 2023
1 parent 821c56a commit c5a9731
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions rust/stackable-cockpit/src/platform/operator/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{fmt::Display, str::FromStr};

use semver::Version;
use snafu::{ResultExt, Snafu};
use snafu::{ensure, ResultExt, Snafu};
use tracing::{info, instrument};

use crate::{
Expand Down Expand Up @@ -77,19 +77,21 @@ impl FromStr for OperatorSpec {
let input = s.trim();

// Empty input is not allowed
if input.is_empty() {
return Err(SpecParseError::EmptyInput);
}
ensure!(!input.is_empty(), EmptyInputSnafu);

// Split at each equal sign
let parts: Vec<&str> = input.split('=').collect();
let len = parts.len();

// If there are more than 2 equal signs, return error
// because of invalid spec format
if len > 2 {
return Err(SpecParseError::InvalidEqualSignCount);
}
ensure!(len <= 2, InvalidEqualSignCountSnafu);

// Check if the provided operator name is in the list of valid operators
ensure!(
VALID_OPERATORS.contains(&parts[0]),
InvalidNameSnafu { name: parts[0] }
);

// If there is only one part, the input didn't include
// the optional version identifier
Expand All @@ -101,15 +103,7 @@ impl FromStr for OperatorSpec {
}

// If there is an equal sign, but no version after
if parts[1].is_empty() {
return Err(SpecParseError::MissingVersion);
}

if !VALID_OPERATORS.contains(&parts[0]) {
return Err(SpecParseError::InvalidName {
name: parts[0].to_string(),
});
}
ensure!(!parts[1].is_empty(), MissingVersionSnafu);

// There are two parts, so an operator name and version
let version: Version = parts[1].parse().context(ParseVersionSnafu)?;
Expand Down

0 comments on commit c5a9731

Please sign in to comment.