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

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)=s.parse::<i32>(){
                    if n >0 {
                        return  Command::Calc(n);
                    }
                }
                Command::Invild(input.trim().to_string())
            }
        }
    
    }

}

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).with_context(||format!("沒有檔案{}",&self.path))?;
         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_line(&mut content)?;
        Ok(content)
    }
    
}
fn process_calc(num:i32)->(String,i32){
        let square:Vec<_>=(1..=num).map(|x|(x,x*x)).collect();
        let mut sum:i32=square.iter().map(|&(_,sq)|sq).sum();
        let mut record=format!("正函數{}",num);
        for (val,sq) in &square {
            record+=&format!("\n{}的平方數是{}\n",val,sq);
        }
        record+=&format!("總和為{}",sum);

    (record,sum)
}
fn hadle_command(cmd:Command,file_manager:&FileManager)->Result<bool>{
    match cmd {
        Command::Exit=>{
                file_manager.append("使用者輸入exit離開")?;
                println!("再見");
                Ok(false)  
        }
        Command::Show=>{
                    let 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_calc(num);
                    println!("正函數是{}",num);
                    file_manager.append(&record)?;
                    
                    Ok(true)
        }
        Command::Invild(cmd)=>{
                    file_manager.append(&format!("使用者輸入錯誤{}",cmd))?;
                    println!("使用者輸入錯誤{}",cmd);
                   Ok(true)  
        }
    }
}

fn main()->Result<()>{
    let file_manager=FileManager::new("nnnn.txt");
    println!("您好");
    let stdin=io::stdin();
    let mut reader=stdin.lock();
    loop {
        let mut input=String::new();
        io::stdout().flush().unwrap();
        reader.read_line(&mut input).context("輸入錯誤")?;
        let cmd=Command::parse(&input);
    
        if !hadle_command(cmd, &file_manager)?{
            break;
        }
    }
    Ok(())
}
請修正