use crate::err::Error;
use serde::Deserialize;
use std::collections::HashSet;
const REGISTRY: &[(&str, &str)] = &[
("blind75", include_str!("../../data/sets/blind75.toml")),
(
"neetcode150",
include_str!("../../data/sets/neetcode150.toml"),
),
(
"neetcode250",
include_str!("../../data/sets/neetcode250.toml"),
),
(
"neetcode-all",
include_str!("../../data/sets/neetcode-all.toml"),
),
(
"top-interview-150",
include_str!("../../data/sets/top-interview-150.toml"),
),
(
"top-100-liked",
include_str!("../../data/sets/top-100-liked.toml"),
),
(
"leetcode-75",
include_str!("../../data/sets/leetcode-75.toml"),
),
("google", include_str!("../../data/sets/google.toml")),
("facebook", include_str!("../../data/sets/facebook.toml")),
("amazon", include_str!("../../data/sets/amazon.toml")),
("microsoft", include_str!("../../data/sets/microsoft.toml")),
];
#[derive(Clone, Debug, Deserialize)]
pub struct ProblemSet {
pub slug: String,
pub name: String,
pub description: String,
pub source_url: String,
pub source_license: String,
pub source_as_of: String,
pub generated_at: String,
pub problems: Vec<SetProblem>,
}
#[derive(Clone, Debug, Deserialize)]
pub struct SetProblem {
pub fid: i32,
pub slug: String,
}
impl ProblemSet {
pub fn fids(&self) -> HashSet<i32> {
self.problems.iter().map(|problem| problem.fid).collect()
}
}
pub fn slugs() -> Vec<&'static str> {
REGISTRY.iter().map(|(slug, _)| *slug).collect()
}
pub fn get(slug: &str) -> Result<ProblemSet, Error> {
let raw = REGISTRY
.iter()
.find(|(key, _)| *key == slug)
.map(|(_, raw)| *raw)
.ok_or_else(|| Error::UnknownSet(slug.to_string()))?;
toml::from_str(raw).map_err(|source| Error::SetData {
slug: slug.to_string(),
source,
})
}
pub fn all() -> Result<Vec<ProblemSet>, Error> {
slugs().into_iter().map(get).collect()
}
#[cfg(test)]
mod tests {
use super::*;
const EXPECTED_COUNTS: &[(&str, usize)] = &[
("blind75", 75),
("neetcode150", 150),
("neetcode250", 250),
("neetcode-all", 450),
("top-interview-150", 150),
("top-100-liked", 100),
("leetcode-75", 75),
("google", 488),
("facebook", 371),
("amazon", 592),
("microsoft", 363),
];
#[test]
fn every_bundled_set_parses() {
for slug in slugs() {
get(slug).unwrap_or_else(|e| panic!("set `{slug}` failed to parse: {e}"));
}
}
#[test]
fn registry_keys_are_unique() {
let unique: HashSet<&str> = slugs().into_iter().collect();
assert_eq!(unique.len(), slugs().len(), "duplicate slug in REGISTRY");
}
#[test]
fn registry_key_matches_the_slug_inside_the_file() {
for slug in slugs() {
assert_eq!(get(slug).unwrap().slug, slug);
}
}
#[test]
fn set_members_are_unique_by_both_fid_and_slug() {
for slug in slugs() {
let set = get(slug).unwrap();
let fids: HashSet<i32> = set.problems.iter().map(|p| p.fid).collect();
let problem_slugs: HashSet<&str> =
set.problems.iter().map(|p| p.slug.as_str()).collect();
assert_eq!(fids.len(), set.problems.len(), "{slug}: duplicate fid");
assert_eq!(
problem_slugs.len(),
set.problems.len(),
"{slug}: duplicate problem slug"
);
}
}
#[test]
fn sets_have_their_expected_size() {
for (slug, expected) in EXPECTED_COUNTS {
assert_eq!(
get(slug).unwrap().problems.len(),
*expected,
"{slug} changed size; regenerate and review before updating this number"
);
}
assert_eq!(
EXPECTED_COUNTS.len(),
slugs().len(),
"a set has no expected count"
);
}
#[test]
fn set_metadata_is_populated() {
for slug in slugs() {
let set = get(slug).unwrap();
assert!(!set.name.is_empty(), "{slug}: no name");
assert!(!set.description.is_empty(), "{slug}: no description");
assert!(
set.source_url.starts_with("https://"),
"{slug}: source_url is not a URL"
);
assert!(!set.source_license.is_empty(), "{slug}: no source_license");
assert!(!set.source_as_of.is_empty(), "{slug}: no source_as_of");
}
}
#[test]
fn unknown_slug_is_rejected() {
assert!(matches!(get("nope"), Err(Error::UnknownSet(_))));
}
#[test]
fn the_neetcode_lists_nest() {
let blind = get("blind75").unwrap().fids();
let hundred_fifty = get("neetcode150").unwrap().fids();
let two_fifty = get("neetcode250").unwrap().fids();
assert!(
blind.is_subset(&hundred_fifty),
"NeetCode 150 is supposed to extend Blind 75"
);
assert!(
hundred_fifty.is_subset(&two_fifty),
"NeetCode 250 is supposed to extend NeetCode 150"
);
}
}