Skip to content

Commit

Permalink
refactor(schema): Adopt RustVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Dec 11, 2023
1 parent 085d860 commit 808e2a1
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 59 deletions.
3 changes: 2 additions & 1 deletion crates/resolver-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use cargo::core::{Dependency, PackageId, Registry, Summary};
use cargo::core::{GitReference, SourceId};
use cargo::sources::source::QueryKind;
use cargo::sources::IndexSummary;
use cargo::util::{CargoResult, Config, IntoUrl, RustVersion};
use cargo::util::{CargoResult, Config, IntoUrl};
use cargo::util_schemas::manifest::RustVersion;

use proptest::collection::{btree_map, vec};
use proptest::prelude::*;
Expand Down
3 changes: 2 additions & 1 deletion src/cargo/core/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use crate::core::{Dependency, PackageId, PackageIdSpec, SourceId, Summary};
use crate::core::{Edition, Feature, Features, WorkspaceConfig};
use crate::util::errors::*;
use crate::util::interning::InternedString;
use crate::util::{short_hash, Config, Filesystem, RustVersion};
use crate::util::{short_hash, Config, Filesystem};
use crate::util_schemas::manifest::RustVersion;
use crate::util_schemas::manifest::{TomlManifest, TomlProfiles};

pub enum EitherManifest {
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ use crate::util::network::http::HttpTimeout;
use crate::util::network::retry::{Retry, RetryResult};
use crate::util::network::sleep::SleepTracker;
use crate::util::toml::prepare_for_publish;
use crate::util::RustVersion;
use crate::util::{self, internal, Config, Progress, ProgressStyle};
use crate::util_schemas::manifest::RustVersion;

pub const MANIFEST_PREAMBLE: &str = "\
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/resolver/version_prefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::collections::{HashMap, HashSet};

use crate::core::{Dependency, PackageId, Summary};
use crate::util::interning::InternedString;
use crate::util::RustVersion;
use crate::util_schemas::manifest::RustVersion;

/// A collection of preferences for particular package versions.
///
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/summary.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::core::{Dependency, PackageId, SourceId};
use crate::util::interning::InternedString;
use crate::util::CargoResult;
use crate::util::RustVersion;
use crate::util_schemas::manifest::RustVersion;
use anyhow::bail;
use semver::Version;
use std::collections::{BTreeMap, HashMap, HashSet};
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ use crate::util::edit_distance;
use crate::util::errors::{CargoResult, ManifestError};
use crate::util::interning::InternedString;
use crate::util::toml::{read_manifest, InheritableFields};
use crate::util::RustVersion;
use crate::util::{config::ConfigRelativePath, Config, Filesystem, IntoUrl};
use crate::util_schemas::manifest::RustVersion;
use crate::util_schemas::manifest::{TomlDependency, TomlProfiles};
use cargo_util::paths;
use cargo_util::paths::normalize_path;
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/cargo_add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::util::toml_mut::dependency::WorkspaceSource;
use crate::util::toml_mut::is_sorted;
use crate::util::toml_mut::manifest::DepTable;
use crate::util::toml_mut::manifest::LocalManifest;
use crate::util::RustVersion;
use crate::util_schemas::manifest::RustVersion;
use crate::CargoResult;
use crate::Config;
use crate_spec::CrateSpec;
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ use crate::ops;
use crate::sources::PathSource;
use crate::util::cache_lock::CacheLockMode;
use crate::util::errors::CargoResult;
use crate::util::RustVersion;
use crate::util::{profile, CanonicalUrl};
use crate::util_schemas::manifest::RustVersion;
use anyhow::Context as _;
use std::collections::{HashMap, HashSet};
use tracing::{debug, trace};
Expand Down
3 changes: 2 additions & 1 deletion src/cargo/sources/registry/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ use crate::sources::registry::{LoadResponse, RegistryData};
use crate::util::cache_lock::CacheLockMode;
use crate::util::interning::InternedString;
use crate::util::IntoUrl;
use crate::util::{internal, CargoResult, Config, Filesystem, OptVersionReq, RustVersion};
use crate::util::{internal, CargoResult, Config, Filesystem, OptVersionReq};
use crate::util_schemas::manifest::RustVersion;
use anyhow::bail;
use cargo_util::{paths, registry::make_dep_path};
use semver::Version;
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub use self::progress::{Progress, ProgressStyle};
pub use self::queue::Queue;
pub use self::restricted_names::validate_package_name;
pub use self::rustc::Rustc;
pub use self::semver_ext::{OptVersionReq, RustVersion};
pub use self::semver_ext::OptVersionReq;
pub use self::vcs::{existing_vcs_repo, FossilRepo, GitRepo, HgRepo, PijulRepo};
pub use self::workspace::{
add_path_args, path_args, print_available_benches, print_available_binaries,
Expand Down
47 changes: 0 additions & 47 deletions src/cargo/util/semver_ext.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::fmt::{self, Display};

use semver::{Op, Version, VersionReq};
use serde_untagged::UntaggedEnumVisitor;

use crate::util_semver::PartialVersion;
use crate::util_semver::VersionExt as _;

#[derive(PartialEq, Eq, Hash, Clone, Debug)]
Expand Down Expand Up @@ -125,48 +123,3 @@ impl From<VersionReq> for OptVersionReq {
OptVersionReq::Req(req)
}
}

#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Debug, serde::Serialize)]
#[serde(transparent)]
pub struct RustVersion(PartialVersion);

impl std::ops::Deref for RustVersion {
type Target = PartialVersion;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl std::str::FromStr for RustVersion {
type Err = anyhow::Error;

fn from_str(value: &str) -> Result<Self, Self::Err> {
let partial = value.parse::<PartialVersion>()?;
if partial.pre.is_some() {
anyhow::bail!("unexpected prerelease field, expected a version like \"1.32\"")
}
if partial.build.is_some() {
anyhow::bail!("unexpected prerelease field, expected a version like \"1.32\"")
}
Ok(Self(partial))
}
}

impl<'de> serde::Deserialize<'de> for RustVersion {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
UntaggedEnumVisitor::new()
.expecting("SemVer version")
.string(|value| value.parse().map_err(serde::de::Error::custom))
.deserialize(deserializer)
}
}

impl Display for RustVersion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
2 changes: 1 addition & 1 deletion src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ use crate::util::interning::InternedString;
use crate::util::restricted_names;
use crate::util::{
self, config::ConfigRelativePath, validate_package_name, Config, IntoUrl, OptVersionReq,
RustVersion,
};
use crate::util_schemas::manifest;
use crate::util_schemas::manifest::RustVersion;

mod embedded;
mod targets;
Expand Down
47 changes: 46 additions & 1 deletion src/cargo/util_schemas/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use serde::ser;
use serde::{Deserialize, Serialize};
use serde_untagged::UntaggedEnumVisitor;

use crate::util::RustVersion;
use crate::util_schemas::core::PackageIdSpec;
use crate::util_semver::PartialVersion;

/// This type is used to deserialize `Cargo.toml` files.
#[derive(Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -1208,6 +1208,51 @@ pub enum TomlLintLevel {
Allow,
}

#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Debug, serde::Serialize)]
#[serde(transparent)]
pub struct RustVersion(PartialVersion);

impl std::ops::Deref for RustVersion {
type Target = PartialVersion;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl std::str::FromStr for RustVersion {
type Err = anyhow::Error;

fn from_str(value: &str) -> Result<Self, Self::Err> {
let partial = value.parse::<PartialVersion>()?;
if partial.pre.is_some() {
anyhow::bail!("unexpected prerelease field, expected a version like \"1.32\"")
}
if partial.build.is_some() {
anyhow::bail!("unexpected prerelease field, expected a version like \"1.32\"")
}
Ok(Self(partial))
}
}

impl<'de> serde::Deserialize<'de> for RustVersion {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
UntaggedEnumVisitor::new()
.expecting("SemVer version")
.string(|value| value.parse().map_err(serde::de::Error::custom))
.deserialize(deserializer)
}
}

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

#[derive(Copy, Clone, Debug)]
pub struct InvalidCargoFeatures {}

Expand Down

0 comments on commit 808e2a1

Please sign in to comment.