use citum_schema::options::{Config, StrongTerminalCommaPolicy};
pub(crate) fn strong_terminal_comma_policy(config: Option<&Config>) -> StrongTerminalCommaPolicy {
config
.and_then(|config| config.punctuation.as_ref())
.and_then(|punctuation| punctuation.strong_terminal_comma_policy)
.unwrap_or_default()
}
pub(crate) fn is_terminal_punctuation(ch: char) -> bool {
matches!(ch, ':' | '.' | ';' | '!' | '?' | ',' | '…')
}
pub(crate) fn is_strong_terminal(ch: char) -> bool {
matches!(ch, '!' | '?' | '…')
}
pub(crate) fn resolve_punctuation_collision(
first: char,
second: char,
strong_terminal_comma_policy: StrongTerminalCommaPolicy,
) -> String {
if second == ','
&& is_strong_terminal(first)
&& strong_terminal_comma_policy == StrongTerminalCommaPolicy::KeepTerminal
{
return first.to_string();
}
match (first, second) {
(':', ':') => ":".to_string(),
('.', ':') => ".:".to_string(),
(';', ':') => ";".to_string(),
('!', ':') => "!".to_string(),
('?', ':') => "?".to_string(),
(',', ':') => ",:".to_string(),
(':', '.') => ":".to_string(),
('.', '.') => ".".to_string(),
(';', '.') => ";".to_string(),
('!', '.') => "!".to_string(),
('?', '.') => "?".to_string(),
(',', '.') => ",.".to_string(),
(':', ';') => ":;".to_string(),
('.', ';') => ".;".to_string(),
(';', ';') => ";".to_string(),
('!', ';') => "!;".to_string(),
('?', ';') => "?;".to_string(),
(',', ';') => ",;".to_string(),
(':', '!') => "!".to_string(),
('.', '!') => ".!".to_string(),
(';', '!') => "!".to_string(),
('!', '!') => "!".to_string(),
('?', '!') => "?!".to_string(),
(',', '!') => ",!".to_string(),
(':', '?') => "?".to_string(),
('.', '?') => ".?".to_string(),
(';', '?') => "?".to_string(),
('!', '?') => "!?".to_string(),
('?', '?') => "?".to_string(),
(',', '?') => ",?".to_string(),
(':', ',') => ":,".to_string(),
('.', ',') => ".,".to_string(),
(';', ',') => ";,".to_string(),
('!', ',') => "!,".to_string(),
('?', ',') => "?,".to_string(),
(',', ',') => ",".to_string(),
_ => format!("{first}{second}"),
}
}