Skip to content

Commit

Permalink
ci: Parse --on URI using url crate
Browse files Browse the repository at this point in the history
  • Loading branch information
srid committed Jan 11, 2025
1 parent 7ef866f commit f983390
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions crates/nix_rs/src/store/uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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,
Expand All @@ -39,28 +47,23 @@ impl StoreURI {
///
/// Currently only supports `ssh` scheme
pub fn parse(uri: &str) -> Result<Self, StoreURIParseError> {
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())),
}
}
}
Expand Down

0 comments on commit f983390

Please sign in to comment.