Skip to content

Commit

Permalink
Merge branch 'main' of github.com:omnibor/omnibor-rs
Browse files Browse the repository at this point in the history
  • Loading branch information
alilleybrinker committed Dec 15, 2023
2 parents eeb5515 + 48ce1dc commit 8d88434
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 142 deletions.
2 changes: 1 addition & 1 deletion gitoid/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# It is intended to be easy for newcomers to the project to understand.

# Variables for cbindgen.
CBINDGEN_CONFIG="../cbindgen.toml"
CBINDGEN_CONFIG="./cbindgen.toml"

# Variables for the header file.
INCLUDE_DIR="./include"
Expand Down
24 changes: 2 additions & 22 deletions cbindgen.toml → gitoid/cbindgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
language = "C"

# Wrap generated file in an include guard.
include_guard = "gitbom_h"
include_guard = "gitoid_h"

# Note the version of cbindgen used to generate the header.
include_version = true
Expand All @@ -20,7 +20,7 @@ style = "both"
header = """
/**
* @file
* @brief "GitBom"
* @brief "GitOID"
*/
"""

Expand All @@ -40,23 +40,3 @@ autogen_warning = "/* Warning, this file is autogenerated by cbindgen. Don't mod

# Prefix enum variants with the name of the overall type.
prefix_with_name = true


#==============================================================================
# Parsing Rules
#------------------------------------------------------------------------------

[parse]

# Parse dependencies.
#parse_deps = true
#include = []

#==============================================================================
# Macro Expansion Rules
#------------------------------------------------------------------------------

#[parse.expand]

# Expand macros for `gitoid`.
#crates = ["gitoid"]
120 changes: 1 addition & 119 deletions omnibor/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,120 +1,2 @@
use gitoid::GitOid;
use im::{HashSet, Vector};

/// A [persistent][wiki] collection of [git oids][git_scm].
///
/// Why persistent? While Rust and the borrow checker is great about ownership and
/// mutation, always knowing that a Ref will not change if passed as a parameter
/// to a function eliminates a class of errors.
///
/// [wiki]: https://en.wikipedia.org/wiki/Persistent_data_structure
/// [git_scm]: https://git-scm.com/book/en/v2/Git-Internals-Git-Objects
#[derive(Clone, PartialOrd, Eq, Ord, Debug, Hash, PartialEq)]
pub struct OmniBor {
git_oids: HashSet<GitOid>,
}

impl FromIterator<GitOid> for OmniBor {
/// Create an OmniBor from many GitOids
fn from_iter<T>(gitoids: T) -> Self
where
T: IntoIterator<Item = GitOid>,
{
let me = OmniBor::new();
me.add_many(gitoids)
}
}

impl OmniBor {
/// Create a new instance
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self {
git_oids: HashSet::new(),
}
}

/// Create a OmniBor from many GitOids
pub fn new_from_iterator<I>(gitoids: I) -> Self
where
I: IntoIterator<Item = GitOid>,
{
let me = OmniBor::new();
me.add_many(gitoids)
}

/// Add a `GitOid` hash to the `OmniBor`.
///
/// Note that this creates a new persistent data structure under the hood.
pub fn add(&self, gitoid: GitOid) -> Self {
self.add_many([gitoid])
}

/// Append many `GitOid`s and return a new `OmniBor`
pub fn add_many<I>(&self, gitoids: I) -> Self
where
I: IntoIterator<Item = GitOid>,
{
let mut updated = self.git_oids.clone(); // im::HashSet has O(1) cloning
for gitoid in gitoids {
updated = updated.update(gitoid);
}
Self { git_oids: updated }
}

/// Return the `Vector` of `GitOid`s.
pub fn get_oids(&self) -> HashSet<GitOid> {
self.git_oids.clone() // im::HashSet as O(1) cloning.
}

/// Get a sorted `Vector` of `GitOid`s.
///
/// In some cases, getting a sorted `Vector` of oids is desirable.
/// This function (cost O(n log n)) returns a `Vector` of sorted oids
pub fn get_sorted_oids(&self) -> Vector<GitOid> {
let mut ret = self.git_oids.clone().into_iter().collect::<Vector<_>>();
ret.sort();
ret
}
}

#[cfg(test)]
mod tests {
use gitoid::{GitOid, HashAlgorithm, ObjectType::Blob};
use im::vector;

use super::*;

#[test]
fn test_add() {
let oid = GitOid::new_from_str(HashAlgorithm::Sha256, Blob, "Hello");
assert_eq!(OmniBor::new().add(oid).get_sorted_oids(), vector![oid])
}

#[test]
fn test_add_many() {
let mut oids: Vector<GitOid> = vec!["eee", "Hello", "Cat", "Dog"]
.into_iter()
.map(|s| GitOid::new_from_str(HashAlgorithm::Sha256, Blob, s))
.collect();

let da_bom = OmniBor::new().add_many(oids.clone());
oids.sort();
assert_eq!(da_bom.get_sorted_oids(), oids);
}

#[test]
fn test_add_gitoid_to_gitbom() {
let input = "hello world".as_bytes();

let generated_gitoid = GitOid::new_from_bytes(HashAlgorithm::Sha256, Blob, input);

let new_gitbom = OmniBor::new();
let new_gitbom = new_gitbom.add(generated_gitoid);

assert_eq!(
"fee53a18d32820613c0527aa79be5cb30173c823a9b448fa4817767cc84c6f03",
new_gitbom.get_sorted_oids()[0].hash().as_hex()
)
}
}
// TODO(abrinker): Complete first draft of the rewrite of this crate.

0 comments on commit 8d88434

Please sign in to comment.