Skip to content

Commit 0a9ced8

Browse files
committed
Add semver dependency to proper validation & ordering
1 parent 1351281 commit 0a9ced8

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ nix = { optional = true, version = "0.29", features = ["signal", "user"] }
2222
notify = "8.0"
2323
regex = "1.11"
2424
russh = { optional = true, version = "0.49" }
25+
semver = "1.0"
2526
serde = { version = "1.0", features = ["derive"] }
2627
serde_json = "1.0"
2728
serde_yaml = { git = "https://github.com/markcda/serde-yaml.git", tag = "0.9.36" }

src/entities/info.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,14 @@ impl Serialize for Info {
117117

118118
static SHORT_NAME_VALIDATOR: LazyLock<Regex> = LazyLock::new(|| Regex::new("^[a-zA-Z_0-9-_]*$").unwrap());
119119

120-
static VERSION_VALIDATOR: LazyLock<Regex> = LazyLock::new(|| {
121-
Regex::new(r#"^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$"#).unwrap()
122-
});
123-
124120
/// Validates the short name.
125121
pub fn validate_short_name(short_name: &str) -> bool {
126122
SHORT_NAME_VALIDATOR.is_match(short_name)
127123
}
128124

129125
/// Validates the version.
130126
pub fn validate_version(version: &str) -> bool {
131-
if !VERSION_VALIDATOR.is_match(version) {
127+
if semver::Version::parse(version).is_err() {
132128
log(format!(
133129
"Version `{version}` is invalid! You should specify `SemVer 2.0` version."
134130
));

src/storage.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
//! Storage module.
22
3-
use anyhow::bail;
3+
use anyhow::{Context, anyhow, bail};
44
#[cfg(feature = "tui")]
55
use colored::Colorize;
66
use std::path::{Path, PathBuf};
77

8-
use crate::STORAGE_DIR;
98
use crate::entities::info::ContentInfo;
109
#[cfg(feature = "tui")]
1110
use crate::entities::info::ShortName;
1211
use crate::i18n;
1312
use crate::rw::copy_all;
13+
use crate::{STORAGE_DIR, hmap};
1414

1515
/// Environment variable name.
1616
pub const CUSTOM_STORAGE_PATH: &str = "DEPLOYER_STORAGE_PATH";
@@ -116,12 +116,17 @@ pub fn use_from_storage(storage_dir: &Path, build_dir: &Path, content_info: &Con
116116
}
117117
copy_all(&content_path, &content_path, build_dir, &[""])?;
118118
} else {
119-
let mut versions = vec![];
119+
let mut versions = hmap!();
120120
for entry in std::fs::read_dir(&content_path)? {
121121
let entry = entry?;
122-
let name = entry.file_name().to_str().unwrap().to_owned();
122+
let name = entry.file_name().to_string_lossy().to_string();
123123
if name.starts_with(content_info.short_name()) {
124-
versions.push(name);
124+
let version = name
125+
.split('@')
126+
.next_back()
127+
.ok_or(anyhow!("There is no version spec in folder name!"))?;
128+
let version = semver::Version::parse(version).context("Can't parse version as SemVer!")?;
129+
versions.insert(version, name);
125130
}
126131
}
127132
if versions.is_empty() {
@@ -132,7 +137,8 @@ pub fn use_from_storage(storage_dir: &Path, build_dir: &Path, content_info: &Con
132137
i18n::CONTENT_CONSIDER_ADD
133138
)
134139
}
135-
let max = versions.iter().max().unwrap();
140+
let max = versions.keys().max().ok_or(anyhow!("No version available!"))?;
141+
let max = versions.get(max).ok_or(anyhow!("No version available!"))?;
136142

137143
crate::rw::log(format!("Decided to choose `{max}` from `latest`."));
138144
content_path.push(max);

0 commit comments

Comments
 (0)