File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -114,16 +114,25 @@ fn read_to_string_bom(path: &Path) -> anyhow::Result<String> {
114114}
115115
116116fn 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
You can’t perform that action at this time.
0 commit comments