Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: simplify GitOid crate substantially. #108

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,6 @@ jobs:
- name: Linting
run: cargo clippy --verbose

ffi-test-linux:
name: FFI Test (Ubuntu)
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@nightly
- name: FFI Test
run: cd gitoid && ./ctest.sh

ffi-test-windows:
name: FFI Test (Windows)
runs-on: windows-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@nightly
- name: FFI Test
run: cd gitoid && ./ctest.sh

conventional-commits:
name: Conventional Commits
runs-on: ubuntu-latest
Expand Down
39 changes: 0 additions & 39 deletions gitoid/cbindgen.toml

This file was deleted.

38 changes: 0 additions & 38 deletions gitoid/ctest.sh

This file was deleted.

9 changes: 1 addition & 8 deletions gitoid/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ pub(crate) type Result<T> = StdResult<T, Error>;
/// An error arising during `GitOid` construction or use.
#[derive(Debug)]
pub enum Error {
/// The expected and actual length of the data being read didn't
/// match, indicating something has likely gone wrong.
BadLength { expected: usize, actual: usize },
/// Tried to construct a `GitOid` from a `Url` with a scheme besides `gitoid`.
InvalidScheme(Url),
/// Tried to construct a `GitOid` from a `Url` without an `ObjectType` in it.
Expand Down Expand Up @@ -46,9 +43,6 @@ pub enum Error {
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match self {
Error::BadLength { expected, actual } => {
write!(f, "expected length {}, actual length {}", expected, actual)
}
Error::InvalidScheme(url) => write!(f, "invalid scheme in URL '{}'", url.scheme()),
Error::MissingObjectType(url) => write!(f, "missing object type in URL '{}'", url),
Error::MissingHashAlgorithm(url) => {
Expand Down Expand Up @@ -83,8 +77,7 @@ impl Display for Error {
impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
Error::BadLength { .. }
| Error::InvalidScheme(_)
Error::InvalidScheme(_)
| Error::MissingObjectType(_)
| Error::MissingHashAlgorithm(_)
| Error::MissingHash(_)
Expand Down
43 changes: 9 additions & 34 deletions gitoid/src/gitoid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ use crate::Error;
use crate::HashAlgorithm;
use crate::ObjectType;
use crate::Result;
use core::cmp::Ordering;
use core::fmt::Debug;
use core::fmt::Display;
use core::fmt::Formatter;
use core::fmt::Result as FmtResult;
use core::hash::Hash;
use core::hash::Hasher;
use core::marker::PhantomData;
use core::ops::Not as _;
use core::str::FromStr;
use core::str::Split;
use digest::OutputSizeUser;
use format_bytes::format_bytes;
use generic_array::sequence::GenericSequence;
use generic_array::ArrayLength;
use generic_array::GenericArray;
use std::cmp::Ordering;
use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::Result as FmtResult;
use std::hash::Hasher;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Read;
use std::io::Seek;
use std::io::SeekFrom;
use std::str::FromStr;
use std::str::Split;
use url::Url;

/// A struct that computes [gitoids][g] based on the selected algorithm
Expand All @@ -36,7 +36,6 @@ where
H: HashAlgorithm,
O: ObjectType,
H::OutputSize: ArrayLength<u8>,
GenericArray<u8, H::OutputSize>: Copy,
{
#[doc(hidden)]
_phantom: PhantomData<O>,
Expand All @@ -53,12 +52,7 @@ where
H: HashAlgorithm,
O: ObjectType,
H::OutputSize: ArrayLength<u8>,
GenericArray<u8, H::OutputSize>: Copy,
{
//===========================================================================================
// Constructors
//-------------------------------------------------------------------------------------------

/// Helper constructor for building a [`GitOid`] from a parsed hash.
///
/// Use this so you don't have to care about filling in the phantom data.
Expand All @@ -76,7 +70,6 @@ where
H: HashAlgorithm,
O: ObjectType,
H::OutputSize: ArrayLength<u8>,
GenericArray<u8, H::OutputSize>: Copy,
{
// PANIC SAFETY: We're reading from an in-memory buffer, so no IO errors can arise.
gitoid_from_buffer(H::new(), content, content.len()).unwrap()
Expand All @@ -93,7 +86,6 @@ where
H: HashAlgorithm,
O: ObjectType,
H::OutputSize: ArrayLength<u8>,
GenericArray<u8, H::OutputSize>: Copy,
{
GitOid::from_bytes(s.as_bytes())
}
Expand All @@ -120,10 +112,6 @@ where
GitOid::try_from(url)
}

//===========================================================================================
// Getters
//-------------------------------------------------------------------------------------------

/// Get a URL for the current `GitOid`.
pub fn url(&self) -> Url {
// PANIC SAFETY: We know that this is a valid URL.
Expand Down Expand Up @@ -168,7 +156,6 @@ where
H: HashAlgorithm,
O: ObjectType,
H::OutputSize: ArrayLength<u8>,
GenericArray<u8, H::OutputSize>: Copy,
{
type Err = Error;

Expand Down Expand Up @@ -203,7 +190,6 @@ where
H: HashAlgorithm,
O: ObjectType,
H::OutputSize: ArrayLength<u8>,
GenericArray<u8, H::OutputSize>: Copy,
{
fn eq(&self, other: &GitOid<H, O>) -> bool {
self.value == other.value
Expand All @@ -215,7 +201,6 @@ where
H: HashAlgorithm,
O: ObjectType,
H::OutputSize: ArrayLength<u8>,
GenericArray<u8, H::OutputSize>: Copy,
{
}

Expand All @@ -224,7 +209,6 @@ where
H: HashAlgorithm,
O: ObjectType,
H::OutputSize: ArrayLength<u8>,
GenericArray<u8, H::OutputSize>: Copy,
{
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
Expand All @@ -236,7 +220,6 @@ where
H: HashAlgorithm,
O: ObjectType,
H::OutputSize: ArrayLength<u8>,
GenericArray<u8, H::OutputSize>: Copy,
{
fn cmp(&self, other: &Self) -> Ordering {
self.value.cmp(&other.value)
Expand All @@ -248,7 +231,6 @@ where
H: HashAlgorithm,
O: ObjectType,
H::OutputSize: ArrayLength<u8>,
GenericArray<u8, H::OutputSize>: Copy,
{
fn hash<H2>(&self, state: &mut H2)
where
Expand All @@ -263,7 +245,6 @@ where
H: HashAlgorithm,
O: ObjectType,
H::OutputSize: ArrayLength<u8>,
GenericArray<u8, H::OutputSize>: Copy,
{
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
f.debug_struct("GitOid")
Expand All @@ -277,7 +258,6 @@ where
H: HashAlgorithm,
O: ObjectType,
H::OutputSize: ArrayLength<u8>,
GenericArray<u8, H::OutputSize>: Copy,
{
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(f, "{}:{}", H::NAME, self.as_hex())
Expand All @@ -289,7 +269,6 @@ where
H: HashAlgorithm,
O: ObjectType,
H::OutputSize: ArrayLength<u8>,
GenericArray<u8, H::OutputSize>: Copy,
{
url: &'u Url,

Expand All @@ -311,7 +290,6 @@ where
H: HashAlgorithm,
O: ObjectType,
H::OutputSize: ArrayLength<u8>,
GenericArray<u8, H::OutputSize>: Copy,
{
fn new(url: &'u Url) -> GitOidUrlParser<'u, H, O> {
GitOidUrlParser {
Expand Down Expand Up @@ -400,7 +378,6 @@ where
H: HashAlgorithm,
O: ObjectType,
H::OutputSize: ArrayLength<u8>,
GenericArray<u8, H::OutputSize>: Copy,
{
type Error = Error;

Expand Down Expand Up @@ -428,16 +405,15 @@ where
H: HashAlgorithm,
O: ObjectType,
H::OutputSize: ArrayLength<u8>,
GenericArray<u8, H::OutputSize>: Copy,
R: Read,
{
let expected_hash_length = <H as OutputSizeUser>::output_size();
let (hash, amount_read) = hash_from_buffer::<H, O, R>(digester, reader, expected_read_length)?;

if amount_read != expected_read_length {
return Err(Error::BadLength {
return Err(Error::UnexpectedHashLength {
expected: expected_read_length,
actual: amount_read,
observed: amount_read,
});
}

Expand Down Expand Up @@ -494,7 +470,6 @@ where
H: HashAlgorithm,
O: ObjectType,
H::OutputSize: ArrayLength<u8>,
GenericArray<u8, H::OutputSize>: Copy,
R: Read,
{
digester.update(format_bytes!(
Expand Down
2 changes: 1 addition & 1 deletion gitoid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
//! [omnibor]: https://omnibor.io

mod error;
pub mod ffi;
//pub mod ffi;
mod gitoid;
mod hash_algorithm;
mod object_type;
Expand Down
6 changes: 3 additions & 3 deletions gitoid/src/object_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use crate::sealed::Sealed;
use crate::Error;
#[cfg(doc)]
use crate::GitOid;
use core::fmt::Display;
use core::fmt::Formatter;
use core::fmt::Result as FmtResult;
use core::str::FromStr;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;

/// Object types usable to construct a [`GitOid`]
pub trait ObjectType: Display + FromStr + Copy + Clone + Sealed {
Expand Down
5 changes: 2 additions & 3 deletions gitoid/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use super::hash::*;
use super::object::*;
use super::*;
use std::fs::File;
use std::io::BufReader;
use url::Url;

#[test]
Expand All @@ -20,7 +19,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 reader = File::open("test/data/hello_world.txt")?;
let result = GitOid::<Sha1, Blob>::from_reader(reader)?;

assert_eq!(result.as_hex(), "95d09f2b10159347eece71399a7e2e907ea3df4f");
Expand Down Expand Up @@ -51,7 +50,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 reader = File::open("test/data/hello_world.txt")?;
let result = GitOid::<Sha256, Blob>::from_reader(reader)?;

assert_eq!(
Expand Down
2 changes: 0 additions & 2 deletions gitoid/test/.gitignore

This file was deleted.

Loading