Skip to content

Commit

Permalink
Shortened GitOid constructor names. (#92)
Browse files Browse the repository at this point in the history
This commit shortens constructor names to make code a little
easier to parse visually.

Signed-off-by: Andrew Lilley Brinker <[email protected]>
  • Loading branch information
alilleybrinker authored Feb 12, 2024
1 parent efd6038 commit 3325adb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 25 deletions.
10 changes: 5 additions & 5 deletions gitoid/src/ffi/gitoid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ macro_rules! generate_gitoid_ffi_for_hash {
let output = catch_panic(|| {
check_null(content, Error::ContentPtrIsNull)?;
let content = unsafe { from_raw_parts(content, content_len) };
let gitoid = GitOid::<$hash_ty, $object_ty>::new_from_bytes(content);
let gitoid = GitOid::<$hash_ty, $object_ty>::from_bytes(content);
let boxed = Box::new(gitoid);
Ok(Box::into_raw(boxed) as *const _)
});
Expand All @@ -149,7 +149,7 @@ macro_rules! generate_gitoid_ffi_for_hash {
let output = catch_panic(|| {
check_null(s, Error::StringPtrIsNull)?;
let s = unsafe { CStr::from_ptr(s) }.to_str()?;
let gitoid = GitOid::<$hash_ty, $object_ty>::new_from_str(s);
let gitoid = GitOid::<$hash_ty, $object_ty>::from_str(s);
let boxed = Box::new(gitoid);
Ok(Box::into_raw(boxed) as *const _)
});
Expand All @@ -172,7 +172,7 @@ macro_rules! generate_gitoid_ffi_for_hash {
check_null(s, Error::StringPtrIsNull)?;
let raw_url = unsafe { CStr::from_ptr(s) }.to_str()?;
let url = Url::parse(raw_url)?;
let gitoid = GitOid::<$hash_ty, $object_ty>::new_from_url(url)?;
let gitoid = GitOid::<$hash_ty, $object_ty>::from_url(url)?;
let boxed = Box::new(gitoid);
Ok(Box::into_raw(boxed) as *const _)
});
Expand All @@ -198,9 +198,9 @@ macro_rules! generate_gitoid_ffi_for_hash {

let gitoid = if should_buffer {
let reader = BufReader::new(file);
GitOid::<$hash_ty, $object_ty>::new_from_reader(reader)?
GitOid::<$hash_ty, $object_ty>::from_reader(reader)?
} else {
GitOid::<$hash_ty, $object_ty>::new_from_reader(file)?
GitOid::<$hash_ty, $object_ty>::from_reader(file)?
};

let boxed = Box::new(gitoid);
Expand Down
32 changes: 24 additions & 8 deletions gitoid/src/gitoid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::fmt::Result as FmtResult;
use std::io::Read;
use std::io::Seek;
use std::io::SeekFrom;
use std::str::FromStr;
use std::str::Split;
use std::{hash::Hasher, io::BufReader};
use url::Url;
Expand Down Expand Up @@ -53,15 +54,15 @@ where
//-------------------------------------------------------------------------------------------

/// Helper constructor for building a [`GitOid`] from a parsed hash.
fn new_from_hash(value: GenericArray<u8, H::OutputSize>) -> GitOid<H, O> {
fn from_hash(value: GenericArray<u8, H::OutputSize>) -> GitOid<H, O> {
GitOid {
_phantom: PhantomData,
value,
}
}

/// Create a new `GitOid` based on a slice of bytes.
pub fn new_from_bytes<B: AsRef<[u8]>>(content: B) -> GitOid<H, O> {
pub fn from_bytes<B: AsRef<[u8]>>(content: B) -> GitOid<H, O> {
fn inner<H, O>(content: &[u8]) -> GitOid<H, O>
where
H: HashAlgorithm,
Expand All @@ -81,22 +82,23 @@ where
}

/// Create a `GitOid` from a UTF-8 string slice.
pub fn new_from_str<S: AsRef<str>>(s: S) -> GitOid<H, O> {
#[allow(clippy::should_implement_trait)]
pub fn from_str<S: AsRef<str>>(s: S) -> GitOid<H, O> {
fn inner<H, O>(s: &str) -> GitOid<H, O>
where
H: HashAlgorithm,
O: ObjectType,
<H as OutputSizeUser>::OutputSize: ArrayLength<u8>,
GenericArray<u8, H::OutputSize>: Copy,
{
GitOid::new_from_bytes(s.as_bytes())
GitOid::from_bytes(s.as_bytes())
}

inner(s.as_ref())
}

/// Create a `GitOid` from a reader.
pub fn new_from_reader<R>(mut reader: R) -> Result<GitOid<H, O>>
pub fn from_reader<R>(mut reader: R) -> Result<GitOid<H, O>>
where
R: Read + Seek,
{
Expand All @@ -106,7 +108,7 @@ where
}

/// Construct a new `GitOid` from a `Url`.
pub fn new_from_url(url: Url) -> Result<GitOid<H, O>> {
pub fn from_url(url: Url) -> Result<GitOid<H, O>> {
url.try_into()
}

Expand Down Expand Up @@ -147,6 +149,20 @@ where
}
}

impl<H, O> FromStr for GitOid<H, O>
where
H: HashAlgorithm,
O: ObjectType,
<H as OutputSizeUser>::OutputSize: ArrayLength<u8>,
GenericArray<u8, H::OutputSize>: Copy,
{
type Err = Error;

fn from_str(s: &str) -> Result<GitOid<H, O>> {
Ok(GitOid::from_str(s))
}
}

impl<H, O> Clone for GitOid<H, O>
where
H: HashAlgorithm,
Expand Down Expand Up @@ -297,7 +313,7 @@ where
.and_then(|_| self.validate_object_type())
.and_then(|_| self.validate_hash_algorithm())
.and_then(|_| self.parse_hash())
.map(GitOid::new_from_hash)
.map(GitOid::from_hash)
}

fn validate_url_scheme(&self) -> Result<()> {
Expand Down Expand Up @@ -437,7 +453,7 @@ where
});
}

Ok(GitOid::new_from_hash(hash))
Ok(GitOid::from_hash(hash))
}

// Adapted from the Rust standard library's unstable implementation
Expand Down
24 changes: 12 additions & 12 deletions gitoid/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use url::Url;
#[test]
fn generate_sha1_gitoid_from_bytes() {
let input = b"hello world";
let result = GitOid::<Sha1, Blob>::new_from_bytes(input);
let result = GitOid::<Sha1, Blob>::from_bytes(input);

assert_eq!(result.as_hex(), "95d09f2b10159347eece71399a7e2e907ea3df4f");

Expand All @@ -21,7 +21,7 @@ fn generate_sha1_gitoid_from_bytes() {
#[test]
fn generate_sha1_gitoid_from_buffer() -> Result<()> {
let reader = BufReader::new(File::open("test/data/hello_world.txt")?);
let result = GitOid::<Sha1, Blob>::new_from_reader(reader)?;
let result = GitOid::<Sha1, Blob>::from_reader(reader)?;

assert_eq!(result.as_hex(), "95d09f2b10159347eece71399a7e2e907ea3df4f");

Expand All @@ -36,7 +36,7 @@ fn generate_sha1_gitoid_from_buffer() -> Result<()> {
#[test]
fn generate_sha256_gitoid_from_bytes() {
let input = b"hello world";
let result = GitOid::<Sha256, Blob>::new_from_bytes(input);
let result = GitOid::<Sha256, Blob>::from_bytes(input);

assert_eq!(
result.as_hex(),
Expand All @@ -52,7 +52,7 @@ fn generate_sha256_gitoid_from_bytes() {
#[test]
fn generate_sha256_gitoid_from_buffer() -> Result<()> {
let reader = BufReader::new(File::open("test/data/hello_world.txt")?);
let result = GitOid::<Sha256, Blob>::new_from_reader(reader)?;
let result = GitOid::<Sha256, Blob>::from_reader(reader)?;

assert_eq!(
result.as_hex(),
Expand All @@ -70,7 +70,7 @@ fn generate_sha256_gitoid_from_buffer() -> Result<()> {
#[test]
fn validate_uri() -> Result<()> {
let content = b"hello world";
let gitoid = GitOid::<Sha256, Blob>::new_from_bytes(content);
let gitoid = GitOid::<Sha256, Blob>::from_bytes(content);

assert_eq!(
gitoid.url().to_string(),
Expand All @@ -87,7 +87,7 @@ fn try_from_url_bad_scheme() {
)
.unwrap();

match GitOid::<Sha256, Blob>::new_from_url(url) {
match GitOid::<Sha256, Blob>::from_url(url) {
Ok(_) => panic!("gitoid parsing should fail"),
Err(e) => assert_eq!(e.to_string(), "invalid scheme in URL 'whatever'"),
}
Expand All @@ -97,7 +97,7 @@ fn try_from_url_bad_scheme() {
fn try_from_url_missing_object_type() {
let url = Url::parse("gitoid:").unwrap();

match GitOid::<Sha1, Blob>::new_from_url(url) {
match GitOid::<Sha1, Blob>::from_url(url) {
Ok(_) => panic!("gitoid parsing should fail"),
Err(e) => assert_eq!(e.to_string(), "missing object type in URL 'gitoid:'"),
}
Expand All @@ -107,7 +107,7 @@ fn try_from_url_missing_object_type() {
fn try_from_url_bad_object_type() {
let url = Url::parse("gitoid:whatever").unwrap();

match GitOid::<Sha1, Blob>::new_from_url(url) {
match GitOid::<Sha1, Blob>::from_url(url) {
Ok(_) => panic!("gitoid parsing should fail"),
Err(e) => assert_eq!(
e.to_string(),
Expand All @@ -120,7 +120,7 @@ fn try_from_url_bad_object_type() {
fn try_from_url_missing_hash_algorithm() {
let url = Url::parse("gitoid:blob:").unwrap();

match GitOid::<Sha256, Blob>::new_from_url(url) {
match GitOid::<Sha256, Blob>::from_url(url) {
Ok(_) => panic!("gitoid parsing should fail"),
Err(e) => assert_eq!(
e.to_string(),
Expand All @@ -133,7 +133,7 @@ fn try_from_url_missing_hash_algorithm() {
fn try_from_url_bad_hash_algorithm() {
let url = Url::parse("gitoid:blob:sha10000").unwrap();

match GitOid::<Sha1, Blob>::new_from_url(url) {
match GitOid::<Sha1, Blob>::from_url(url) {
Ok(_) => panic!("gitoid parsing should fail"),
Err(e) => assert_eq!(
e.to_string(),
Expand All @@ -146,7 +146,7 @@ fn try_from_url_bad_hash_algorithm() {
fn try_from_url_missing_hash() {
let url = Url::parse("gitoid:blob:sha256:").unwrap();

match GitOid::<Sha256, Blob>::new_from_url(url) {
match GitOid::<Sha256, Blob>::from_url(url) {
Ok(_) => panic!("gitoid parsing should fail"),
Err(e) => assert_eq!(e.to_string(), "missing hash in URL 'gitoid:blob:sha256:'"),
}
Expand All @@ -158,7 +158,7 @@ fn try_url_roundtrip() {
"gitoid:blob:sha256:fee53a18d32820613c0527aa79be5cb30173c823a9b448fa4817767cc84c6f03",
)
.unwrap();
let gitoid = GitOid::<Sha256, Blob>::new_from_url(url.clone()).unwrap();
let gitoid = GitOid::<Sha256, Blob>::from_url(url.clone()).unwrap();
let output = gitoid.url();

eprintln!("{}", url);
Expand Down

0 comments on commit 3325adb

Please sign in to comment.