Skip to content

Commit

Permalink
Simplify code by processing everything in the same Regexp.
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickdappollonio committed Aug 27, 2024
1 parent 8606c0d commit e10ce3f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 61 deletions.
11 changes: 9 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,17 @@ fn run() -> Result<(), ApplicationError> {

// Create the directory if it does not exist
if !Path::new(&project_path).exists() {
eprintln!("\u{ea83} Destination directory does not exist. Creating...",);
eprintln!(
"\u{ea83} Destination directory for {}/{} does not exist. Creating...",
team, project
);
fs::create_dir_all(&project_path).map_err(ApplicationError::CantCreateTargetDir)?;
} else {
eprint!("\u{eb32} Destination directory already exists. Press <Enter> to confirm deletion or <Ctrl+C> to cancel...");
eprintln!(
"\u{eb32} Destination directory for {}/{} already exists.",
team, project
);
eprintln!("Press <Enter> to confirm deletion or <Ctrl+C> to cancel...");
let mut input = String::new();
io::stdin()
.read_line(&mut input)
Expand Down
67 changes: 8 additions & 59 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,11 @@ impl From<CantConvertSSHError> for ParseRepoError {
}

pub fn repository(repo_url: String) -> Result<(String, String, String), ParseRepoError> {
let prefixes = ["http://", "https://", "github.com/"];
if prefixes.iter().any(|prefix| repo_url.starts_with(prefix)) {
return parse_http_url(&repo_url).map_err(ParseRepoError::from);
}

if repo_url.contains('@') && repo_url.contains(':') {
return parse_ssh_url(&repo_url).map_err(ParseRepoError::from);
}

parse_user_repo(&repo_url).map_err(ParseRepoError::from)
parse_http_url(&repo_url).map_err(ParseRepoError::from)
}

#[derive(Debug)]
Expand All @@ -94,25 +89,6 @@ enum CantConvertSSHError {
CantFindProjectAndName(String),
}

fn parse_user_repo(location: &str) -> Result<(String, String, String), CantConvertError> {
let re = Regex::new(r"^(?<user>[a-zA-Z0-9-]+)/(?<repo>[\w\.-]+)$")
.map_err(CantConvertError::InvalidRegexp)?;

let caps = re
.captures(location)
.ok_or(CantConvertError::InvalidURL(location.to_owned()))?;
let user = caps
.name("user")
.ok_or(CantConvertError::MissingOrganization(location.to_owned()))?
.as_str();
let repo = caps
.name("repo")
.ok_or(CantConvertError::MissingProject(location.to_owned()))?
.as_str();

Ok(("github.com".to_owned(), user.to_string(), repo.to_string()))
}

fn parse_ssh_url(url: &str) -> Result<(String, String, String), CantConvertSSHError> {
let parts: Vec<&str> = url.splitn(2, '@').collect();

Expand All @@ -139,7 +115,7 @@ fn parse_ssh_url(url: &str) -> Result<(String, String, String), CantConvertSSHEr
}

fn parse_http_url(url: &str) -> Result<(String, String, String), CantConvertError> {
let re = Regex::new(r"^(https?://)?github\.com/(?<org>[^/]+)/(?<project>[^/]+).*$")
let re = Regex::new(r"^(https://)?(github\.com/)?(?<org>[a-zA-Z0-9-]+)/(?<repo>[\w\.-]+).*$")
.map_err(CantConvertError::InvalidRegexp)?;

let caps = re
Expand All @@ -150,7 +126,7 @@ fn parse_http_url(url: &str) -> Result<(String, String, String), CantConvertErro
.ok_or(CantConvertError::MissingOrganization(url.to_owned()))?
.as_str();
let project = caps
.name("project")
.name("repo")
.ok_or(CantConvertError::MissingProject(url.to_owned()))?
.as_str()
.trim_end_matches(".git");
Expand Down Expand Up @@ -218,38 +194,6 @@ mod tests {
}
}

#[test]
fn test_valid_user_repo() {
let cases = vec![
(
"patrickdappollonio/gc-rust",
("patrickdappollonio", "gc-rust"),
),
(
"patrickdappollonio/example",
("patrickdappollonio", "example"),
),
];

for (input, expected) in cases {
let (host, user, repo) = parse_user_repo(input).unwrap();
let (expected_user, expected_repo) = expected;
assert_eq!(host, "github.com".to_string());
assert_eq!(user, expected_user.to_string());
assert_eq!(repo, expected_repo.to_string());
}
}

#[test]
fn test_invalid_user_repo() {
let cases = vec!["", "patrickdappollonio", "patrickdappollonio/"];

for input in cases {
let result = parse_user_repo(input);
assert!(result.is_err());
}
}

#[test]
fn test_valid_http_conversor() {
let cases = vec![
Expand All @@ -269,6 +213,11 @@ mod tests {
false,
("github.com", "patrickdappollonio", "gc-rust"),
),
(
"patrickdappollonio/gc-rust",
false,
("github.com", "patrickdappollonio", "gc-rust"),
),
];

for (input, should_fail, expected) in cases {
Expand Down

0 comments on commit e10ce3f

Please sign in to comment.