Skip to content

Commit

Permalink
feat!: ues u32 insted of NumberVersion (#180)
Browse files Browse the repository at this point in the history
* feat: ues u32 insted of NumberVersion

* clean up
  • Loading branch information
Eh2406 authored Mar 20, 2024
1 parent 0d80e9d commit c4d209a
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 154 deletions.
6 changes: 3 additions & 3 deletions benches/large_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use self::criterion::*;
use pubgrub::package::Package;
use pubgrub::range::Range;
use pubgrub::solver::{resolve, OfflineDependencyProvider};
use pubgrub::version::{NumberVersion, SemanticVersion};
use pubgrub::version::SemanticVersion;
use pubgrub::version_set::VersionSet;
use serde::de::Deserialize;

Expand Down Expand Up @@ -36,9 +36,9 @@ fn bench_nested(c: &mut Criterion) {
let case = case.unwrap().path();
let name = case.file_name().unwrap().to_string_lossy();
let data = std::fs::read_to_string(&case).unwrap();
if name.ends_with("u16_NumberVersion.ron") {
if name.ends_with("u16_NumberVersion.ron") || name.ends_with("u16_u32.ron") {
group.bench_function(name, |b| {
bench::<u16, Range<NumberVersion>>(b, &data);
bench::<u16, Range<u32>>(b, &data);
});
} else if name.ends_with("str_SemanticVersion.ron") {
group.bench_function(name, |b| {
Expand Down
7 changes: 3 additions & 4 deletions examples/caching_dependency_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ use std::cell::RefCell;
use pubgrub::package::Package;
use pubgrub::range::Range;
use pubgrub::solver::{resolve, Dependencies, DependencyProvider, OfflineDependencyProvider};
use pubgrub::version::NumberVersion;
use pubgrub::version_set::VersionSet;

type NumVS = Range<NumberVersion>;
type NumVS = Range<u32>;

// An example implementing caching dependency provider that will
// store queried dependencies in memory and check them before querying more from remote.
Expand Down Expand Up @@ -77,11 +76,11 @@ fn main() {
let mut remote_dependencies_provider = OfflineDependencyProvider::<&str, NumVS>::new();

// Add dependencies as needed. Here only root package is added.
remote_dependencies_provider.add_dependencies("root", 1, Vec::new());
remote_dependencies_provider.add_dependencies("root", 1u32, Vec::new());

let caching_dependencies_provider =
CachingDependencyProvider::new(remote_dependencies_provider);

let solution = resolve(&caching_dependencies_provider, "root", 1);
let solution = resolve(&caching_dependencies_provider, "root", 1u32);
println!("Solution: {:?}", solution);
}
13 changes: 6 additions & 7 deletions examples/doc_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

use pubgrub::range::Range;
use pubgrub::solver::{resolve, OfflineDependencyProvider};
use pubgrub::version::NumberVersion;

type NumVS = Range<NumberVersion>;
type NumVS = Range<u32>;

// `root` depends on `menu` and `icons`
// `menu` depends on `dropdown`
Expand All @@ -14,13 +13,13 @@ type NumVS = Range<NumberVersion>;
fn main() {
let mut dependency_provider = OfflineDependencyProvider::<&str, NumVS>::new();
dependency_provider.add_dependencies(
"root", 1, [("menu", Range::full()), ("icons", Range::full())],
"root", 1u32, [("menu", Range::full()), ("icons", Range::full())],
);
dependency_provider.add_dependencies("menu", 1, [("dropdown", Range::full())]);
dependency_provider.add_dependencies("dropdown", 1, [("icons", Range::full())]);
dependency_provider.add_dependencies("icons", 1, []);
dependency_provider.add_dependencies("menu", 1u32, [("dropdown", Range::full())]);
dependency_provider.add_dependencies("dropdown", 1u32, [("icons", Range::full())]);
dependency_provider.add_dependencies("icons", 1u32, []);

// Run the algorithm.
let solution = resolve(&dependency_provider, "root", 1);
let solution = resolve(&dependency_provider, "root", 1u32);
println!("Solution: {:?}", solution);
}
36 changes: 14 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,18 @@
//! # Package and Version traits
//!
//! All the code in this crate is manipulating packages and versions, and for this to work
//! we defined a [Package](package::Package) and [Version](version::Version) traits
//! that are used as bounds on most of the exposed types and functions.
//! we defined a [Package](package::Package) trait
//! that is used as bounds on most of the exposed types and functions.
//!
//! Package identifiers needs to implement our [Package](package::Package) trait,
//! which is automatic if the type already implements
//! [Clone] + [Eq] + [Hash] + [Debug] + [Display](std::fmt::Display).
//! So things like [String] will work out of the box.
//!
//! Our [Version](version::Version) trait requires
//! [Clone] + [Ord] + [Debug] + [Display](std::fmt::Display)
//! and also the definition of two methods,
//! [lowest() -> Self](version::Version::lowest) which returns the lowest version existing,
//! and [bump(&self) -> Self](version::Version::bump) which returns the next smallest version
//! strictly higher than the current one.
//! For convenience, this library already provides
//! two implementations of [Version](version::Version).
//! The first one is [NumberVersion](version::NumberVersion), basically a newtype for [u32].
//! The second one is [SemanticVersion](version::NumberVersion)
//! TODO! This is all wrong. Need to talk about VS, not Version.
//! Our Version trait requires
//! [Clone] + [Ord] + [Debug] + [Display](std::fmt::Display).
//! For convenience, this library provides [SemanticVersion](version::SemanticVersion)
//! that implements semantic versioning rules.
//!
//! # Basic example
Expand All @@ -47,22 +41,21 @@
//! We can model that scenario with this library as follows
//! ```
//! # use pubgrub::solver::{OfflineDependencyProvider, resolve};
//! # use pubgrub::version::NumberVersion;
//! # use pubgrub::range::Range;
//!
//! type NumVS = Range<NumberVersion>;
//! type NumVS = Range<u32>;
//!
//! let mut dependency_provider = OfflineDependencyProvider::<&str, NumVS>::new();
//!
//! dependency_provider.add_dependencies(
//! "root", 1, [("menu", Range::full()), ("icons", Range::full())],
//! "root", 1u32, [("menu", Range::full()), ("icons", Range::full())],
//! );
//! dependency_provider.add_dependencies("menu", 1, [("dropdown", Range::full())]);
//! dependency_provider.add_dependencies("dropdown", 1, [("icons", Range::full())]);
//! dependency_provider.add_dependencies("icons", 1, []);
//! dependency_provider.add_dependencies("menu", 1u32, [("dropdown", Range::full())]);
//! dependency_provider.add_dependencies("dropdown", 1u32, [("icons", Range::full())]);
//! dependency_provider.add_dependencies("icons", 1u32, []);
//!
//! // Run the algorithm.
//! let solution = resolve(&dependency_provider, "root", 1).unwrap();
//! let solution = resolve(&dependency_provider, "root", 1u32).unwrap();
//! ```
//!
//! # DependencyProvider trait
Expand Down Expand Up @@ -183,14 +176,13 @@
//! # use pubgrub::solver::{resolve, OfflineDependencyProvider};
//! # use pubgrub::report::{DefaultStringReporter, Reporter};
//! # use pubgrub::error::PubGrubError;
//! # use pubgrub::version::NumberVersion;
//! # use pubgrub::range::Range;
//! #
//! # type NumVS = Range<NumberVersion>;
//! # type NumVS = Range<u32>;
//! #
//! # let dependency_provider = OfflineDependencyProvider::<&str, NumVS>::new();
//! # let root_package = "root";
//! # let root_version = 1;
//! # let root_version = 1u32;
//! #
//! match resolve(&dependency_provider, root_package, root_version) {
//! Ok(solution) => println!("{:?}", solution),
Expand Down
5 changes: 2 additions & 3 deletions src/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,15 @@
//! ```
//! # use std::convert::Infallible;
//! # use pubgrub::solver::{resolve, OfflineDependencyProvider};
//! # use pubgrub::version::NumberVersion;
//! # use pubgrub::error::PubGrubError;
//! # use pubgrub::range::Range;
//! #
//! # type NumVS = Range<NumberVersion>;
//! # type NumVS = Range<u32>;
//! #
//! # fn try_main() -> Result<(), PubGrubError<&'static str, NumVS, Infallible>> {
//! # let dependency_provider = OfflineDependencyProvider::<&str, NumVS>::new();
//! # let package = "root";
//! # let version = 1;
//! # let version = 1u32;
//! let solution = resolve(&dependency_provider, package, version)?;
//! # Ok(())
//! # }
Expand Down
68 changes: 0 additions & 68 deletions src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ use std::fmt::{self, Debug, Display};
use std::str::FromStr;
use thiserror::Error;

/// Versions have a minimal version (a "0" version)
/// and are ordered such that every version has a next one.
pub trait Version: Clone + Ord + Debug + Display {
/// Returns the lowest version.
fn lowest() -> Self;
/// Returns the next version, the smallest strictly higher version.
fn bump(&self) -> Self;
}

/// Type for semantic versions: major.minor.patch.
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct SemanticVersion {
Expand Down Expand Up @@ -228,62 +219,3 @@ impl Display for SemanticVersion {
write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
}
}

// Implement Version for SemanticVersion.
impl Version for SemanticVersion {
fn lowest() -> Self {
Self::zero()
}
fn bump(&self) -> Self {
self.bump_patch()
}
}

/// Simplest versions possible, just a positive number.
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize,))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct NumberVersion(pub u32);

// Convert an usize into a version.
impl From<u32> for NumberVersion {
fn from(v: u32) -> Self {
Self(v)
}
}

// Convert an &usize into a version.
impl From<&u32> for NumberVersion {
fn from(v: &u32) -> Self {
Self(*v)
}
}

// Convert an &version into a version.
impl From<&NumberVersion> for NumberVersion {
fn from(v: &NumberVersion) -> Self {
*v
}
}

// Convert a version into an usize.
impl From<NumberVersion> for u32 {
fn from(version: NumberVersion) -> Self {
version.0
}
}

impl Display for NumberVersion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}

impl Version for NumberVersion {
fn lowest() -> Self {
Self(0)
}
fn bump(&self) -> Self {
Self(self.0 + 1)
}
}
30 changes: 15 additions & 15 deletions tests/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use pubgrub::range::Range;
use pubgrub::report::{DefaultStringReporter, Reporter as _};
use pubgrub::solver::{resolve, OfflineDependencyProvider};
use pubgrub::type_aliases::Map;
use pubgrub::version::{NumberVersion, SemanticVersion};
use pubgrub::version::SemanticVersion;

type NumVS = Range<NumberVersion>;
type NumVS = Range<u32>;
type SemVS = Range<SemanticVersion>;

use log::LevelFilter;
Expand Down Expand Up @@ -193,22 +193,22 @@ fn conflict_with_partial_satisfier() {
fn double_choices() {
init_log();
let mut dependency_provider = OfflineDependencyProvider::<&str, NumVS>::new();
dependency_provider.add_dependencies("a", 0, [("b", Range::full()), ("c", Range::full())]);
dependency_provider.add_dependencies("b", 0, [("d", Range::singleton(0))]);
dependency_provider.add_dependencies("b", 1, [("d", Range::singleton(1))]);
dependency_provider.add_dependencies("c", 0, []);
dependency_provider.add_dependencies("c", 1, [("d", Range::singleton(2))]);
dependency_provider.add_dependencies("d", 0, []);
dependency_provider.add_dependencies("a", 0u32, [("b", Range::full()), ("c", Range::full())]);
dependency_provider.add_dependencies("b", 0u32, [("d", Range::singleton(0u32))]);
dependency_provider.add_dependencies("b", 1u32, [("d", Range::singleton(1u32))]);
dependency_provider.add_dependencies("c", 0u32, []);
dependency_provider.add_dependencies("c", 1u32, [("d", Range::singleton(2u32))]);
dependency_provider.add_dependencies("d", 0u32, []);

// Solution.
let mut expected_solution = Map::default();
expected_solution.insert("a", 0.into());
expected_solution.insert("b", 0.into());
expected_solution.insert("c", 0.into());
expected_solution.insert("d", 0.into());
expected_solution.insert("a", 0u32);
expected_solution.insert("b", 0u32);
expected_solution.insert("c", 0u32);
expected_solution.insert("d", 0u32);

// Run the algorithm.
let computed_solution = resolve(&dependency_provider, "a", 0).unwrap();
let computed_solution = resolve(&dependency_provider, "a", 0u32).unwrap();
assert_eq!(expected_solution, computed_solution);
}

Expand All @@ -217,15 +217,15 @@ fn confusing_with_lots_of_holes() {
let mut dependency_provider = OfflineDependencyProvider::<&str, NumVS>::new();

// root depends on foo...
dependency_provider.add_dependencies("root", 1, vec![("foo", Range::full())]);
dependency_provider.add_dependencies("root", 1u32, vec![("foo", Range::full())]);

for i in 1..6 {
// foo depends on bar...
dependency_provider.add_dependencies("foo", i as u32, vec![("bar", Range::full())]);
}

let Err(PubGrubError::NoSolution(mut derivation_tree)) =
resolve(&dependency_provider, "root", 1)
resolve(&dependency_provider, "root", 1u32)
else {
unreachable!()
};
Expand Down
Loading

0 comments on commit c4d209a

Please sign in to comment.