Skip to main content

rs_convert2ascii/
lib.rs

1use std::borrow::Cow;
2use std::io;
3
4use io::BufWriter;
5use io::Write;
6
7use io::BufRead;
8
9pub const PLACE_HOLDER_DEFAULT: &str = "[?]";
10
11pub struct Placeholder(pub String);
12
13impl Default for Placeholder {
14    fn default() -> Self {
15        Self(PLACE_HOLDER_DEFAULT.into())
16    }
17}
18
19impl Placeholder {
20    pub fn deunicode<'a>(&self, original: &'a str) -> Cow<'a, str> {
21        deunicode::deunicode_with_tofu_cow(original, self.0.as_str())
22    }
23}
24
25impl Placeholder {
26    pub fn converted2writer<W>(&self, original: &str, wtr: &mut W) -> Result<(), io::Error>
27    where
28        W: Write,
29    {
30        let converted: &str = &self.deunicode(original);
31        writeln!(wtr, "{converted}")
32    }
33
34    pub fn write_all<I, W>(&self, original: I, mut wtr: W) -> Result<(), io::Error>
35    where
36        I: Iterator<Item = Result<String, io::Error>>,
37        W: Write,
38    {
39        for rline in original {
40            let line: String = rline?;
41            self.converted2writer(&line, &mut wtr)?;
42        }
43        wtr.flush()
44    }
45
46    pub fn stdin2converted2writer(&self) -> Result<(), io::Error> {
47        let il = io::stdin().lock();
48        let lines = il.lines();
49
50        let o = io::stdout();
51        let mut ol = o.lock();
52
53        self.write_all(lines, BufWriter::new(&mut ol))?;
54
55        ol.flush()
56    }
57}