-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: version parsing #240
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
4dd8806
wip
baszalmstra a1d61fd
wip
baszalmstra 7728648
wip
baszalmstra d02d6a8
wip
baszalmstra 551731d
wip
baszalmstra 9b19450
refactor: bump
baszalmstra e576aec
fix: clippy and fmt
baszalmstra 413c5d2
refactor: added flags
baszalmstra 61c55f0
Merge branch 'main' into refactor/version_parse
baszalmstra 253497a
fix: removed unused error kinds
baszalmstra 13139f2
test: compare to conda results
baszalmstra 7c77768
fix: tests and docs
baszalmstra 8125181
fix: docs
baszalmstra 901a443
fix: tests
baszalmstra 6628cbc
fix: 1-- and 1__
baszalmstra 311ffa0
fix: error messages
baszalmstra 62731ff
Merge branch 'main' into refactor/version_parse
baszalmstra d39d169
fix: remove default from version and fix issue
baszalmstra 1728599
fix: clippy
baszalmstra 5234e3b
feat: add VersionWithSource
baszalmstra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use criterion::{black_box, criterion_group, criterion_main, Criterion}; | ||
use rattler_conda_types::Version; | ||
|
||
fn criterion_benchmark(c: &mut Criterion) { | ||
c.bench_function("parse simple version", |b| { | ||
b.iter(|| black_box("3.11.4").parse::<Version>()) | ||
}); | ||
c.bench_function("parse complex version", |b| { | ||
b.iter(|| black_box("1!1.0b2.post345.dev456+3.2.20.rc3").parse::<Version>()) | ||
}); | ||
} | ||
|
||
criterion_group!(benches, criterion_benchmark); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
use std::fmt::{Debug, Formatter}; | ||
|
||
/// Bitmask indicates if the first component stored in a [`super::Version`] refers to an epoch. | ||
const EPOCH_MASK: u8 = 0b1; | ||
const EPOCH_OFFSET: u8 = 0; | ||
|
||
/// Bitmask that indicates what the index is of the first segment that belongs to the local version | ||
/// part. E.g. the part after the '+' sign in `1.2.3+4.5.6`. | ||
const LOCAL_VERSION_MASK: u8 = (1 << 7) - 1; | ||
const LOCAL_VERSION_OFFSET: u8 = 1; | ||
|
||
/// Encodes several edge cases in a single byte. | ||
/// | ||
/// The first bit is used to indicate whether or not there is an explicit epoch present in the | ||
/// version. If the flag is set it means the first entry in the [`Version::components`] array refers | ||
/// to the epoch instead of to the first component of the first segment. | ||
/// | ||
/// The remaining bits are used to encode the index of the first segment that belongs to the local | ||
/// version part instead of to the common part. A value of `0` indicates that there is not local | ||
/// version part. | ||
#[derive(Copy, Clone, Eq, PartialEq, Default)] | ||
#[repr(transparent)] | ||
pub struct Flags(u8); | ||
|
||
impl Debug for Flags { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
f.debug_struct("Flags") | ||
.field("has_epoch", &self.has_epoch()) | ||
.field("local_segment_index", &self.local_segment_index()) | ||
.finish() | ||
} | ||
} | ||
|
||
impl Flags { | ||
/// Sets whether or not the version has an explicit epoch. | ||
#[must_use] | ||
pub fn with_has_epoch(self, has_epoch: bool) -> Self { | ||
let flag = self.0 & !(EPOCH_MASK << EPOCH_OFFSET); | ||
Self( | ||
flag | if has_epoch { | ||
EPOCH_MASK << EPOCH_OFFSET | ||
} else { | ||
0 | ||
}, | ||
) | ||
} | ||
|
||
/// Returns true if this instance indicates that an explicit epoch is present. | ||
pub fn has_epoch(self) -> bool { | ||
(self.0 >> EPOCH_OFFSET) & EPOCH_MASK != 0 | ||
} | ||
|
||
/// Sets the index where the local segment starts. Returns `None` if the index is too large to | ||
/// be stored. | ||
#[must_use] | ||
pub fn with_local_segment_index(self, index: u8) -> Option<Self> { | ||
if index > LOCAL_VERSION_MASK { | ||
None | ||
} else { | ||
Some(Self( | ||
(self.0 & !(LOCAL_VERSION_MASK << LOCAL_VERSION_OFFSET)) | ||
| (index << LOCAL_VERSION_OFFSET), | ||
)) | ||
} | ||
} | ||
|
||
/// Returns the index of the first segment that belongs to the local part of the version. | ||
pub fn local_segment_index(self) -> u8 { | ||
(self.0 >> LOCAL_VERSION_OFFSET) & LOCAL_VERSION_MASK | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::version::flags::Flags; | ||
|
||
#[test] | ||
fn test_epoch() { | ||
assert_eq!(Flags::default().has_epoch(), false); | ||
assert_eq!(Flags::default().with_has_epoch(true).has_epoch(), true); | ||
assert_eq!( | ||
Flags::default() | ||
.with_has_epoch(true) | ||
.with_has_epoch(false) | ||
.has_epoch(), | ||
false | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_local_segment_idx() { | ||
assert_eq!(Flags::default().local_segment_index(), 0); | ||
assert_eq!( | ||
Flags::default() | ||
.with_local_segment_index(42) | ||
.unwrap() | ||
.local_segment_index(), | ||
42 | ||
); | ||
assert_eq!( | ||
Flags::default() | ||
.with_local_segment_index(127) | ||
.unwrap() | ||
.local_segment_index(), | ||
127 | ||
); | ||
assert_eq!(Flags::default().with_local_segment_index(128), None); | ||
} | ||
|
||
#[test] | ||
fn test_all_elements() { | ||
let flags = Flags::default() | ||
.with_has_epoch(true) | ||
.with_local_segment_index(101) | ||
.unwrap(); | ||
|
||
assert!(flags.has_epoch()); | ||
assert_eq!(flags.local_segment_index(), 101); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what happened here? o.O
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that's what I explained in: