Skip to content

Commit 2121742

Browse files
committed
Move to our own version type
1 parent d488241 commit 2121742

File tree

11 files changed

+424
-185
lines changed

11 files changed

+424
-185
lines changed

Cargo.lock

+100
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ serde = "1.0.164"
9090
serde_json = "1.0.96"
9191
serde_repr = "0.1"
9292
serde-wasm-bindgen = "0.5.0"
93+
serde_with = "3.11.0"
9394
serde_yaml = "0.9.19"
9495
sha-1 = "0.10.0"
9596
sha2 = "0.10.6"

cli/src/package.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use nickel_lang_core::{
1111
use nickel_lang_package::{
1212
config::Config,
1313
index::{self, PackageIndex},
14-
ManifestFile, SemanticVersion,
14+
version::SemVer,
15+
ManifestFile,
1516
};
1617

1718
use crate::{
@@ -46,7 +47,7 @@ pub enum Command {
4647
index: PathBuf,
4748

4849
#[arg(long)]
49-
version: SemanticVersion,
50+
version: SemVer,
5051

5152
#[arg(long)]
5253
commit_id: ObjectId,
@@ -110,7 +111,7 @@ impl PackageCommand {
110111
commit_id,
111112
} => {
112113
let package =
113-
nickel_lang_package::index::fetch_git(package_id, *version, commit_id)?;
114+
nickel_lang_package::index::fetch_git(package_id, version.clone(), commit_id)?;
114115
let config = Config::default().with_index_dir(index.clone());
115116
let mut package_index = PackageIndex::new(config);
116117
package_index.save(package)?;

package/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ regex.workspace = true
2424
semver = { version = "1.0.23", features = ["serde"] }
2525
serde.workspace = true
2626
serde_json.workspace = true
27+
serde_with.workspace = true
2728
tempfile = { workspace = true }
29+
thiserror.workspace = true
2830

2931
[dev-dependencies]
3032
insta = { workspace = true, features = ["filters"] }

package/src/error.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ use std::path::{Path, PathBuf};
33
use nickel_lang_core::{
44
eval::cache::CacheImpl, identifier::Ident, package::ObjectId, program::Program,
55
};
6-
use pubgrub::version::SemanticVersion;
76

87
use crate::{
98
index::{self},
9+
version::SemVer,
1010
UnversionedPackage,
1111
};
1212

@@ -69,7 +69,7 @@ pub enum Error {
6969
/// package and version was already present.
7070
DuplicateIndexPackageVersion {
7171
id: index::Id,
72-
version: SemanticVersion,
72+
version: SemVer,
7373
},
7474
}
7575

package/src/index/mod.rs

+17-13
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ use std::{
1717

1818
use nickel_lang_core::{identifier::Ident, package::ObjectId};
1919
use nickel_lang_git::Spec;
20-
use pubgrub::version::SemanticVersion;
2120
use regex::Regex;
2221
use serde::{Deserialize, Serialize};
2322
use tempfile::{tempdir_in, NamedTempFile};
2423

2524
use crate::{
2625
config::Config,
2726
error::{Error, IoResultExt as _},
27+
version::SemVer,
2828
Precise, VersionReq,
2929
};
3030

@@ -79,7 +79,11 @@ impl PackageCache {
7979
let data = std::fs::read_to_string(&path).with_path(&path)?;
8080
for line in data.lines() {
8181
let package: Package = serde_json::from_str(line).unwrap();
82-
if file.packages.insert(package.vers, package).is_some() {
82+
if file
83+
.packages
84+
.insert(package.vers.clone(), package)
85+
.is_some()
86+
{
8387
panic!("duplicate version, index is corrupt");
8488
}
8589
}
@@ -97,12 +101,12 @@ impl PackageCache {
97101
/// (Also retains a cached copy in memory.)
98102
pub fn save(&mut self, pkg: Package) -> Result<(), Error> {
99103
let id: Id = pkg.id.clone().into();
100-
let version = pkg.vers;
104+
let version = pkg.vers.clone();
101105
let mut existing = self
102106
.load(&id)?
103107
.cloned()
104108
.unwrap_or(CachedPackageFile::default());
105-
if existing.packages.insert(pkg.vers, pkg).is_some() {
109+
if existing.packages.insert(pkg.vers.clone(), pkg).is_some() {
106110
return Err(Error::DuplicateIndexPackageVersion { id, version });
107111
}
108112
let mut tmp = self.tmp_file(&id);
@@ -172,7 +176,7 @@ impl PackageIndex {
172176
pub fn available_versions<'a>(
173177
&'a self,
174178
id: &Id,
175-
) -> Result<impl Iterator<Item = SemanticVersion> + 'a, Error> {
179+
) -> Result<impl Iterator<Item = SemVer> + 'a, Error> {
176180
let mut cache = self.cache.borrow_mut();
177181
let pkg_file = cache.load(id)?;
178182
let versions: Vec<_> = pkg_file
@@ -181,31 +185,31 @@ impl PackageIndex {
181185
Ok(versions.into_iter())
182186
}
183187

184-
pub fn all_versions(&self, id: &Id) -> Result<HashMap<SemanticVersion, Package>, Error> {
188+
pub fn all_versions(&self, id: &Id) -> Result<HashMap<SemVer, Package>, Error> {
185189
let mut cache = self.cache.borrow_mut();
186190
let pkg_file = cache.load(id)?;
187191
Ok(pkg_file
188192
.map(|pkg_file| {
189193
pkg_file
190194
.packages
191195
.iter()
192-
.map(|(v, package)| (*v, package.clone()))
196+
.map(|(v, package)| (v.clone(), package.clone()))
193197
.collect()
194198
})
195199
.unwrap_or_default())
196200
}
197201

198-
pub fn package(&self, id: &Id, v: SemanticVersion) -> Result<Option<Package>, Error> {
202+
pub fn package(&self, id: &Id, v: SemVer) -> Result<Option<Package>, Error> {
199203
Ok(self.all_versions(id)?.get(&v).cloned())
200204
}
201205

202206
pub fn save(&mut self, pkg: Package) -> Result<(), Error> {
203207
self.cache.borrow_mut().save(pkg)
204208
}
205209

206-
pub fn ensure_downloaded(&self, id: &Id, v: SemanticVersion) -> Result<(), Error> {
210+
pub fn ensure_downloaded(&self, id: &Id, v: SemVer) -> Result<(), Error> {
207211
let package = self
208-
.package(id, v)?
212+
.package(id, v.clone())?
209213
.ok_or_else(|| Error::UnknownIndexPackage { id: id.clone() })?;
210214
let precise = Precise::Index {
211215
id: id.clone(),
@@ -371,15 +375,15 @@ impl From<PreciseId> for Id {
371375

372376
#[derive(Clone, Debug, Default)]
373377
pub struct CachedPackageFile {
374-
pub packages: BTreeMap<SemanticVersion, Package>,
378+
pub packages: BTreeMap<SemVer, Package>,
375379
}
376380

377381
/// A package record in the index.
378382
#[derive(Clone, Debug, Serialize, Deserialize)]
379383
pub struct Package {
380384
pub id: PreciseId,
381-
pub vers: SemanticVersion,
382-
pub nickel_vers: SemanticVersion,
385+
pub vers: SemVer,
386+
pub nickel_vers: SemVer,
383387
pub deps: BTreeMap<Ident, IndexDependency>,
384388

385389
/// Version of the index schema. Currently always zero.

package/src/index/scrape.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
77
use nickel_lang_core::package::ObjectId;
88
use nickel_lang_git::Spec;
9-
use pubgrub::version::SemanticVersion;
109
use tempfile::tempdir;
1110

1211
use crate::{
1312
error::{Error, IoResultExt},
13+
version::SemVer,
1414
ManifestFile,
1515
};
1616

@@ -19,7 +19,7 @@ use super::{Id, Package, PreciseId};
1919
/// Fetch a package from the specified place, and figure out what its index
2020
/// entry should look like.
2121
/// TODO: allow a subdirectory?
22-
pub fn fetch_git(id: &Id, version: SemanticVersion, commit: &ObjectId) -> Result<Package, Error> {
22+
pub fn fetch_git(id: &Id, version: SemVer, commit: &ObjectId) -> Result<Package, Error> {
2323
// We need to fetch the manifest file to get some metadata out. We're currently shallow-cloning
2424
// the whole repo, but we could use a github API (or maybe some fancier git features) to be more
2525
// efficient.

0 commit comments

Comments
 (0)