use crate::charset::{
CharSet, builtin, code_points, is_punctuation, locale_names,
};
use crate::judge::{Checker, Config};
use crate::render::{Format, Listed, Listing, SetKind, listing};
use crate::rules;
use std::collections::{HashMap, HashSet};
const PUNCTUATION: &str = "one locale's punctuation, from CLDR";
pub(crate) struct Asked<'a> {
pub names: &'a [String],
pub locales: bool,
pub containing: Option<&'a str>,
}
pub(crate) fn sets(
config: &Config,
format: Format,
asked: &Asked<'_>,
) -> Result<String, String> {
let point = asked.containing.map(point_of).transpose()?;
let all = every_set(config)?;
let entries = entries(&all);
let holds = |item: &&Listed<'_>| point.is_none_or(|p| item.set.contains(p));
let chosen = chosen(&entries, asked)?.into_iter().filter(holds);
let counted = asked.names.is_empty() && !asked.locales && point.is_none();
let locales = entries.iter().filter(|e| e.kind == SetKind::Locale);
let unlisted_locales = if counted { locales.count() } else { 0 };
let sets = chosen.map(copied).collect();
let answer = Listing {
sets,
containing: point,
unlisted_locales,
};
Ok(listing(format, &answer))
}
fn every_set(config: &Config) -> Result<Vec<CharSet>, String> {
let checker = Checker::listing(config)?;
let family = rules::resolve("", &config.rules()?, &rules::matches).family;
checker.declared(&family)
}
fn copied<'a>(item: &Listed<'a>) -> Listed<'a> {
Listed {
set: item.set,
kind: item.kind,
description: item.description,
}
}
fn point_of(asked: &str) -> Result<char, String> {
let text = code_points(asked).unwrap_or_else(|| asked.to_owned());
let mut chars = text.chars();
match (chars.next(), chars.next()) {
(Some(point), None) => Ok(point),
_ => Err(format!(
"--containing takes one character or U+XXXX, not `{asked}`"
)),
}
}
fn entries(all: &[CharSet]) -> Vec<Listed<'_>> {
let by_name: HashMap<&str, &CharSet> =
all.iter().map(|set| (set.name.as_str(), set)).collect();
let find = |name: &str| by_name.get(name).copied();
let presets = builtin::described().filter_map(|(name, said)| {
Some(entry(find(name)?, SetKind::Preset, Some(said)))
});
let mut out: Vec<Listed<'_>> = presets.collect();
out.extend(own(all).map(|set| entry(set, SetKind::Declared, None)));
let locales = locale_names().filter_map(find);
out.extend(locales.map(|set| entry(set, SetKind::Locale, said(set))));
out
}
fn said(set: &CharSet) -> Option<&'static str> {
is_punctuation(&set.name).then_some(PUNCTUATION)
}
fn own(all: &[CharSet]) -> impl Iterator<Item = &CharSet> {
let builtin = builtin::described().map(|(name, _)| name);
let known: HashSet<&str> = locale_names().chain(builtin).collect();
all.iter()
.filter(move |set| !known.contains(set.name.as_str()))
}
const fn entry<'a>(
set: &'a CharSet,
kind: SetKind,
description: Option<&'a str>,
) -> Listed<'a> {
Listed {
set,
kind,
description,
}
}
fn chosen<'e, 'a>(
entries: &'e [Listed<'a>],
asked: &Asked<'_>,
) -> Result<Vec<&'e Listed<'a>>, String> {
if !asked.names.is_empty() {
return named(entries, asked.names);
}
let wanted = |kind: SetKind| match (asked.locales, asked.containing) {
(true, _) => kind == SetKind::Locale,
(false, Some(_)) => true,
(false, None) => kind != SetKind::Locale,
};
Ok(entries.iter().filter(|e| wanted(e.kind)).collect())
}
fn named<'e, 'a>(
entries: &'e [Listed<'a>],
names: &[String],
) -> Result<Vec<&'e Listed<'a>>, String> {
let mut out: Vec<&Listed<'a>> = Vec::new();
for name in names {
let found = entries.iter().find(|entry| entry.set.name == *name);
let found = found.ok_or_else(|| format!("unknown set `{name}`"))?;
if !out.iter().any(|kept| kept.set.name == *name) {
out.push(found);
}
}
Ok(out)
}
#[cfg(test)]
#[path = "listing_test.rs"]
mod tests;