mirror of
https://github.com/RGBCube/dix
synced 2025-07-28 12:17:45 +00:00
main: fix regex version handling
The previous regex didn't match names without any versions, this one does.
This commit is contained in:
parent
d8f071c887
commit
78b3e4e3a9
1 changed files with 10 additions and 6 deletions
16
src/main.rs
16
src/main.rs
|
@ -253,7 +253,7 @@ fn main() {
|
||||||
fn store_path_regex() -> &'static Regex {
|
fn store_path_regex() -> &'static Regex {
|
||||||
static REGEX: OnceLock<Regex> = OnceLock::new();
|
static REGEX: OnceLock<Regex> = OnceLock::new();
|
||||||
REGEX.get_or_init(|| {
|
REGEX.get_or_init(|| {
|
||||||
Regex::new(r"(.+?)-([0-9].*?)$")
|
Regex::new(r"(.+?)(-([0-9].*?))?$")
|
||||||
.expect("Failed to compile regex pattern for nix store paths")
|
.expect("Failed to compile regex pattern for nix store paths")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -269,8 +269,8 @@ fn store_path_regex() -> &'static Regex {
|
||||||
fn get_version<'a>(pack: impl Into<&'a str>) -> Result<(&'a str, &'a str)> {
|
fn get_version<'a>(pack: impl Into<&'a str>) -> Result<(&'a str, &'a str)> {
|
||||||
let path = pack.into();
|
let path = pack.into();
|
||||||
|
|
||||||
// We can strip the path since it _always_ starts with
|
// We can strip the path since it _always_ follows the format
|
||||||
// /nix/store/....-
|
// /nix/store/<...>-<program_name>-......
|
||||||
// This part is exactly 44 chars long, so we just remove it.
|
// This part is exactly 44 chars long, so we just remove it.
|
||||||
let stripped_path = &path[44..];
|
let stripped_path = &path[44..];
|
||||||
debug!("Stripped path: {stripped_path}");
|
debug!("Stripped path: {stripped_path}");
|
||||||
|
@ -279,11 +279,15 @@ fn get_version<'a>(pack: impl Into<&'a str>) -> Result<(&'a str, &'a str)> {
|
||||||
if let Some(cap) = store_path_regex().captures(stripped_path) {
|
if let Some(cap) = store_path_regex().captures(stripped_path) {
|
||||||
// Handle potential missing captures safely
|
// Handle potential missing captures safely
|
||||||
let name = cap.get(1).map_or("", |m| m.as_str());
|
let name = cap.get(1).map_or("", |m| m.as_str());
|
||||||
let version = cap.get(2).map_or("", |m| m.as_str());
|
let mut version = cap.get(2).map_or("<none>", |m| m.as_str());
|
||||||
|
|
||||||
if name.is_empty() || version.is_empty() {
|
if version.starts_with('-') {
|
||||||
|
version = &version[1..];
|
||||||
|
}
|
||||||
|
|
||||||
|
if name.is_empty() {
|
||||||
return Err(AppError::ParseError {
|
return Err(AppError::ParseError {
|
||||||
message: format!("Failed to extract name or version from path: {path}"),
|
message: format!("Failed to extract name from path: {path}"),
|
||||||
context: "get_version".to_string(),
|
context: "get_version".to_string(),
|
||||||
source: None,
|
source: None,
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue