Skip to content

Commit

Permalink
Concretely type the iterable diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemo157 authored and Joshua Nelson committed Aug 14, 2020
1 parent 9df57ee commit 4696a0f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 59 deletions.
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! [Docs.rs](https://docs.rs) (formerly cratesfyi) is an open source project to host
//! documentation of crates for the Rust Programming Language.
#![allow(clippy::cognitive_complexity)]
#![feature(type_alias_impl_trait)]

pub use self::build_queue::BuildQueue;

Expand Down
102 changes: 44 additions & 58 deletions src/utils/consistency/diff.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
use super::data::{Crate, CrateName, Data, Release, Version};
use std::{collections::BTreeMap, fmt::Debug};
use std::{
cmp::Ordering,
collections::{btree_map::IntoIter, BTreeMap},
fmt::Debug,
iter::Peekable,
};

#[derive(Debug)]
pub(crate) struct DataDiff {
pub(crate) crates: CratesDiff,
pub(crate) crates: DiffMap<CrateName, Crate>,
}

pub(crate) type CratesDiff = impl Iterator<Item = Diff<CrateName, Crate>> + Debug;

#[derive(Debug)]
pub(crate) struct CrateDiff {
pub(crate) releases: ReleasesDiff,
pub(crate) releases: DiffMap<Version, Release>,
}

pub(crate) type ReleasesDiff = impl Iterator<Item = Diff<Version, Release>> + Debug;

#[derive(Debug)]
pub(crate) struct ReleaseDiff {}

Expand All @@ -30,69 +31,60 @@ pub(crate) trait Diffable {
fn diff(self, other: Self) -> Self::Diff;
}

fn diff_map<Key: Ord + Debug, Value: Diffable + Debug>(
left: BTreeMap<Key, Value>,
right: BTreeMap<Key, Value>,
) -> impl Iterator<Item = Diff<Key, Value>> + Debug {
use std::{cmp::Ordering, collections::btree_map::IntoIter, iter::Peekable};
#[derive(Debug)]
pub(crate) struct DiffMap<Key, Value> {
left: Peekable<std::collections::btree_map::IntoIter<Key, Value>>,
right: Peekable<IntoIter<Key, Value>>,
}

#[derive(Debug)]
struct DiffMap<Key, Value> {
left: Peekable<std::collections::btree_map::IntoIter<Key, Value>>,
right: Peekable<IntoIter<Key, Value>>,
impl<Key, Value> DiffMap<Key, Value> {
fn new(left: BTreeMap<Key, Value>, right: BTreeMap<Key, Value>) -> Self {
Self {
left: left.into_iter().peekable(),
right: right.into_iter().peekable(),
}
}
}

impl<Key: Ord, Value: Diffable> Iterator for DiffMap<Key, Value> {
type Item = Diff<Key, Value>;

impl<Key: Ord, Value: Diffable> Iterator for DiffMap<Key, Value> {
type Item = Diff<Key, Value>;

fn next(&mut self) -> Option<Self::Item> {
match (self.left.peek(), self.right.peek()) {
(Some((left, _)), Some((right, _))) => match left.cmp(right) {
Ordering::Less => {
let (key, value) = self.left.next().unwrap();
Some(Diff::Left(key, value))
}
Ordering::Equal => {
let (key, left) = self.left.next().unwrap();
let (_, right) = self.right.next().unwrap();
Some(Diff::Both(key, left.diff(right)))
}
Ordering::Greater => {
let (key, value) = self.right.next().unwrap();
Some(Diff::Right(key, value))
}
},
(Some((_, _)), None) => {
fn next(&mut self) -> Option<Self::Item> {
match (self.left.peek(), self.right.peek()) {
(Some((left, _)), Some((right, _))) => match left.cmp(right) {
Ordering::Less => {
let (key, value) = self.left.next().unwrap();
Some(Diff::Left(key, value))
}
(None, Some((_, _))) => {
Ordering::Equal => {
let (key, left) = self.left.next().unwrap();
let (_, right) = self.right.next().unwrap();
Some(Diff::Both(key, left.diff(right)))
}
Ordering::Greater => {
let (key, value) = self.right.next().unwrap();
Some(Diff::Right(key, value))
}
(None, None) => None,
},
(Some((_, _)), None) => {
let (key, value) = self.left.next().unwrap();
Some(Diff::Left(key, value))
}
(None, Some((_, _))) => {
let (key, value) = self.right.next().unwrap();
Some(Diff::Right(key, value))
}
(None, None) => None,
}
}

DiffMap {
left: left.into_iter().peekable(),
right: right.into_iter().peekable(),
}
}

impl Diffable for Data {
type Diff = DataDiff;

fn diff(self, other: Self) -> Self::Diff {
fn diff_crates(
left: BTreeMap<CrateName, Crate>,
right: BTreeMap<CrateName, Crate>,
) -> CratesDiff {
diff_map(left, right)
}
DataDiff {
crates: diff_crates(self.crates, other.crates),
crates: DiffMap::new(self.crates, other.crates),
}
}
}
Expand All @@ -101,14 +93,8 @@ impl Diffable for Crate {
type Diff = CrateDiff;

fn diff(self, other: Self) -> Self::Diff {
fn diff_releases(
left: BTreeMap<Version, Release>,
right: BTreeMap<Version, Release>,
) -> ReleasesDiff {
diff_map(left, right)
}
CrateDiff {
releases: diff_releases(self.releases, other.releases),
releases: DiffMap::new(self.releases, other.releases),
}
}
}
Expand Down

0 comments on commit 4696a0f

Please sign in to comment.