Skip to content

Commit 4092b1d

Browse files
fix: improve version parsing in parse_version function to support v0.3.3-dev3
Enhance version parsing logic to handle different formats and validate input.
1 parent 8783574 commit 4092b1d

1 file changed

Lines changed: 18 additions & 9 deletions

File tree

src/main.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,25 @@ fn read_to_string_bom(path: &Path) -> anyhow::Result<String> {
114114
}
115115

116116
fn parse_version(mod_version: &serde_yaml::Value) -> String {
117-
if mod_version.is_f64() {
118-
mod_version.as_f64().unwrap().to_string()
119-
} else {
120-
let v = mod_version.as_str().unwrap_or("1.0.0").to_string();
117+
// 1. 处理数字类型 (如 YAML 中写 1.0)
118+
if let Some(f) = mod_version.as_f64() {
119+
return f.to_string();
120+
}
121121

122-
if v.chars().all(|c| c.is_ascii_digit() || c == '.') {
123-
v
124-
} else {
125-
"1.0.0".to_string()
126-
}
122+
// 2. 处理字符串类型
123+
let v_str = mod_version.as_str().unwrap_or("1.0.0");
124+
125+
// 3. 去除前缀 (例如 "v0.3.3" -> "0.3.3")
126+
// 找到第一个数字出现的位置
127+
let start_idx = v_str.find(|c: char| c.is_ascii_digit()).unwrap_or(0);
128+
let trimmed = &v_str[start_idx..];
129+
130+
// 4. 验证基本合法性
131+
// SemVer 允许数字、点、连字符和加号 (0.3.3-dev3+build1)
132+
if !trimmed.is_empty() && trimmed.chars().next().unwrap().is_ascii_digit() {
133+
trimmed.to_string()
134+
} else {
135+
"1.0.0".to_string()
127136
}
128137
}
129138

0 commit comments

Comments
 (0)