Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code shared from the Rust Playground
enum Cell {
Text(String),
Number(f64),
Formula(String),
Empty,
}
impl Cell {
pub fn cell_text(&self) -> String {
match self {
Cell::Text(string) => cell_text_spaces(string),
Cell::Number(number) => cell_text_spaces(&number.to_string()),
Cell::Formula(string) => cell_text_spaces(self.get_value()),
Cell::Empty => " ".repeat(10).to_string(),
}
}
pub fn full_text(&self) -> String {
match self {
Cell::Text(string) => format!("\"{}\"", string),
Cell::Number(number) => number.to_string(),
Cell::Formula(string)=> string.clone(),
Cell::Empty => String::from(" "),
}
}
pub fn get_value(&self) -> Option<f64> {
match self {
Cell::Number(number) => Some(*number),
Cell::Formula(string) => Some(string_to_f64(string)),
Cell::Text(_) => None,
Cell::Empty => None,
}
}
}
fn cell_text_spaces(string: &str) -> String {
let mut spaces = String::new();
for _i in 0..10 - (string.len() as i32) {
spaces.push(' ');
}
if string.len() > 10 {
string[0..10].parse().unwrap()
} else {
format!("{}{}", &string, spaces)
}
}
fn string_to_f64(string: &str) -> f64 {
let mut input = &string[1..string.len()-1];
match input.parse::<f64>(){
Ok(input) => input,
Err(_)=> 0.0,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.