Use parsed Version instead of String
MODIFY
build.rs
build.rs
@@ -177,7 +177,7 @@ impl fmt::Display for EnvList<'_> {
#[derive(Debug)]
struct Library {
pub include_paths: Vec<PathBuf>,
- pub version: String,
+ pub version: Version,
pub cargo_metadata: Vec<String>,
}
@@ -204,7 +204,7 @@ impl Library {
})
}
- fn version_from_include_paths(include_paths: impl IntoIterator<Item=impl AsRef<Path>>) -> Option<String> {
+ fn version_from_include_paths(include_paths: impl IntoIterator<Item=impl AsRef<Path>>) -> Option<Version> {
include_paths.into_iter().find_map(|x| get_version_from_headers(x.as_ref()))
}
@@ -278,7 +278,7 @@ impl Library {
Ok(Self {
include_paths,
- version: version.unwrap_or_else(|| "0.0.0".to_owned()),
+ version: version.unwrap_or_else(|| Version::new(0, 0, 0)),
cargo_metadata,
})
} else {
@@ -307,7 +307,7 @@ impl Library {
Ok(Self {
include_paths,
- version: opencv.version,
+ version: Version::parse(&opencv.version)?,
cargo_metadata,
})
}
@@ -350,7 +350,7 @@ impl Library {
Ok(Self {
include_paths: Self::process_env_var_list(include_paths, probe_result.include_paths),
- version: probe_result.version.unwrap_or_else(|| "0.0.0".to_string()),
+ version: probe_result.version.unwrap_or_else(|| Version::new(0, 0, 0)),
cargo_metadata,
})
}
@@ -387,7 +387,7 @@ impl Library {
Ok(Self {
include_paths,
- version: version.unwrap_or_else(|| "0.0.0".to_string()),
+ version: version.unwrap_or_else(|| Version::new(0, 0, 0)),
cargo_metadata,
})
}
@@ -599,9 +599,9 @@ fn get_version_from_headers(header_dir: &Path) -> Option<String> {
line.clear();
}
if let (Some(major), Some(minor), Some(revision)) = (major, minor, revision) {
- Some(format!("{}.{}.{}", major, minor, revision))
+ Some(Version::new(major.parse().ok()?, minor.parse().ok()?, revision.parse().ok()?))
} else {
- Some("0.0.0".to_string())
+ Some(Version::new(0, 0, 0))
}
}

MODIFY
build_cmake_probe.rs
build_cmake_probe.rs
@@ -6,12 +6,13 @@ use std::{
};
use std::process::Output;
+use semver::Version;
use shlex::Shlex;
use super::Result;
pub struct ProbeResult {
- pub version: Option<String>,
+ pub version: Option<Version>,
pub include_paths: Vec<PathBuf>,
pub link_paths: Vec<PathBuf>,
pub link_libs: Vec<String>,
@@ -72,7 +73,7 @@ impl<'r> CmakeProbe<'r> {
out
}
- fn extract_from_output(output: &Output, version: &mut Option<String>, opencv_include_paths: &mut Vec<PathBuf>) -> Result<()> {
+ fn extract_from_output(output: &Output, version: &mut Option<Version>, opencv_include_paths: &mut Vec<PathBuf>) -> Result<()> {
if output.status.success() {
let mut line = String::new();
let mut reader = BufReader::new(output.stderr.as_slice());
@@ -94,7 +95,7 @@ impl<'r> CmakeProbe<'r> {
);
}
Some("OCVRS_VERSION") => {
- *version = Some(value.trim().to_string());
+ *version = Some(Version::parse(value.trim())?);
}
_ => {}
}

Use parsed Version instead of String