style: format the whole tree with cargo fmt (stock defaults, #35)
One-time, mechanical reformat — no functional changes. The tree was not rustfmt-clean (~1800 hunks across ~100 files); this brings it to stock `cargo fmt` defaults so a `cargo fmt --check` CI gate can follow. Behaviour-preserving: 2509 pass / 0 fail / 1 ignored (unchanged baseline), clippy clean. A .git-blame-ignore-revs entry follows so `git blame` skips this commit.
This commit is contained in:
+127
-42
@@ -81,17 +81,22 @@ pub fn generate_value(generator: &Generator, ty: Type, rng: &mut SeedRng) -> Val
|
||||
Generator::CurrencyAmount => currency_amount(ty, rng),
|
||||
Generator::Age => Value::Number(rng.random_range(18..=80).to_string()),
|
||||
Generator::SmallInt => Value::Number(rng.random_range(1..=100).to_string()),
|
||||
Generator::YearRecent => {
|
||||
Value::Number(rng.random_range((REF_YEAR - YEAR_RECENT_SPAN)..=REF_YEAR).to_string())
|
||||
}
|
||||
Generator::YearRecent => Value::Number(
|
||||
rng.random_range((REF_YEAR - YEAR_RECENT_SPAN)..=REF_YEAR)
|
||||
.to_string(),
|
||||
),
|
||||
Generator::YearBirth => Value::Number(
|
||||
rng.random_range((REF_YEAR - YEAR_BIRTH_MAX_AGE)..=(REF_YEAR - YEAR_BIRTH_MIN_AGE))
|
||||
.to_string(),
|
||||
),
|
||||
Generator::DateRecent => Value::Text(format_date(random_past_date(rng, 0, RECENT_WINDOW_DAYS))),
|
||||
Generator::DateAdult => {
|
||||
Value::Text(format_date(random_past_date(rng, ADULT_MIN_DAYS, ADULT_MAX_DAYS)))
|
||||
Generator::DateRecent => {
|
||||
Value::Text(format_date(random_past_date(rng, 0, RECENT_WINDOW_DAYS)))
|
||||
}
|
||||
Generator::DateAdult => Value::Text(format_date(random_past_date(
|
||||
rng,
|
||||
ADULT_MIN_DAYS,
|
||||
ADULT_MAX_DAYS,
|
||||
))),
|
||||
Generator::DateTimeRecent => Value::Text(random_recent_datetime(rng)),
|
||||
Generator::Boolean => Value::Bool(rng.random_range(0..2) == 1),
|
||||
Generator::PickFrom(values) if !values.is_empty() => {
|
||||
@@ -232,8 +237,7 @@ fn random_datetime_between(
|
||||
} else {
|
||||
rng.random_range(hi_s..=lo_s)
|
||||
};
|
||||
let dt = chrono::DateTime::from_timestamp(secs, 0)
|
||||
.map_or(lo, |d| d.naive_utc());
|
||||
let dt = chrono::DateTime::from_timestamp(secs, 0).map_or(lo, |d| d.naive_utc());
|
||||
dt.format("%Y-%m-%dT%H:%M:%S").to_string()
|
||||
}
|
||||
|
||||
@@ -294,20 +298,35 @@ fn currency_amount(ty: Type, rng: &mut SeedRng) -> Value {
|
||||
// — the hand-rolled `product` generator (D9) —
|
||||
|
||||
const PRODUCT_ADJECTIVES: &[&str] = &[
|
||||
"Sleek", "Rustic", "Ergonomic", "Handcrafted", "Refined", "Modern",
|
||||
"Vintage", "Compact", "Premium", "Lightweight", "Durable", "Elegant",
|
||||
"Sturdy", "Smooth", "Gorgeous", "Intelligent", "Practical", "Awesome",
|
||||
"Incredible", "Recycled",
|
||||
"Sleek",
|
||||
"Rustic",
|
||||
"Ergonomic",
|
||||
"Handcrafted",
|
||||
"Refined",
|
||||
"Modern",
|
||||
"Vintage",
|
||||
"Compact",
|
||||
"Premium",
|
||||
"Lightweight",
|
||||
"Durable",
|
||||
"Elegant",
|
||||
"Sturdy",
|
||||
"Smooth",
|
||||
"Gorgeous",
|
||||
"Intelligent",
|
||||
"Practical",
|
||||
"Awesome",
|
||||
"Incredible",
|
||||
"Recycled",
|
||||
];
|
||||
const PRODUCT_MATERIALS: &[&str] = &[
|
||||
"Wooden", "Copper", "Granite", "Cotton", "Steel", "Leather", "Bamboo",
|
||||
"Plastic", "Ceramic", "Glass", "Concrete", "Rubber", "Bronze", "Marble",
|
||||
"Linen", "Silk", "Aluminum", "Wool", "Gold", "Carbon",
|
||||
"Wooden", "Copper", "Granite", "Cotton", "Steel", "Leather", "Bamboo", "Plastic", "Ceramic",
|
||||
"Glass", "Concrete", "Rubber", "Bronze", "Marble", "Linen", "Silk", "Aluminum", "Wool", "Gold",
|
||||
"Carbon",
|
||||
];
|
||||
const PRODUCT_NOUNS: &[&str] = &[
|
||||
"Chair", "Lamp", "Table", "Bottle", "Backpack", "Keyboard", "Mug",
|
||||
"Shoes", "Jacket", "Watch", "Wallet", "Bench", "Hat", "Gloves",
|
||||
"Towel", "Ball", "Bike", "Knife", "Pillow", "Blanket",
|
||||
"Chair", "Lamp", "Table", "Bottle", "Backpack", "Keyboard", "Mug", "Shoes", "Jacket", "Watch",
|
||||
"Wallet", "Bench", "Hat", "Gloves", "Towel", "Ball", "Bike", "Knife", "Pillow", "Blanket",
|
||||
];
|
||||
|
||||
fn product_name(rng: &mut SeedRng) -> String {
|
||||
@@ -396,7 +415,9 @@ mod tests {
|
||||
] {
|
||||
let v = gen_once(&generator, Type::Text, 3);
|
||||
match v {
|
||||
Value::Text(s) => assert!(!s.trim().is_empty(), "{generator:?} produced empty text"),
|
||||
Value::Text(s) => {
|
||||
assert!(!s.trim().is_empty(), "{generator:?} produced empty text")
|
||||
}
|
||||
other => panic!("{generator:?} produced non-text {other:?}"),
|
||||
}
|
||||
}
|
||||
@@ -405,18 +426,25 @@ mod tests {
|
||||
#[test]
|
||||
fn email_looks_like_an_email() {
|
||||
let v = gen_once(&Generator::Email, Type::Text, 11);
|
||||
let Value::Text(s) = v else { panic!("not text") };
|
||||
let Value::Text(s) = v else {
|
||||
panic!("not text")
|
||||
};
|
||||
assert!(s.contains('@'), "email should contain @: {s}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn product_name_is_three_capitalised_words() {
|
||||
let v = gen_once(&Generator::ProductName, Type::Text, 99);
|
||||
let Value::Text(s) = v else { panic!("not text") };
|
||||
let Value::Text(s) = v else {
|
||||
panic!("not text")
|
||||
};
|
||||
let words: Vec<&str> = s.split(' ').collect();
|
||||
assert_eq!(words.len(), 3, "product name should be 3 words: {s}");
|
||||
for w in words {
|
||||
assert!(w.chars().next().unwrap().is_ascii_uppercase(), "word `{w}` not capitalised");
|
||||
assert!(
|
||||
w.chars().next().unwrap().is_ascii_uppercase(),
|
||||
"word `{w}` not capitalised"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -429,9 +457,14 @@ mod tests {
|
||||
let latest = reference_date();
|
||||
for _ in 0..200 {
|
||||
let v = generate_value(&Generator::DateRecent, Type::Date, &mut rng);
|
||||
let Value::Text(s) = v else { panic!("date not text") };
|
||||
let Value::Text(s) = v else {
|
||||
panic!("date not text")
|
||||
};
|
||||
let d = NaiveDate::parse_from_str(&s, "%Y-%m-%d").expect("valid ISO date");
|
||||
assert!(d >= earliest && d <= latest, "date {d} outside recent window");
|
||||
assert!(
|
||||
d >= earliest && d <= latest,
|
||||
"date {d} outside recent window"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -446,7 +479,9 @@ mod tests {
|
||||
.unwrap();
|
||||
for _ in 0..200 {
|
||||
let v = generate_value(&Generator::DateAdult, Type::Date, &mut rng);
|
||||
let Value::Text(s) = v else { panic!("date not text") };
|
||||
let Value::Text(s) = v else {
|
||||
panic!("date not text")
|
||||
};
|
||||
let d = NaiveDate::parse_from_str(&s, "%Y-%m-%d").expect("valid ISO date");
|
||||
assert!(d >= earliest && d <= latest, "dob {d} outside adult window");
|
||||
}
|
||||
@@ -455,7 +490,9 @@ mod tests {
|
||||
#[test]
|
||||
fn datetime_is_iso_shaped() {
|
||||
let v = gen_once(&Generator::DateTimeRecent, Type::DateTime, 5);
|
||||
let Value::Text(s) = v else { panic!("not text") };
|
||||
let Value::Text(s) = v else {
|
||||
panic!("not text")
|
||||
};
|
||||
assert!(s.contains('T'), "datetime needs a T separator: {s}");
|
||||
// Parses as a naive datetime.
|
||||
chrono::NaiveDateTime::parse_from_str(&s, "%Y-%m-%dT%H:%M:%S")
|
||||
@@ -467,11 +504,17 @@ mod tests {
|
||||
let Value::Number(int_amt) = gen_once(&Generator::CurrencyAmount, Type::Int, 4) else {
|
||||
panic!("not a number")
|
||||
};
|
||||
assert!(!int_amt.contains('.'), "int currency should be whole: {int_amt}");
|
||||
assert!(
|
||||
!int_amt.contains('.'),
|
||||
"int currency should be whole: {int_amt}"
|
||||
);
|
||||
let Value::Number(dec_amt) = gen_once(&Generator::CurrencyAmount, Type::Decimal, 4) else {
|
||||
panic!("not a number")
|
||||
};
|
||||
assert!(dec_amt.contains('.'), "decimal currency should have cents: {dec_amt}");
|
||||
assert!(
|
||||
dec_amt.contains('.'),
|
||||
"decimal currency should have cents: {dec_amt}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -494,7 +537,10 @@ mod tests {
|
||||
let Value::Text(s) = generate_value(&generator, Type::Text, &mut rng) else {
|
||||
panic!("not text")
|
||||
};
|
||||
assert!(matches!(s.as_str(), "active" | "closed"), "unexpected pick {s}");
|
||||
assert!(
|
||||
matches!(s.as_str(), "active" | "closed"),
|
||||
"unexpected pick {s}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -503,7 +549,10 @@ mod tests {
|
||||
let generator = Generator::PickFrom(vec!["1".into(), "2".into(), "3".into()]);
|
||||
let mut rng = make_rng(Some(6));
|
||||
let v = generate_value(&generator, Type::Int, &mut rng);
|
||||
assert!(matches!(v, Value::Number(_)), "numeric pick should be a Number: {v:?}");
|
||||
assert!(
|
||||
matches!(v, Value::Number(_)),
|
||||
"numeric pick should be a Number: {v:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -517,7 +566,10 @@ mod tests {
|
||||
panic!("YearRecent must be a Number")
|
||||
};
|
||||
let n: i32 = s.parse().unwrap();
|
||||
assert!((1950..=2025).contains(&n), "YearRecent {n} out of [1950,2025]");
|
||||
assert!(
|
||||
(1950..=2025).contains(&n),
|
||||
"YearRecent {n} out of [1950,2025]"
|
||||
);
|
||||
}
|
||||
for _ in 0..300 {
|
||||
let Value::Number(s) = generate_value(&Generator::YearBirth, Type::Int, &mut rng)
|
||||
@@ -525,7 +577,10 @@ mod tests {
|
||||
panic!("YearBirth must be a Number")
|
||||
};
|
||||
let n: i32 = s.parse().unwrap();
|
||||
assert!((1945..=2007).contains(&n), "YearBirth {n} out of [1945,2007]");
|
||||
assert!(
|
||||
(1945..=2007).contains(&n),
|
||||
"YearBirth {n} out of [1945,2007]"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -543,7 +598,10 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn int_range_stays_within_inclusive_bounds() {
|
||||
let g = Generator::Range { low: "10".into(), high: "20".into() };
|
||||
let g = Generator::Range {
|
||||
low: "10".into(),
|
||||
high: "20".into(),
|
||||
};
|
||||
let mut rng = make_rng(Some(5));
|
||||
for _ in 0..200 {
|
||||
let Value::Number(s) = generate_value(&g, Type::Int, &mut rng) else {
|
||||
@@ -556,7 +614,10 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn real_range_stays_within_bounds_and_has_cents() {
|
||||
let g = Generator::Range { low: "1.0".into(), high: "9.0".into() };
|
||||
let g = Generator::Range {
|
||||
low: "1.0".into(),
|
||||
high: "9.0".into(),
|
||||
};
|
||||
let mut rng = make_rng(Some(5));
|
||||
for _ in 0..200 {
|
||||
let Value::Number(s) = generate_value(&g, Type::Real, &mut rng) else {
|
||||
@@ -588,13 +649,19 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn reversed_bounds_are_tolerated() {
|
||||
let g = Generator::Range { low: "20".into(), high: "10".into() };
|
||||
let g = Generator::Range {
|
||||
low: "20".into(),
|
||||
high: "10".into(),
|
||||
};
|
||||
let mut rng = make_rng(Some(1));
|
||||
let Value::Number(s) = generate_value(&g, Type::Int, &mut rng) else {
|
||||
panic!("number")
|
||||
};
|
||||
let n: i64 = s.parse().unwrap();
|
||||
assert!((10..=20).contains(&n), "reversed bounds still produce in-range: {n}");
|
||||
assert!(
|
||||
(10..=20).contains(&n),
|
||||
"reversed bounds still produce in-range: {n}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -603,7 +670,10 @@ mod tests {
|
||||
assert!(range_bounds_reason(Type::Int, "1", "10").is_none());
|
||||
assert!(range_bounds_reason(Type::Real, "1.5", "9.9").is_none());
|
||||
assert!(range_bounds_reason(Type::Date, "2023-01-01", "2024-01-01").is_none());
|
||||
assert!(range_bounds_reason(Type::DateTime, "2023-01-01T00:00:00", "2024-01-01T00:00:00").is_none());
|
||||
assert!(
|
||||
range_bounds_reason(Type::DateTime, "2023-01-01T00:00:00", "2024-01-01T00:00:00")
|
||||
.is_none()
|
||||
);
|
||||
// Non-numeric bound on a numeric column.
|
||||
assert!(range_bounds_reason(Type::Int, "abc", "10").is_some());
|
||||
// A range on a text column is meaningless.
|
||||
@@ -623,14 +693,29 @@ mod tests {
|
||||
#[test]
|
||||
fn generic_fallback_matches_each_type() {
|
||||
let mut rng = make_rng(Some(0));
|
||||
assert!(matches!(generate_value(&Generator::Generic, Type::Text, &mut rng), Value::Text(_)));
|
||||
assert!(matches!(generate_value(&Generator::Generic, Type::Int, &mut rng), Value::Number(_)));
|
||||
assert!(matches!(generate_value(&Generator::Generic, Type::Bool, &mut rng), Value::Bool(_)));
|
||||
assert!(matches!(generate_value(&Generator::Generic, Type::Blob, &mut rng), Value::Null));
|
||||
assert!(matches!(
|
||||
generate_value(&Generator::Generic, Type::Text, &mut rng),
|
||||
Value::Text(_)
|
||||
));
|
||||
assert!(matches!(
|
||||
generate_value(&Generator::Generic, Type::Int, &mut rng),
|
||||
Value::Number(_)
|
||||
));
|
||||
assert!(matches!(
|
||||
generate_value(&Generator::Generic, Type::Bool, &mut rng),
|
||||
Value::Bool(_)
|
||||
));
|
||||
assert!(matches!(
|
||||
generate_value(&Generator::Generic, Type::Blob, &mut rng),
|
||||
Value::Null
|
||||
));
|
||||
// shortid fallback is a valid base58 id.
|
||||
let Value::Text(sid) = generate_value(&Generator::Generic, Type::ShortId, &mut rng) else {
|
||||
panic!("shortid not text")
|
||||
};
|
||||
assert!(crate::dsl::shortid::validate(&sid).is_ok(), "invalid shortid {sid}");
|
||||
assert!(
|
||||
crate::dsl::shortid::validate(&sid).is_ok(),
|
||||
"invalid shortid {sid}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user