Skip to content

Commit f843018

Browse files
committed
Knock out some more easy TODOs
1 parent 027396b commit f843018

File tree

8 files changed

+50
-58
lines changed

8 files changed

+50
-58
lines changed

cli/src/input.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl<C: clap::Args + Customize> Prepare for InputOptions<C> {
8181
}
8282

8383
if let Some(lock_file_path) = self.lock_file.as_ref() {
84-
let lock_file = LockFile::from_path(lock_file_path);
84+
let lock_file = LockFile::from_path(lock_file_path)?;
8585
let lock_dir = lock_file_path
8686
.parent()
8787
.ok_or_else(|| Error::PathWithoutParent {

package/src/error.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,19 @@ use crate::{
99
UnversionedPackage,
1010
};
1111

12-
// TODO: implement IntoDiagnostic.
12+
/// Errors related to package management.
1313
pub enum Error {
1414
Io {
1515
path: Option<PathBuf>,
1616
error: std::io::Error,
1717
},
18-
Serialize {
18+
/// We failed to serialize the index description.
19+
PackageSerialization {
20+
pkg: crate::index::Package,
21+
error: serde_json::Error,
22+
},
23+
LockFileDeserialization {
24+
path: PathBuf,
1925
error: serde_json::Error,
2026
},
2127
ManifestEval {
@@ -149,15 +155,18 @@ impl std::fmt::Display for Error {
149155
Error::DuplicateIndexPackageVersion { id, version } => {
150156
write!(f, "package {id}@{version} is already present in the index")
151157
}
152-
Error::Serialize { error } => {
153-
write!(f, "serialization error: {error}")
158+
Error::PackageSerialization { error, pkg } => {
159+
write!(f, "error serializing package; this is a bug in nickel. Failed package {pkg:?}, caused by {error}")
154160
}
155161
Error::NoProjectDir => {
156162
write!(
157163
f,
158164
"failed to find a location for the nickel cache directory"
159165
)
160166
}
167+
Error::LockFileDeserialization { path, error } => {
168+
write!(f, "lock file {} is invalid: {error}", path.display())
169+
}
161170
}
162171
}
163172
}
@@ -216,9 +225,3 @@ impl From<tempfile::PersistError> for Error {
216225
Self::TempFilePersist { error }
217226
}
218227
}
219-
220-
impl From<serde_json::Error> for Error {
221-
fn from(error: serde_json::Error) -> Self {
222-
Self::Serialize { error }
223-
}
224-
}

package/src/index/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ impl PackageCache {
112112
}
113113
let mut tmp = self.tmp_file(&id);
114114
for pkg in existing.packages.values() {
115-
serde_json::to_writer(&mut tmp, pkg)?;
115+
serde_json::to_writer(&mut tmp, pkg).map_err(|error| Error::PackageSerialization {
116+
pkg: pkg.clone(),
117+
error,
118+
})?;
116119
tmp.write_all(b"\n").with_path(tmp.path())?;
117120
}
118121

package/src/lib.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use config::Config;
99
use error::Error;
1010
use serde::{Deserialize, Serialize};
1111
use serde_with::{DeserializeFromStr, SerializeDisplay};
12-
use version::{PartialSemVer, PartialSemVerParseError, SemVer, SemVerParseError};
12+
use version::{PartialSemVerParseError, SemVer, SemVerParseError, SemVerPrefix};
1313

1414
pub mod config;
1515
pub mod error;
@@ -37,9 +37,7 @@ pub struct GitDependency {
3737

3838
#[derive(Clone, Debug, PartialEq, Eq, Hash, DeserializeFromStr, SerializeDisplay)]
3939
pub enum VersionReq {
40-
// TODO: could make this a PartialSemVer
41-
Compatible(SemVer),
42-
// TODO: This one could allow pre-releases
40+
Compatible(SemVerPrefix),
4341
Exact(SemVer),
4442
}
4543

@@ -67,17 +65,15 @@ impl FromStr for VersionReq {
6765
if let Some(v) = s.strip_prefix('=') {
6866
Ok(VersionReq::Exact(v.parse()?))
6967
} else {
70-
Ok(VersionReq::Compatible(PartialSemVer::from_str(s)?.into()))
68+
Ok(VersionReq::Compatible(SemVerPrefix::from_str(s)?))
7169
}
7270
}
7371
}
7472

7573
impl VersionReq {
7674
pub fn matches(&self, v: &SemVer) -> bool {
7775
match self {
78-
VersionReq::Compatible(lower_bound) => {
79-
lower_bound <= v && *v < lower_bound.next_incompatible()
80-
}
76+
VersionReq::Compatible(lower_bound) => lower_bound.matches(v),
8177
VersionReq::Exact(w) => v == w,
8278
}
8379
}

package/src/lock.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,14 @@ impl LockFile {
149149
})
150150
}
151151

152-
// TODO: propagate the error
153-
pub fn from_path(path: impl AsRef<Path>) -> Self {
154-
let contents = std::fs::read_to_string(path.as_ref()).unwrap();
155-
serde_json::from_str(&contents).unwrap()
152+
/// Read a lock-file from disk.
153+
pub fn from_path(path: impl AsRef<Path>) -> Result<Self, Error> {
154+
let path = path.as_ref();
155+
let contents = std::fs::read_to_string(path).with_path(path)?;
156+
serde_json::from_str(&contents).map_err(|error| Error::LockFileDeserialization {
157+
path: path.to_owned(),
158+
error,
159+
})
156160
}
157161

158162
/// Build a package map from a lock-file.

package/src/manifest.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::{
2323
lock::LockFile,
2424
repo_root,
2525
resolve::{Resolution, UnversionedPrecise},
26-
version::{FullSemVer, PartialSemVer, SemVer},
26+
version::{FullSemVer, SemVer, SemVerPrefix},
2727
Dependency, GitDependency, Precise, VersionReq,
2828
};
2929

@@ -42,7 +42,7 @@ use crate::{
4242
struct ManifestFileFormat {
4343
name: Ident,
4444
version: FullSemVer,
45-
minimal_nickel_version: PartialSemVer,
45+
minimal_nickel_version: SemVerPrefix,
4646
dependencies: HashMap<Ident, DependencyFormat>,
4747
}
4848

package/src/resolve.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ impl From<BucketVersion> for SemVer {
163163
impl From<VersionReq> for BucketVersion {
164164
fn from(v: VersionReq) -> Self {
165165
match v {
166-
VersionReq::Compatible(v) | VersionReq::Exact(v) => v.into(),
166+
VersionReq::Compatible(v) => SemVer::from(v).into(),
167+
VersionReq::Exact(v) => v.into(),
167168
}
168169
}
169170
}

package/src/version.rs

+16-31
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,19 @@ impl SemVer {
4343
major,
4444
minor,
4545
patch,
46-
pre: String::default(),
47-
}
48-
}
49-
50-
pub fn bump_major(&self) -> SemVer {
51-
SemVer {
52-
major: self.major + 1,
53-
minor: 0,
54-
patch: 0,
55-
pre: String::default(),
56-
}
57-
}
58-
59-
pub fn bump_minor(&self) -> SemVer {
60-
SemVer {
61-
major: self.major,
62-
minor: self.minor + 1,
63-
patch: 0,
64-
pre: String::default(),
46+
pre: String::new(),
6547
}
6648
}
49+
}
6750

68-
pub fn next_incompatible(&self) -> SemVer {
69-
// TODO: should we panic or something if pre is non-empty?
70-
if self.major == 0 {
71-
self.bump_minor()
72-
} else {
73-
self.bump_major()
51+
impl SemVerPrefix {
52+
pub fn matches(&self, v: &SemVer) -> bool {
53+
match (self.minor, self.patch) {
54+
(None, _) => v.major == self.major,
55+
(Some(minor), None) => v.major == self.major && v.minor >= minor,
56+
(Some(minor), Some(patch)) => {
57+
v.major == self.major && v.minor == minor && v.patch >= patch
58+
}
7459
}
7560
}
7661
}
@@ -113,8 +98,8 @@ impl From<SemVer> for FullSemVer {
11398
// information is sometimes relevant for comparing version requirements (e.g.,
11499
// "1.3.0" matches the requirement "1.2" but it doesn't match the requirement
115100
// "1.2.0").
116-
impl From<PartialSemVer> for SemVer {
117-
fn from(psv: PartialSemVer) -> Self {
101+
impl From<SemVerPrefix> for SemVer {
102+
fn from(psv: SemVerPrefix) -> Self {
118103
Self {
119104
major: psv.major,
120105
minor: psv.minor.unwrap_or(0),
@@ -166,13 +151,13 @@ impl std::fmt::Display for SemVer {
166151
#[derive(
167152
Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, serde::Serialize, serde::Deserialize,
168153
)]
169-
pub struct PartialSemVer {
154+
pub struct SemVerPrefix {
170155
pub major: u64,
171156
pub minor: Option<u64>,
172157
pub patch: Option<u64>,
173158
}
174159

175-
impl PartialSemVer {
160+
impl SemVerPrefix {
176161
pub fn major_minor(major: u64, minor: u64) -> Self {
177162
Self {
178163
major,
@@ -192,7 +177,7 @@ pub enum PartialSemVerParseError {
192177
Num(#[from] ParseIntError),
193178
}
194179

195-
impl FromStr for PartialSemVer {
180+
impl FromStr for SemVerPrefix {
196181
type Err = PartialSemVerParseError;
197182

198183
fn from_str(s: &str) -> Result<Self, Self::Err> {
@@ -215,7 +200,7 @@ impl FromStr for PartialSemVer {
215200
}
216201
}
217202

218-
impl std::fmt::Display for PartialSemVer {
203+
impl std::fmt::Display for SemVerPrefix {
219204
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
220205
match (self.minor, self.patch) {
221206
(None, _) => {

0 commit comments

Comments
 (0)