2024-12-06 14:59:59 +01:00
|
|
|
use std::env;
|
|
|
|
use std::fs;
|
2024-12-06 23:43:35 +01:00
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
mod nombres;
|
|
|
|
|
|
|
|
enum ErreurSophie {
|
|
|
|
CommandeInconnue(String),
|
|
|
|
PhraseVide,
|
|
|
|
ManqueArgument,
|
|
|
|
OrthographeNombre(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for ErreurSophie {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
Self::CommandeInconnue(commande) => write!(f, "La commande \"{}\" est inconnue.", commande),
|
|
|
|
Self::PhraseVide => write!(f, "La phrase est vide."),
|
|
|
|
Self::ManqueArgument => write!(f, "Il manque un argument."),
|
|
|
|
Self::OrthographeNombre(nombre) => write!(f, "Le nombre \"{}\" est mal orthographié.", nombre),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-12-06 14:59:59 +01:00
|
|
|
|
|
|
|
fn main() {
|
2024-12-06 23:43:35 +01:00
|
|
|
let arguments: Vec<String> = env::args().collect();
|
|
|
|
|
|
|
|
if arguments.len() < 2 {
|
|
|
|
eprintln!("Utilisation : sophie <FILE>");
|
|
|
|
return
|
|
|
|
}
|
2024-12-06 14:59:59 +01:00
|
|
|
|
2024-12-06 23:43:35 +01:00
|
|
|
let chemin_de_fichier = &arguments[1];
|
2024-12-06 14:59:59 +01:00
|
|
|
|
2024-12-06 23:43:35 +01:00
|
|
|
match fs::read_to_string(chemin_de_fichier) {
|
|
|
|
Ok(contenu) => {
|
|
|
|
execute(contenu);
|
|
|
|
}
|
|
|
|
Err(raison) => {
|
|
|
|
eprintln!("Fichier illisible : {}", raison);
|
|
|
|
}
|
|
|
|
}
|
2024-12-06 14:59:59 +01:00
|
|
|
}
|
2024-12-06 23:43:35 +01:00
|
|
|
|
|
|
|
fn execute(contenu: String) {
|
|
|
|
let contenu_propre = contenu.replace("\n", "");
|
|
|
|
let mut texte: Vec<&str> = contenu_propre.split('.').collect();
|
|
|
|
texte.pop(); // remove empty phrase after last dot
|
|
|
|
for (index_phrase, phrase) in texte.iter().enumerate() {
|
|
|
|
match execute_phrase(phrase) {
|
|
|
|
Ok(_) => {},
|
|
|
|
Err(raison) => {
|
|
|
|
eprintln!("Erreur phrase {} : {}", index_phrase + 1, raison);
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn execute_phrase(phrase: &str) -> Result<(), ErreurSophie> {
|
|
|
|
let phrase = phrase.trim();
|
|
|
|
let parties: Vec<&str> = phrase.splitn(2, ' ').collect();
|
|
|
|
|
|
|
|
if parties.is_empty() {
|
|
|
|
return Err(ErreurSophie::PhraseVide)
|
|
|
|
}
|
|
|
|
|
|
|
|
if parties.len() == 1 {
|
|
|
|
return Err(ErreurSophie::ManqueArgument)
|
|
|
|
}
|
|
|
|
|
|
|
|
match parties[0] {
|
|
|
|
"Modifie" => {
|
|
|
|
modifie(parties[1])?;
|
|
|
|
},
|
|
|
|
"Affiche" => {
|
|
|
|
affiche(parties[1])?;
|
|
|
|
},
|
|
|
|
"Demande" => {
|
|
|
|
demande(parties[1])?;
|
|
|
|
}
|
|
|
|
autre_commande => {
|
|
|
|
return Err(ErreurSophie::CommandeInconnue(autre_commande.to_string()))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn modifie(arguments: &str) -> Result<(), ErreurSophie> {
|
|
|
|
println!("- modifie : {}", arguments);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn affiche(arguments: &str) -> Result<(), ErreurSophie> {
|
|
|
|
let liste_arguments: Vec<&str> = arguments.split(',').collect();
|
|
|
|
|
|
|
|
let mut texte = "".to_string();
|
|
|
|
|
|
|
|
for argument in liste_arguments {
|
|
|
|
let argument: &str = argument.trim();
|
|
|
|
if argument.starts_with('"') {
|
|
|
|
if argument.ends_with('"') {
|
|
|
|
texte += &argument[1..argument.len()-1];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let resultat = nombres::operation(argument)?;
|
|
|
|
texte += &nombres::nombre_comme_texte(resultat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
println!("{}", texte);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn demande(arguments: &str) -> Result<(), ErreurSophie> {
|
|
|
|
println!("- demande : {}", arguments);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
|