Skip to content

Commit

Permalink
[ref #152] refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Aug 9, 2021
1 parent e852399 commit 431dd86
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 51 deletions.
54 changes: 9 additions & 45 deletions git-ref/src/name.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,9 @@
use crate::{FullName, PartialName};
use bstr::{BStr, ByteSlice};
use std::{borrow::Cow, convert::TryFrom, path::Path};

mod error {
use bstr::BString;
use quick_error::quick_error;
use std::convert::Infallible;

quick_error! {
/// The error used in the [`PartialName`][super::PartialName]::try_from(…) implementations.
/// TODO: remove this error, turning it into the inner one. This is an unnecessary proxy
#[derive(Debug)]
#[allow(missing_docs)]
pub enum Error {
RefnameValidation{err: git_validate::reference::name::Error, path: BString} {
display("The reference name '{}' is invalid", path)
source(err)
}
}
}
use std::{borrow::Cow, convert::Infallible, convert::TryFrom, path::Path};

impl From<Infallible> for Error {
fn from(_: Infallible) -> Self {
unreachable!("this impl is needed to allow passing a known valid partial path as parameter")
}
}
}
pub use error::Error;
use std::convert::Infallible;
/// The error used in the [`PartialName`][super::PartialName]::try_from(…) implementations.
pub type Error = git_validate::reference::name::Error;

impl<'a> FullName<'a> {
/// Convert this name into the relative path identifying the reference location.
Expand Down Expand Up @@ -58,9 +34,7 @@ impl<'a> TryFrom<&'a BStr> for FullName<'a> {
type Error = Error;

fn try_from(v: &'a BStr) -> Result<Self, Self::Error> {
Ok(FullName(
git_validate::reference::name(v).map_err(|err| Error::RefnameValidation { err, path: v.into() })?,
))
Ok(FullName(git_validate::reference::name(v)?))
}
}

Expand All @@ -76,9 +50,7 @@ impl<'a> TryFrom<&'a BStr> for PartialName<'a> {
type Error = Error;

fn try_from(v: &'a BStr) -> Result<Self, Self::Error> {
Ok(PartialName(
git_validate::reference::name_partial(v).map_err(|err| Error::RefnameValidation { err, path: v.into() })?,
))
Ok(PartialName(git_validate::reference::name_partial(v)?))
}
}

Expand All @@ -87,9 +59,7 @@ impl<'a> TryFrom<&'a str> for FullName<'a> {

fn try_from(v: &'a str) -> Result<Self, Self::Error> {
let v = v.as_bytes().as_bstr();
Ok(FullName(
git_validate::reference::name(v).map_err(|err| Error::RefnameValidation { err, path: v.into() })?,
))
Ok(FullName(git_validate::reference::name(v)?))
}
}

Expand All @@ -98,9 +68,7 @@ impl<'a> TryFrom<&'a str> for PartialName<'a> {

fn try_from(v: &'a str) -> Result<Self, Self::Error> {
let v = v.as_bytes().as_bstr();
Ok(PartialName(
git_validate::reference::name_partial(v).map_err(|err| Error::RefnameValidation { err, path: v.into() })?,
))
Ok(PartialName(git_validate::reference::name_partial(v)?))
}
}

Expand All @@ -109,9 +77,7 @@ impl<'a> TryFrom<&'a String> for FullName<'a> {

fn try_from(v: &'a String) -> Result<Self, Self::Error> {
let v = v.as_bytes().as_bstr();
Ok(FullName(
git_validate::reference::name(v).map_err(|err| Error::RefnameValidation { err, path: v.into() })?,
))
Ok(FullName(git_validate::reference::name(v)?))
}
}

Expand All @@ -120,8 +86,6 @@ impl<'a> TryFrom<&'a String> for PartialName<'a> {

fn try_from(v: &'a String) -> Result<Self, Self::Error> {
let v = v.as_bytes().as_bstr();
Ok(PartialName(
git_validate::reference::name_partial(v).map_err(|err| Error::RefnameValidation { err, path: v.into() })?,
))
Ok(PartialName(git_validate::reference::name_partial(v)?))
}
}
9 changes: 4 additions & 5 deletions git-ref/tests/namespace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ mod expand {
fn empty_namespaces_are_not_allowed() {
assert!(matches!(
git_ref::namespace::expand("").expect_err("empty invalid"),
git_ref::namespace::expand::Error::RefnameValidation(git_ref::name::Error::RefnameValidation {
err: git_validate::refname::Error::Tag(git_validate::tag::name::Error::Empty),
..
})
))
git_ref::namespace::expand::Error::RefnameValidation(git_validate::refname::Error::Tag(
git_validate::tag::name::Error::Empty
))
));
}
}
2 changes: 1 addition & 1 deletion git-ref/tests/packed/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::convert::{TryFrom, TryInto};
fn a_lock_file_would_not_be_a_valid_partial_name() {
// doesn't really belong here but want to make sure refname validation works as expected.
let err = PartialName::try_from("heads/hello.lock").expect_err("this should fail");
assert_eq!(err.to_string(), "The reference name 'heads/hello.lock' is invalid");
assert_eq!(err.to_string(), "A reference must be a valid tag name as well");
}

#[test]
Expand Down
6 changes: 6 additions & 0 deletions git-validate/src/reference.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
///
pub mod name {
use quick_error::quick_error;
use std::convert::Infallible;

quick_error! {
/// The error used in [name()][super::name()] and [name_partial()][super::name_partial()]
Expand All @@ -24,7 +25,12 @@ pub mod name {
SingleDot {
display("Names must not be a single '.', but may contain it.")
}
}
}

impl From<Infallible> for Error {
fn from(_: Infallible) -> Self {
unreachable!("this impl is needed to allow passing a known valid partial path as parameter")
}
}
}
Expand Down

0 comments on commit 431dd86

Please sign in to comment.