1use serde::Serialize;
2use std::fs::File;
3use std::io::{self, BufRead};
4use std::path::Path;
5
6pub fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
7where
8 P: AsRef<Path>,
9{
10 let file = File::open(filename)?;
11 Ok(io::BufReader::new(file).lines())
12}
13pub fn to_unicode(domain: &str) -> (String, String) {
16 match idna::domain_to_unicode(domain) {
17 (s, Ok(_)) => (s, String::new()),
18 (s, Err(e)) => (s, e.to_string()),
19 }
20}
21
22pub fn to_ascii(domain: &str) -> (String, String) {
23 match idna::domain_to_ascii(domain) {
24 Ok(s) => (s, String::new()),
25 Err(e) => (String::new(), e.to_string()),
26 }
27}
28
29pub fn process(domain: &str, decode: bool) -> (String, String) {
30 if decode {
31 to_unicode(domain)
32 } else {
33 to_ascii(domain)
34 }
35}
36
37pub fn print_csv_result(domain: &str, decode: bool) {
38 let (converted, errors) = process(domain, decode);
39 println!("{domain:?},{converted:?},{errors:?}");
40}
41
42#[derive(Debug, Serialize)]
43pub struct Domain {
44 ascii: String,
45 unicode: String,
46 errors: String,
47}
48
49impl Domain {
50 pub fn new(domain: &str, decode: bool) -> Domain {
51 let (converted, errors) = process(domain, decode);
52 if decode {
53 Domain {
54 ascii: domain.to_string(),
55 unicode: converted,
56 errors,
57 }
58 } else {
59 Domain {
60 ascii: converted,
61 unicode: domain.to_string(),
62 errors,
63 }
64 }
65 }
66}