pub trait ToCharset {
fn to_chars(&self) -> Vec<char>;
}
impl ToCharset for String {
fn to_chars(&self) -> Vec<char> {
self.chars().into_iter().collect()
}
}
impl ToCharset for &str {
fn to_chars(&self) -> Vec<char> {
self.chars().into_iter().collect()
}
}
#[derive(Debug)]
pub struct Charset {
length: usize,
body: Vec<char>,
}
impl Charset {
pub fn new<T>(charset: T) -> Option<Self>
where T: ToCharset {
let chars = charset.to_chars();
let len = chars.len();
if len == 0 {
return None
}
Some( Self {
length: len,
body: chars,
})
}
pub fn length(&self) -> usize {
self.length
}
pub fn borrow_body(&self) -> &Vec<char> {
&self.body
}
pub fn clone_body(&self) -> Vec<char> {
self.body.clone()
}
pub fn to_string(&self) -> String {
let mut result = String::new();
for ch in self.body.iter() {
result.push(*ch)
}
result
}
}