yfunc_rust/
func.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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(())
}