From b218abebdf8a0aed73bfe6f61ab22e51a0f2f43c Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 1 Aug 2020 14:51:49 +0200 Subject: Move `github-suggestion-config` to `github-suggestion-cli` Going to use this for more cli-oriented code, so that the binaries can share more logic. --- Cargo.lock | 4 +- Cargo.toml | 4 +- github-suggestion-cli/Cargo.toml | 9 +++++ github-suggestion-cli/src/lib.rs | 75 +++++++++++++++++++++++++++++++++++++ github-suggestion-config/Cargo.toml | 9 ----- github-suggestion-config/src/lib.rs | 75 ------------------------------------- 6 files changed, 88 insertions(+), 88 deletions(-) create mode 100644 github-suggestion-cli/Cargo.toml create mode 100644 github-suggestion-cli/src/lib.rs delete mode 100644 github-suggestion-config/Cargo.toml delete mode 100644 github-suggestion-config/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index f4c3d6c..c8e5a96 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -264,7 +264,7 @@ version = "0.0.1" dependencies = [ "git2", "github-rs", - "github-suggestion-config", + "github-suggestion-cli", "regex", "serde", "serde_json", @@ -274,7 +274,7 @@ dependencies = [ ] [[package]] -name = "github-suggestion-config" +name = "github-suggestion-cli" version = "0.0.1" dependencies = [ "git2", diff --git a/Cargo.toml b/Cargo.toml index d5d3dd7..a5ea35b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,10 +13,10 @@ tempfile = "3.1.0" thiserror = "1.0.20" url = "2.1.1" -github-suggestion-config = { path = "github-suggestion-config" } +github-suggestion-cli = { path = "github-suggestion-cli" } [workspace] members = [ - "github-suggestion-config", + "github-suggestion-cli", ] diff --git a/github-suggestion-cli/Cargo.toml b/github-suggestion-cli/Cargo.toml new file mode 100644 index 0000000..0d07b7b --- /dev/null +++ b/github-suggestion-cli/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "github-suggestion-cli" +version = "0.0.1" +edition = "2018" + +[dependencies] +git2 = "0.13.8" +thiserror = "1.0.20" +url = "2.1.1" diff --git a/github-suggestion-cli/src/lib.rs b/github-suggestion-cli/src/lib.rs new file mode 100644 index 0000000..c0d97a2 --- /dev/null +++ b/github-suggestion-cli/src/lib.rs @@ -0,0 +1,75 @@ +use std::str::FromStr; + +use git2::Repository; +use thiserror::Error; +use url; +use url::Url; + + +#[derive(Debug, Error)] +pub enum Error { + #[error(transparent)] + Git(#[from] git2::Error), + + #[error(transparent)] + OwnerRepo(#[from] OwnerRepoError), + + #[error("Unable to find remote '{0}'")] + NoRemote(String), +} + +#[derive(Debug, Error)] +pub enum OwnerRepoError { + #[error("Unable to parse URL")] + Url(#[from] url::ParseError), + + #[error("URL has no path")] + NoPath, + + #[error("Unable to parse owner or repo")] + NoOwnerRepo, +} + + +#[derive(Debug)] +pub struct OwnerRepo { + pub owner: String, + pub repo: String, +} + +impl FromStr for OwnerRepo { + type Err = OwnerRepoError; + + fn from_str(s: &str) -> Result { + let url = Url::parse(s)?; + let path = url.path_segments() + .ok_or(OwnerRepoError::NoPath)? + .collect::>(); + + if path.len() < 2 { + return Err(OwnerRepoError::NoOwnerRepo); + } + + Ok(OwnerRepo { + owner: path[0].to_owned(), + repo: path[1].to_owned(), + }) + } +} + +pub fn identifier_for_remote( + remote_name: Option<&str>, +) -> Result { + let repo = Repository::open(".")?; + + let remote_name = match remote_name { + Some(r) => r, + None => "origin", + }; + + let remote = repo.find_remote(remote_name)?; + let url = remote.url() + .ok_or_else(|| Error::NoRemote(remote_name.to_owned()))?; + + Ok(url.parse()?) +} diff --git a/github-suggestion-config/Cargo.toml b/github-suggestion-config/Cargo.toml deleted file mode 100644 index 788d00d..0000000 --- a/github-suggestion-config/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "github-suggestion-config" -version = "0.0.1" -edition = "2018" - -[dependencies] -git2 = "0.13.8" -thiserror = "1.0.20" -url = "2.1.1" diff --git a/github-suggestion-config/src/lib.rs b/github-suggestion-config/src/lib.rs deleted file mode 100644 index c0d97a2..0000000 --- a/github-suggestion-config/src/lib.rs +++ /dev/null @@ -1,75 +0,0 @@ -use std::str::FromStr; - -use git2::Repository; -use thiserror::Error; -use url; -use url::Url; - - -#[derive(Debug, Error)] -pub enum Error { - #[error(transparent)] - Git(#[from] git2::Error), - - #[error(transparent)] - OwnerRepo(#[from] OwnerRepoError), - - #[error("Unable to find remote '{0}'")] - NoRemote(String), -} - -#[derive(Debug, Error)] -pub enum OwnerRepoError { - #[error("Unable to parse URL")] - Url(#[from] url::ParseError), - - #[error("URL has no path")] - NoPath, - - #[error("Unable to parse owner or repo")] - NoOwnerRepo, -} - - -#[derive(Debug)] -pub struct OwnerRepo { - pub owner: String, - pub repo: String, -} - -impl FromStr for OwnerRepo { - type Err = OwnerRepoError; - - fn from_str(s: &str) -> Result { - let url = Url::parse(s)?; - let path = url.path_segments() - .ok_or(OwnerRepoError::NoPath)? - .collect::>(); - - if path.len() < 2 { - return Err(OwnerRepoError::NoOwnerRepo); - } - - Ok(OwnerRepo { - owner: path[0].to_owned(), - repo: path[1].to_owned(), - }) - } -} - -pub fn identifier_for_remote( - remote_name: Option<&str>, -) -> Result { - let repo = Repository::open(".")?; - - let remote_name = match remote_name { - Some(r) => r, - None => "origin", - }; - - let remote = repo.find_remote(remote_name)?; - let url = remote.url() - .ok_or_else(|| Error::NoRemote(remote_name.to_owned()))?; - - Ok(url.parse()?) -} -- cgit v1.2.3