From f983390ac0cf6578d2dc0799c5b24c07b9da0c86 Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar Date: Sat, 11 Jan 2025 14:23:57 -0500 Subject: [PATCH] ci: Parse --on URI using `url` crate --- crates/nix_rs/src/store/uri.rs | 41 ++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/crates/nix_rs/src/store/uri.rs b/crates/nix_rs/src/store/uri.rs index ac806294..4d2ba59d 100644 --- a/crates/nix_rs/src/store/uri.rs +++ b/crates/nix_rs/src/store/uri.rs @@ -3,6 +3,7 @@ use std::{fmt, str::FromStr}; use serde::{Deserialize, Serialize}; use thiserror::Error; +use url::Url; /// Nix Store URI #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] @@ -23,6 +24,13 @@ pub struct SSHStoreURI { /// Error parsing a store URI #[derive(Error, Debug)] pub enum StoreURIParseError { + /// Parse error + #[error(transparent)] + ParseError { + /// Source error + #[from] + source: url::ParseError, + }, /// Invalid URI format #[error("Invalid URI format")] InvalidFormat, @@ -39,28 +47,23 @@ impl StoreURI { /// /// Currently only supports `ssh` scheme pub fn parse(uri: &str) -> Result { - let (scheme, rest) = uri - .split_once("://") - .ok_or(StoreURIParseError::InvalidFormat)?; - - match scheme { + let url = Url::parse(uri)?; + match url.scheme() { "ssh" => { - let (user, host) = rest - .split_once('@') - .map(|(u, h)| (Some(u.to_string()), h)) - .unwrap_or((None, rest)); - - if host.is_empty() { - return Err(StoreURIParseError::MissingHost); - } - - Ok(StoreURI::SSH(SSHStoreURI { - user, - host: host.to_string(), - })) + let host = url + .host_str() + .ok_or(StoreURIParseError::MissingHost)? + .to_string(); + let user = if !url.username().is_empty() { + Some(url.username().to_string()) + } else { + None + }; + let store_uri = SSHStoreURI { user, host }; + Ok(StoreURI::SSH(store_uri)) } // Add future schemes here - _ => Err(StoreURIParseError::UnsupportedScheme(scheme.to_string())), + scheme => Err(StoreURIParseError::UnsupportedScheme(scheme.to_string())), } } }