Impl new fn helpers::parse_version

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-08-07 18:37:45 +10:00
parent 20c7b61e7a
commit 51d6b3039b
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

@ -8,6 +8,7 @@ use std::sync::Arc;
use bytes::Bytes; use bytes::Bytes;
use cargo_toml::Manifest; use cargo_toml::Manifest;
use compact_str::format_compact;
use futures_util::stream::Stream; use futures_util::stream::Stream;
use log::debug; use log::debug;
use once_cell::sync::{Lazy, OnceCell}; use once_cell::sync::{Lazy, OnceCell};
@ -86,6 +87,20 @@ pub async fn await_task<T>(task: tokio::task::JoinHandle<miette::Result<T>>) ->
} }
} }
pub fn parse_version(version: &str) -> Result<semver::VersionReq, semver::Error> {
// Treat 0.1.2 as =0.1.2
if version
.chars()
.next()
.map(|ch| ch.is_ascii_digit())
.unwrap_or(false)
{
format_compact!("={version}").parse()
} else {
version.parse()
}
}
/// Load binstall metadata from the crate `Cargo.toml` at the provided path /// Load binstall metadata from the crate `Cargo.toml` at the provided path
pub fn load_manifest_path<P: AsRef<Path>>( pub fn load_manifest_path<P: AsRef<Path>>(
manifest_path: P, manifest_path: P,