Compare commits
2 Commits
736c6bd564
...
5bbf7ca107
| Author | SHA1 | Date | |
|---|---|---|---|
| 5bbf7ca107 | |||
| bcdd487eec |
@@ -23,13 +23,36 @@ const ITEMS: &[&str] = &[
|
|||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct DescCtx {
|
struct DescCtx {
|
||||||
chambers: Vec<ChamberDesc>,
|
sentences: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
/// Pick "a" or "an" based on the first letter — keeps the prose reading
|
||||||
struct ChamberDesc {
|
/// naturally without giving away that chamber names are YAML keys.
|
||||||
name: String,
|
fn article(word: &str) -> &'static str {
|
||||||
items: Vec<String>,
|
let first = word.chars().next().map(|c| c.to_ascii_lowercase());
|
||||||
|
if matches!(first, Some('a') | Some('e') | Some('i') | Some('o') | Some('u')) {
|
||||||
|
"an"
|
||||||
|
} else {
|
||||||
|
"a"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Join the item list as English: `a sword`, `a sword and a shield`,
|
||||||
|
/// `a sword, a shield, and a potion` (Oxford comma for 3+).
|
||||||
|
fn join_items(items: &[&str]) -> String {
|
||||||
|
let parts: Vec<String> = items
|
||||||
|
.iter()
|
||||||
|
.map(|i| format!("{} {}", article(i), i))
|
||||||
|
.collect();
|
||||||
|
match parts.as_slice() {
|
||||||
|
[] => String::new(),
|
||||||
|
[one] => one.clone(),
|
||||||
|
[a, b] => format!("{a} and {b}"),
|
||||||
|
rest => {
|
||||||
|
let (last, head) = rest.split_last().unwrap();
|
||||||
|
format!("{}, and {}", head.join(", "), last)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Level for DictList {
|
impl Level for DictList {
|
||||||
@@ -48,7 +71,7 @@ impl Level for DictList {
|
|||||||
CHAMBERS.choose_multiple(&mut rng, n).copied().collect();
|
CHAMBERS.choose_multiple(&mut rng, n).copied().collect();
|
||||||
|
|
||||||
let mut inner = Mapping::new();
|
let mut inner = Mapping::new();
|
||||||
let mut desc_chambers = Vec::new();
|
let mut sentences = Vec::new();
|
||||||
for name in &chamber_names {
|
for name in &chamber_names {
|
||||||
let item_n = rng.gen_range(2..=3);
|
let item_n = rng.gen_range(2..=3);
|
||||||
let items: Vec<&'static str> =
|
let items: Vec<&'static str> =
|
||||||
@@ -58,10 +81,13 @@ impl Level for DictList {
|
|||||||
.map(|i| Value::String((*i).to_string()))
|
.map(|i| Value::String((*i).to_string()))
|
||||||
.collect();
|
.collect();
|
||||||
inner.insert(Value::String((*name).to_string()), Value::Sequence(seq));
|
inner.insert(Value::String((*name).to_string()), Value::Sequence(seq));
|
||||||
desc_chambers.push(ChamberDesc {
|
|
||||||
name: (*name).to_string(),
|
let be = if items.len() == 1 { "is" } else { "are" };
|
||||||
items: items.iter().map(|s| s.to_string()).collect(),
|
sentences.push(format!(
|
||||||
});
|
"There {be} {} inside {} {name}.",
|
||||||
|
join_items(&items),
|
||||||
|
article(name),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut top = Mapping::new();
|
let mut top = Mapping::new();
|
||||||
@@ -76,18 +102,13 @@ impl Level for DictList {
|
|||||||
let mut d = Describer::new();
|
let mut d = Describer::new();
|
||||||
d.register(
|
d.register(
|
||||||
"l05",
|
"l05",
|
||||||
"Several chambers branch off, each with its own inventory:\n\
|
"Several chambers branch off, each with its own contents:\n\
|
||||||
{% for c in chambers %}\n{{ c.name }}:{% for it in c.items %}\n - {{ it }}{% endfor %}\n{% endfor %}\n\
|
{% for s in sentences %}\n {{ s }}{% endfor %}\n\n\
|
||||||
💡 Wrap the whole tree under a `chambers:` key — a dict of lists.",
|
💡 Wrap the whole tree under a `chambers:` key — a dict of lists.",
|
||||||
)
|
)
|
||||||
.expect("register template");
|
.expect("register template");
|
||||||
let description = d
|
let description = d
|
||||||
.render(
|
.render("l05", &DescCtx { sentences })
|
||||||
"l05",
|
|
||||||
&DescCtx {
|
|
||||||
chambers: desc_chambers,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.expect("render template");
|
.expect("render template");
|
||||||
|
|
||||||
Generated {
|
Generated {
|
||||||
|
|||||||
@@ -88,5 +88,7 @@ pub fn registry() -> Vec<Box<dyn Level>> {
|
|||||||
Box::new(l01_minimum::Minimum),
|
Box::new(l01_minimum::Minimum),
|
||||||
Box::new(l02_kv::KeyValue),
|
Box::new(l02_kv::KeyValue),
|
||||||
Box::new(l03_dict::Dict),
|
Box::new(l03_dict::Dict),
|
||||||
|
Box::new(l04_list::List),
|
||||||
|
Box::new(l05_dict_list::DictList),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user