aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib.rs
diff options
context:
space:
mode:
authorTeddy Wing2020-08-01 16:01:56 +0200
committerTeddy Wing2020-08-01 16:01:56 +0200
commit1453c4bdb4f179f1d27b0cbe95cfb33d111fa406 (patch)
treeaeff1e53d0b86172cea044281e6a24560bb79cdb /src/lib.rs
parent3f6225a5287ee10175c3f9a4f8443585368ac07a (diff)
downloadgit-suggestion-1453c4bdb4f179f1d27b0cbe95cfb33d111fa406.tar.bz2
lib.rs: Move code to `owner_repo.rs`
Need to add other code to the library.
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs76
1 files changed, 2 insertions, 74 deletions
diff --git a/src/lib.rs b/src/lib.rs
index c0d97a2..a08be2b 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,75 +1,3 @@
-use std::str::FromStr;
+#![warn(rust_2018_idioms)]
-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<Self, Self::Err> {
- let url = Url::parse(s)?;
- let path = url.path_segments()
- .ok_or(OwnerRepoError::NoPath)?
- .collect::<Vec<_>>();
-
- 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<OwnerRepo, Error> {
- 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()?)
-}
+pub(crate) mod owner_repo;