const TAG: char = '\u{E0000}';
const SUFFIX: &str = " \u{E0000}";
pub fn bypass(text: &str, last_wire: Option<&str>) -> String {
if last_wire == Some(text) {
format!("{text}{SUFFIX}")
} else {
text.to_string()
}
}
pub fn strip(text: &str) -> &str {
let t = text.strip_suffix(TAG).unwrap_or(text);
if t.len() == text.len() {
return text;
}
t.strip_suffix(' ').unwrap_or(t)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn first_and_different_sends_go_plain() {
assert_eq!(bypass("hi", None), "hi");
assert_eq!(bypass("hi", Some("yo")), "hi");
}
#[test]
fn repeat_gets_suffix_and_toggles_back() {
let a = bypass("hi", Some("hi"));
assert_eq!(a, "hi \u{E0000}");
assert_eq!(bypass("hi", Some(&a)), "hi");
}
#[test]
fn strip_handles_spaced_and_bare_suffix() {
assert_eq!(strip("hi \u{E0000}"), "hi");
assert_eq!(strip("hi\u{E0000}"), "hi");
assert_eq!(strip("hi"), "hi");
assert_eq!(strip("hi "), "hi ");
assert_eq!(strip("\u{E0000}"), "");
}
}