zoozle 0.1.7

Some I/O macros like C++ cin/cout.
Documentation
/// Struct which stores .csv file data.
/// You can get the data by "read_string()" or "read_numeric()".
/// If you rewrite sheet by "rewrite()", call "apply()" to apply changes and output data to specified file.
#[derive(Clone,Default,Debug)]
pub struct CsvSheet {
    sheet:Vec<Vec<String>>,
}

impl CsvSheet {

    /// Create instance from .csv file assigned path.
    ///  If the file is not found, returns Err(()).
    pub fn from(path: &str) -> Result<Self,()> {
        let linear;
        if let Ok(handle)= std::fs::read_to_string(path){
            linear=handle;
        }else{
            return Err(())
        }
        
        //let mut x_len: Vec<usize> = Vec::new();
        //let mut y_len = 0;
        //let mut count_x=0;
        let mut sheet:Vec<Vec<String>>=Vec::new();
        let mut buf=String::new();

        for run in linear.chars() {
            match run {
                '\n' |'\r'=> {
                    sheet.push(buf.split(',').map(|s|s.to_string()).collect());
                    buf.clear();
                }
                _=>{
                    buf.push(run);
                }
            }
        }

        if let Some(a)=linear.chars().last(){
            match a{
                '\n'|'\r'=>{

                }
                _=>{
                    sheet.push(buf.split(',').map(|s|s.to_string()).collect());
                }
            }
        }
        
        Ok(
            CsvSheet {
                sheet,
            }
        )
    }

    /// Create empty instance without .csv files.
    /// This function just assigns Vec::new() to CsvSheet.sheet.
    pub fn new()->Self{
        CsvSheet{
            sheet:Vec::new()
        }
    }

    pub fn read_string(&self,x:usize,y:usize)->Option<&String>{
        if let Some(a)=self.sheet.get(y){
            if let Some(b)=a.get(x){
                if b.is_empty(){
                    None
                }else{
                    Some(b)
                }
            }else{
                None
            }
        }else{
            None
        }
    }

    pub fn read_numeric(&self,x:usize,y:usize)->Result<f64,()>{
        if let Some(a)=self.sheet.get(y){
            if let Some(b)=a.get(x){
                if let Ok(num)=b.parse::<f64>(){
                    Ok(num)
                }else{
                    Err(())
                }
            }else{
                Err(())
            }
        }else{
            Err(())
        }
    }

    pub fn width(&self,pos:usize)->Option<usize>{
        if let Some(a)=self.sheet.get(pos){
            Some(a.len())
        }else{
            None
        }
    }

    pub fn height(&self)->usize{
        self.sheet().len()
    }
    
    pub fn line(&self,pos:usize)->Option<&Vec<String>>{
        if let Some(_)=self.sheet.get(pos){
            //Some(a.to_vec())
            Some(&self.sheet[pos])
        }else{
            None
        }
    }

    pub fn sheet(&self)->&Vec<Vec<String>>{
        &self.sheet
    }

    pub fn mut_line(&mut self,pos:usize)->Option<&mut Vec<String>>{
        if let Some(_)=self.sheet.get(pos){
            Some(&mut self.sheet[pos])
        }else{
            None
        }
    } 

    pub fn mut_sheet(&mut self)->&mut Vec<Vec<String>>{
        &mut self.sheet
    }

    /// Rewrite the date.
    /// Don't forget to call apply() before the program ends.
    pub fn rewrite(&mut self,x:usize,y:usize,wh:String){
        if let Some(a)=self.sheet.get_mut(y){
            if let Some(_b)=a.get(x){
                a[x]=wh;
            }else{
                while let None=a.get(x){
                    a.push("".to_string());
                }
                a[x]=wh;
            }
        }else{
            while let None=self.sheet.get_mut(y){
                self.sheet.push(Vec::new());
            }
            if let Some(a)=self.sheet.get_mut(y){
                if let Some(_b)=a.get(x){
                    a[x]=wh;
                }else{
                    while let None=a.get(x){
                        a.push("".to_string());
                    }
                    a[x]=wh;
                }
            }        
        }
    }

    pub fn rewrite_line(&mut self,pos:usize,line:Vec<String>){
        while let None=self.sheet.get_mut(pos){
            self.sheet.push(Vec::new());
        }    
        self.sheet[pos]=line;
    }

    /// Applies changes and output data to .csv file assigned.
    /// Don't forget to call apply() before the program ends.
    pub fn apply(&self,out:&str)->bool{
        let mut linear=String::new();
        for f in &self.sheet{
            for s in f{
                linear.push_str(&s.clone());
                linear.push_str(&",".to_string())
            }
            linear.pop();
            linear.push_str(&"\n".to_string());
        }
        linear.pop();

        if let Ok(_)=std::fs::write(out, linear.as_bytes()){
            true
        }else{
            if let Ok(_h)=std::fs::File::create(out){
                if let Ok(_)=std::fs::write(out, linear.as_bytes()){
                    true
                }else{
                    false
                }
            }else{
                false
            }
            
        }
    }

}