idna-cli 0.3.5

Encode/decode Unicode domain names to/from IDNA ASCII
Documentation
use serde::Serialize;
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;

/**
# Errors

Return an error if the given filename path fails to open for reading
*/
pub fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where
    P: AsRef<Path>,
{
    let file = File::open(filename)?;
    Ok(io::BufReader::new(file).lines())
}
// https://doc.rust-lang.org/rust-by-example/std_misc/file/read_lines.html#a-more-efficient-approach

#[must_use]
pub fn to_unicode(domain: &str) -> (String, String) {
    match idna::domain_to_unicode(domain) {
        (s, Err(e)) => (s, e.to_string()),
        (s, _) => (s, String::new()),
    }
}

#[must_use]
pub fn to_ascii(domain: &str) -> (String, String) {
    match idna::domain_to_ascii(domain) {
        Ok(s) => (s, String::new()),
        Err(e) => (String::new(), e.to_string()),
    }
}

#[must_use]
pub fn process(domain: &str, decode: bool) -> (String, String) {
    if decode {
        to_unicode(domain)
    } else {
        to_ascii(domain)
    }
}

pub fn print_csv_result(domain: &str, decode: bool) {
    let (converted, errors) = process(domain, decode);
    println!("{domain:?},{converted:?},{errors:?}");
}

#[derive(Debug, Serialize)]
pub struct Domain {
    ascii: String,
    unicode: String,
    errors: String,
}

impl Domain {
    #[must_use]
    pub fn new(domain: &str, decode: bool) -> Domain {
        let (converted, errors) = process(domain, decode);
        if decode {
            Domain {
                ascii: domain.to_string(),
                unicode: converted,
                errors,
            }
        } else {
            Domain {
                ascii: converted,
                unicode: domain.to_string(),
                errors,
            }
        }
    }
}