use std::io::{Write,self,Read,BufRead,BufReader};
use std::fs::{File,OpenOptions};
use anyhow::{Result,Context};

enum Command{
    Exit,
    Show,
    Calc(i32),
    Invild(String),  
}
impl Command {
    fn parse(input:&str)->Self{
        let s=input.trim().to_lowercase();
        match s.as_str() {
            "exit"=>Command::Exit,
            "show"=>Command::Show,
            _=>{
                if let Ok(n) =input.parse::<i32>(){
                    if n>0 {
                        return Command::Calc(n);
                    }
                }
                Command::Invild(input.trim().to_string())
            }
        }
    }
}
fn process_clac(num:i32)->(String,i32){
    let square:Vec<_>=(0..num).map(|x|(x,x*x)).collect();
    let mut sum:i32=square.iter().map(|&(_,sq)|sq).sum();
    let mut recorcd=format!("正整數{}",num);
    for (val,sq) in &square {
        recorcd+=&format!("{}的平方根是{}\n",val,sq);
        println!("{}的平方根是{}",val,sq);
    }
    recorcd+=&format!("總和是{}",sum);
    (recorcd,sum)
}
struct FileManager{
    path:String,
}
impl FileManager{
    fn new(path:&str)->Self{
        Self { path: path.to_string() }
    }
    fn append(&self,content:&str)->Result<()>{
        let mut file=OpenOptions::new()
                    .append(true)
                    .create(true)
                    .open(&self.path)
                    .unwrap();
        write!(file,"{}",content)?;
        Ok(())
    }
    fn read_all(&self)->Result<String>{
        let mut file=File::open(&self.path).with_context(||format!("{}",&self.path))?;
        let mut content=String::new();
        let mut buf_reader=BufReader::new(file);
        buf_reader.read_to_string(&mut content)?;
        Ok(content)
    }

}
fn hadle_filemanager(cmd:Command,file_manager:&FileManager)->Result<bool>{
    match cmd {
        Command::Exit=>{
                        println!("再見");
                        file_manager.append(&format!("再見"))?;
                        Ok(false)
        }
        Command::Show=>{
                        let mut content=file_manager.read_all()?;
                        if content.trim().is_empty(){
                            println!("檔案為空");
                        }else {
                            println!("======");
                            println!("{}",content);
                            println!("======");
                        };
                        Ok(true)
        }
        Command::Calc(num)=>{
                            let (record,sum) =process_clac(num);
                            println!("正整{}",num);
                            println!("總和{}",sum);
                            file_manager.append(&record)?;

                            Ok(true)
        }
        Command::Invild(cmd)=>{
                            println!("使用者輸入錯誤{}",cmd);
                            file_manager.append(&format!("使用者輸入錯誤{}",cmd))?;

                            Ok(true)
        }
                        
    }
}
fn main()->Result<()>{
    let file_manager=FileManager::new("ddd.txt");
    let stdin=io::stdin();
    let mut reader=stdin.lock();
    println!("歡迎");
    loop {
        let mut input=String::new();
        io::stdout().flush().unwrap();
        let mut reader=reader.read_line(&mut input)?;
        let cmd=Command::parse(&input);
        if !hadle_filemanager(cmd, &file_manager)?{
            break;
        }
    }
    Ok(())
}
請問哪裡有問題,我輸入數字出現使用者錯誤輸入