use std::{collections::HashSet, hash::Hash, io::Write};
use crate::prelude::*;
pub trait Unique {
fn unique(&self) -> Self;
}
impl<T> Unique for Vec<T> where T: Eq+Hash+Clone {
fn unique(&self) -> Self {
let mut appear: HashSet<T> = HashSet::new();
let mut ret: Vec<T> = vec![];
for x in self {
if !appear.contains(x) {
ret.push(x.clone());
appear.insert(x.clone());
}
}
ret
}
}
pub trait VariableFormat {
fn Aabb(&self) -> String;
}
impl VariableFormat for String {
fn Aabb(&self) -> String {
let mut c = self.chars();
match c.next() {
Some(first_char) => first_char.to_uppercase().collect::<String>() + c.as_str().to_lowercase().as_str(),
None => String::new(),
}
}
}
pub fn write_str_to_stdout(s: &str) -> YRes<()> {
write!(std::io::stdout(), "{}", s).map_err(|e|
err!("failed to output string").trace(
ctx!("write str to stdout: write! failed", e)
)
)?;
std::io::stdout().flush().map_err(|e|
err!("failed to output string").trace(
ctx!("write str to stdout -> flush stdout: std::io::stdout().flush failed", e)
)
)?;
Ok(())
}
pub fn write_bytes_to_stdout(b: &Vec<u8>) -> YRes<()> {
std::io::stdout().write_all(&b).map_err(|e|
err!("failed to output bytes").trace(
ctx!("write bytes to stdout: std::io::stdout().write_all failed", e)
)
)?;
std::io::stdout().flush().map_err(|e|
err!("failed to output bytes").trace(
ctx!("write bytes to stdout -> flush stdout: std::io::stdout().flush failed", e)
)
)?;
Ok(())
}