Skip to content

Commit

Permalink
tag::Score: Rename (unchecked) constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
uklotzde committed Jul 26, 2023
1 parent a4c0947 commit 3732ed9
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 26 deletions.
6 changes: 3 additions & 3 deletions crates/core-json/src/tag/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl<'de> Visitor<'de> for ScoreVisitor {
where
E: serde::de::Error,
{
let score = _core::Score::new(value);
let score = _core::Score::new_unchecked(value);
if !score.is_valid() {
return Err(serde::de::Error::invalid_value(
serde::de::Unexpected::Float(value),
Expand Down Expand Up @@ -259,12 +259,12 @@ impl From<PlainTag> for _core::PlainTag<'static> {
..Default::default()
},
IntScoreFallback(iscore) => _core::PlainTag {
score: _core::Score::new(iscore as f64),
score: _core::Score::new_unchecked(iscore as f64),
..Default::default()
},
LabelIntScoreFallback(label, iscore) => _core::PlainTag {
label: Some(label.into()),
score: _core::Score::new(iscore as f64),
score: _core::Score::new_unchecked(iscore as f64),
},
LabelScore(label, score) => _core::PlainTag {
label: Some(label.into()),
Expand Down
10 changes: 5 additions & 5 deletions crates/core-json/src/tag/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn should_fail_to_deserialize_plain_tag_from_single_element_array_with_label() {

#[test]
fn deserialize_plain_tag_score_integer_one() {
let score = _core::Score::new(1.0);
let score = _core::Score::new_unchecked(1.0);
let tag: PlainTag = serde_json::from_str("1").unwrap();
assert_eq!(
_core::PlainTag::from(PlainTag::Score(score.into())),
Expand All @@ -34,7 +34,7 @@ fn deserialize_plain_tag_score_integer_one() {

#[test]
fn deserialize_plain_tag_score_integer_zero() {
let score = _core::Score::new(0.0);
let score = _core::Score::new_unchecked(0.0);
let tag: PlainTag = serde_json::from_str("0").unwrap();
assert_eq!(
_core::PlainTag::from(PlainTag::Score(score.into())),
Expand All @@ -46,7 +46,7 @@ fn deserialize_plain_tag_score_integer_zero() {
#[test]
fn deserialize_plain_tag_label_score() {
let label = _core::Label::from_unchecked("label");
let score = _core::Score::new(0.5);
let score = _core::Score::new_unchecked(0.5);
let json = format!("[\"{label}\",{}]", score.value());
let tag: PlainTag = serde_json::from_str(&json).unwrap();
assert_eq!(PlainTag::LabelScore(label.into(), score.into()), tag);
Expand All @@ -57,7 +57,7 @@ fn deserialize_plain_tag_label_score() {
fn deserialize_plain_tag_label_score_integer_zero() {
let expected_tag = _core::PlainTag {
label: Some(_core::Label::from_unchecked("label")),
score: _core::Score::new(0.0),
score: _core::Score::new_unchecked(0.0),
};
// Ensure to parse score from literal 0, not 0.0!
let json = format!("[\"{}\",0]", expected_tag.label.as_ref().unwrap());
Expand All @@ -70,7 +70,7 @@ fn deserialize_plain_tag_label_score_integer_zero() {
fn deserialize_plain_tag_label_score_integer_one() {
let expected_tag = _core::PlainTag {
label: Some(_core::Label::from_unchecked("label")),
score: _core::Score::new(1.0),
score: _core::Score::new_unchecked(1.0),
};
// Ensure to parse score from literal 1, not 1.0!
let json = format!("[\"{}\",1]", expected_tag.label.as_ref().unwrap());
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/tag/score/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ impl Score {
}

pub fn clamp_from(value: impl Into<ScoreValue>) -> Self {
Self::new(Self::clamp_value(value))
Self::new_unchecked(Self::clamp_value(value))
}

#[must_use]
pub const fn new(value: ScoreValue) -> Self {
pub const fn new_unchecked(value: ScoreValue) -> Self {
Self(value)
}

Expand Down
12 changes: 9 additions & 3 deletions crates/core/src/tag/score/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ use super::*;
fn validate() {
assert!(Score::MIN.validate().is_ok());
assert!(Score::MAX.validate().is_ok());
assert!(Score::new(Score::MIN.0 + Score::MAX.0).validate().is_ok());
assert!(Score::new(Score::MIN.0 - Score::MAX.0).validate().is_err());
assert!(Score::new(Score::MAX.0 + Score::MAX.0).validate().is_err());
assert!(Score::new_unchecked(Score::MIN.0 + Score::MAX.0)
.validate()
.is_ok());
assert!(Score::new_unchecked(Score::MIN.0 - Score::MAX.0)
.validate()
.is_err());
assert!(Score::new_unchecked(Score::MAX.0 + Score::MAX.0)
.validate()
.is_err());
}

#[test]
Expand Down
22 changes: 11 additions & 11 deletions crates/core/src/tag/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn canonical_unique_labels_and_score() {
plain: vec![
PlainTag {
label: Some(Label::from_unchecked("label1")),
score: Score::new(0.5),
score: Score::new_unchecked(0.5),
},
PlainTag {
label: Some(Label::from_unchecked("label2")),
Expand All @@ -43,15 +43,15 @@ fn duplicate_labels_same_score() {
plain: vec![
PlainTag {
label: Some(Label::from_unchecked("label1")),
score: Score::new(0.5),
score: Score::new_unchecked(0.5),
},
PlainTag {
label: Some(Label::from_unchecked("label2")),
..Default::default()
},
PlainTag {
label: Some(Label::from_unchecked("label1")),
score: Score::new(0.5),
score: Score::new_unchecked(0.5),
},
],
..Default::default()
Expand All @@ -66,15 +66,15 @@ fn duplicate_labels_differing_score() {
plain: vec![
PlainTag {
label: Some(Label::from_unchecked("label1")),
score: Score::new(0.7),
score: Score::new_unchecked(0.7),
},
PlainTag {
label: Some(Label::from_unchecked("label2")),
..Default::default()
},
PlainTag {
label: Some(Label::from_unchecked("label1")),
score: Score::new(0.5),
score: Score::new_unchecked(0.5),
},
],
..Default::default()
Expand Down Expand Up @@ -191,11 +191,11 @@ fn duplicate_facets_and_labels() {
tags: vec![
PlainTag {
label: Some(Label::from_unchecked("label2")),
score: Score::new(0.5),
score: Score::new_unchecked(0.5),
},
PlainTag {
label: Some(Label::from_unchecked("label2")),
score: Score::new(1.0),
score: Score::new_unchecked(1.0),
},
],
},
Expand Down Expand Up @@ -246,15 +246,15 @@ fn duplicate_facets_and_labels() {
tags: vec![
PlainTag {
label: Some(Label::from_unchecked("label2")),
score: Score::new(0.5),
score: Score::new_unchecked(0.5),
},
PlainTag {
label: Some(Label::from_unchecked("label2")),
score: Score::new(0.75),
score: Score::new_unchecked(0.75),
},
PlainTag {
label: Some(Label::from_unchecked("label2")),
score: Score::new(0.25),
score: Score::new_unchecked(0.25),
},
],
},
Expand All @@ -267,7 +267,7 @@ fn duplicate_facets_and_labels() {
facet_id: FacetId::from_unchecked("facet2"),
tags: vec![PlainTag {
label: Some(Label::from_unchecked("label2")),
score: Score::new(0.75),
score: Score::new_unchecked(0.75),
},],
}));
}
Expand Down
2 changes: 1 addition & 1 deletion crates/media-file/src/util/gigtag/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fn plain_tag_with_label_and_score<'a>(
fn try_import_tags() {
let label_value = "DJ";
let date_like_facet = facet_from_str("@20220703");
let score = aoide_core::tag::Score::new(0.75);
let score = aoide_core::tag::Score::new_unchecked(0.75);

// Label
let tag = Tag {
Expand Down
2 changes: 1 addition & 1 deletion crates/repo-sqlite/src/db/track_tag/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl From<QueryableRecord> for (RecordId, Record) {
debug_assert!(facet_id.as_ref().map_or(true, FacetId::is_valid));
let label = label.map(Label::from_unchecked);
debug_assert!(label.as_ref().map_or(true, Label::is_valid));
let score = Score::new(score);
let score = Score::new_unchecked(score);
debug_assert!(score.is_valid());
let record = Record {
track_id: track_id.into(),
Expand Down

0 comments on commit 3732ed9

Please sign in to comment.