Skip to content

Commit 6fa140f

Browse files
committed
Merge branch 'ayabust-feat/error-handling'
2 parents c30a27e + e19991b commit 6fa140f

20 files changed

Lines changed: 613 additions & 233 deletions

src/error.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//! Module d'erreurs personnalisées pour GSP
2+
//!
3+
//! Ce module définit les types d'erreurs utilisés dans l'ensemble du projet.
4+
5+
use std::fmt;
6+
use std::io;
7+
8+
/// Type d'erreur personnalisé pour GSP
9+
#[derive(Debug)]
10+
#[allow(dead_code)]
11+
pub enum GspError {
12+
/// Erreur lors de la récupération du texte
13+
TextRetrieval(String),
14+
/// Erreur d'entrée/sortie
15+
Io(io::Error),
16+
/// Erreur de traduction
17+
Translation(String),
18+
/// Erreur TTS (Text-To-Speech)
19+
Tts(String),
20+
/// Erreur de processus
21+
Process(String),
22+
/// Erreur de fichier
23+
File(String),
24+
/// Erreur de configuration
25+
Config(String),
26+
}
27+
28+
impl fmt::Display for GspError {
29+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30+
match self {
31+
GspError::TextRetrieval(msg) => write!(f, "Erreur de récupération de texte: {}", msg),
32+
GspError::Io(err) => write!(f, "Erreur I/O: {}", err),
33+
GspError::Translation(msg) => write!(f, "Erreur de traduction: {}", msg),
34+
GspError::Tts(msg) => write!(f, "Erreur TTS: {}", msg),
35+
GspError::Process(msg) => write!(f, "Erreur de processus: {}", msg),
36+
GspError::File(msg) => write!(f, "Erreur de fichier: {}", msg),
37+
GspError::Config(msg) => write!(f, "Erreur de configuration: {}", msg),
38+
}
39+
}
40+
}
41+
42+
impl std::error::Error for GspError {}
43+
44+
impl From<io::Error> for GspError {
45+
fn from(err: io::Error) -> Self {
46+
GspError::Io(err)
47+
}
48+
}
49+
50+
/// Alias de Result utilisant GspError
51+
pub type GspResult<T> = Result<T, GspError>;

src/input/clipboard.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
1+
//! Module de lecture du presse-papiers
2+
//!
3+
//! Implémentation du trait InputEngine pour le presse-papiers.
4+
15
use super::InputEngine;
26
use cli_clipboard::{ClipboardContext, ClipboardProvider};
37

8+
/// Lecteur du presse-papiers
49
pub struct Clipboard {}
510

611
impl InputEngine for Clipboard {
712
fn input(&self) -> String {
8-
let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap();
13+
let mut ctx: ClipboardContext = match ClipboardProvider::new() {
14+
Ok(ctx) => ctx,
15+
Err(e) => {
16+
eprintln!("Erreur lors de l'accès au presse-papiers: {}", e);
17+
return String::new();
18+
}
19+
};
920

10-
ctx.get_contents().unwrap()
21+
match ctx.get_contents() {
22+
Ok(contents) => contents,
23+
Err(e) => {
24+
eprintln!("Erreur lors de la lecture du presse-papiers: {}", e);
25+
String::new()
26+
}
27+
}
1128
}
1229
}

src/input/ocr/cuneiform.rs

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,57 @@
1+
//! Module de reconnaissance OCR avec Cuneiform
2+
//!
3+
//! Interface pour l'outil de reconnaissance Cuneiform.
4+
15
use std::process::Command;
26

3-
// cuneiform -l fra /tmp/screenshot.png
7+
/// Exécute Cuneiform OCR sur une image
8+
///
9+
/// # Arguments
10+
/// * `screenshooter` - Chemin vers le fichier image
11+
/// * `lang` - Code de langue
12+
///
13+
/// # Retour
14+
/// Le texte reconnu, ou une chaîne vide en cas d'erreur
415
pub fn cuneiform(screenshooter: &str, lang: &str) -> String {
5-
let screenshot = "/dev/shm/screenshot.txt";
16+
let screenshot_output = "/dev/shm/screenshot.txt";
617

7-
let mut child = Command::new("cuneiform")
18+
let mut child = match Command::new("cuneiform")
819
.arg("-l")
920
.arg(lang)
1021
.arg("-o")
11-
.arg(screenshot)
22+
.arg(screenshot_output)
1223
.arg(screenshooter)
1324
.spawn()
14-
.expect("failed to execute process");
15-
child.wait().expect("failed to wait on child");
25+
{
26+
Ok(child) => child,
27+
Err(e) => {
28+
eprintln!("Erreur lors de l'exécution de Cuneiform: {}", e);
29+
return String::new();
30+
}
31+
};
32+
33+
if let Err(e) = child.wait() {
34+
eprintln!("Erreur lors de l'attente de Cuneiform: {}", e);
35+
return String::new();
36+
}
1637

17-
let output = std::fs::read_to_string(screenshot)
18-
.unwrap()
19-
.trim()
20-
.to_string()
21-
.replace(['\n', '\r'], " ");
38+
match std::fs::read_to_string(screenshot_output) {
39+
Ok(content) => {
40+
let output = content.trim().to_string().replace(['\n', '\r'], " ");
2241

23-
std::fs::remove_file(screenshot).unwrap();
42+
// Nettoyage du fichier temporaire
43+
if let Err(e) = std::fs::remove_file(screenshot_output) {
44+
eprintln!(
45+
"Avertissement: impossible de supprimer le fichier temporaire: {}",
46+
e
47+
);
48+
}
2449

25-
output
50+
output
51+
}
52+
Err(e) => {
53+
eprintln!("Erreur lors de la lecture du résultat Cuneiform: {}", e);
54+
String::new()
55+
}
56+
}
2657
}

src/input/ocr/mod.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//! Module de reconnaissance optique de caractères (OCR)
2+
//!
3+
//! Fournit une interface unifiée pour les différents moteurs OCR.
4+
15
mod cuneiform;
26
mod screenshot;
37
mod tesseract;
@@ -6,6 +10,7 @@ use self::{cuneiform::cuneiform, tesseract::tesseract};
610
use super::InputEngine;
711
use which::which;
812

13+
/// Configuration pour la reconnaissance OCR
914
pub struct Ocr {
1015
pub lang: String,
1116
}
@@ -20,10 +25,16 @@ impl InputEngine for Ocr {
2025
} else if which("cuneiform").is_ok() {
2126
input = cuneiform(&screenshooter, &self.lang);
2227
} else {
23-
println!("Aucun outil de reconnaissance d'écriture n'est installé");
28+
eprintln!("Aucun outil de reconnaissance d'écriture n'est installé");
2429
}
2530

26-
std::fs::remove_file(screenshooter).unwrap();
31+
// Nettoyage du fichier temporaire avec gestion d'erreur
32+
if let Err(e) = std::fs::remove_file(&screenshooter) {
33+
eprintln!(
34+
"Avertissement: impossible de supprimer le fichier temporaire: {}",
35+
e
36+
);
37+
}
2738

2839
input
2940
}
Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1+
//! Module de capture d'écran avec gnome-screenshot
2+
//!
3+
//! Interface pour l'outil de capture gnome-screenshot.
4+
15
use std::process::{Command, Stdio};
26

3-
// gnome-screenshot --area --file=/tmp/screenshot.png
7+
/// Capture une zone de l'écran avec gnome-screenshot
8+
///
9+
/// # Arguments
10+
/// * `screenshooter` - Chemin où sauvegarder la capture
411
pub fn gnome_screenshot_area(screenshooter: &str) {
5-
Command::new("gnome-screenshot")
12+
let result = Command::new("gnome-screenshot")
613
.arg("--area")
714
.arg(format!("--file={}", screenshooter))
815
.stdout(Stdio::piped())
9-
.output()
10-
.expect("failed to execute process");
16+
.output();
17+
18+
if let Err(e) = result {
19+
eprintln!(
20+
"Erreur lors de la capture d'écran (gnome-screenshot): {}",
21+
e
22+
);
23+
}
1124
}

src/input/ocr/screenshot/mod.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
1+
//! Module de capture d'écran pour l'OCR
2+
//!
3+
//! Fournit une interface unifiée pour les différents outils de capture.
4+
15
mod gnome_screenshot;
26
mod xfce4_screenshooter;
37

48
use self::xfce4_screenshooter::xfce4_screenshooter_region;
59
use which::which;
610

11+
/// Capture d'écran pour l'OCR
712
pub struct Screenshot {
813
pub path: String,
914
}
1015

16+
impl Default for Screenshot {
17+
fn default() -> Self {
18+
Self::new()
19+
}
20+
}
21+
1122
impl Screenshot {
23+
/// Crée une nouvelle configuration de capture d'écran
1224
pub fn new() -> Self {
1325
Screenshot {
1426
path: "/dev/shm/screenshot.png".to_string(),
1527
}
1628
}
1729

30+
/// Capture une région de l'écran et retourne le chemin du fichier
1831
pub fn capture(&self) -> String {
1932
if which("xfce4-screenshooter").is_ok() {
2033
xfce4_screenshooter_region(&self.path);
2134
} else if which("gnome-screenshot").is_ok() {
2235
gnome_screenshot::gnome_screenshot_area(&self.path);
2336
} else {
24-
panic!("No screenshot tool found");
37+
eprintln!("Aucun outil de capture d'écran trouvé");
2538
}
2639

2740
self.path.clone()
Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1+
//! Module de capture d'écran avec xfce4-screenshooter
2+
//!
3+
//! Interface pour l'outil de capture xfce4-screenshooter.
4+
15
use std::process::{Command, Stdio};
26

3-
// Take a screenshot of a region of the screen
4-
// xfce4-screenshooter --region --save /tmp/screenshot.png
7+
/// Capture une région de l'écran avec xfce4-screenshooter
8+
///
9+
/// # Arguments
10+
/// * `screenshooter` - Chemin où sauvegarder la capture
511
pub fn xfce4_screenshooter_region(screenshooter: &str) {
6-
Command::new("xfce4-screenshooter")
12+
let result = Command::new("xfce4-screenshooter")
713
.arg("--region")
814
.arg("--save")
915
.arg(screenshooter)
1016
.stdout(Stdio::piped())
11-
.output()
12-
.expect("failed to execute process");
17+
.output();
18+
19+
if let Err(e) = result {
20+
eprintln!(
21+
"Erreur lors de la capture d'écran (xfce4-screenshooter): {}",
22+
e
23+
);
24+
}
1325
}

src/input/ocr/tesseract.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1+
//! Module de reconnaissance OCR avec Tesseract
2+
//!
3+
//! Interface pour l'outil de reconnaissance Tesseract.
4+
15
use std::process::{Command, Stdio};
26

3-
// tesseract /tmp/screenshot.png -l fra stdout
7+
/// Exécute Tesseract OCR sur une image
8+
///
9+
/// # Arguments
10+
/// * `screenshooter` - Chemin vers le fichier image
11+
/// * `lang` - Code de langue (ex: "fr-FR", "en-US")
12+
///
13+
/// # Retour
14+
/// Le texte reconnu, ou une chaîne vide en cas d'erreur
415
pub fn tesseract(screenshooter: &str, lang: &str) -> String {
516
let lang = match lang {
617
"de-DE" => "deu",
@@ -11,16 +22,18 @@ pub fn tesseract(screenshooter: &str, lang: &str) -> String {
1122
_ => "eng",
1223
};
1324

14-
let command = Command::new("tesseract")
25+
match Command::new("tesseract")
1526
.arg(screenshooter)
1627
.arg("stdout")
1728
.arg("-l")
1829
.arg(lang)
1930
.stderr(Stdio::null())
2031
.output()
21-
.expect("failed to execute process");
22-
23-
let stdout = String::from_utf8_lossy(&command.stdout);
24-
25-
stdout.to_string()
32+
{
33+
Ok(output) => String::from_utf8_lossy(&output.stdout).to_string(),
34+
Err(e) => {
35+
eprintln!("Erreur lors de l'exécution de Tesseract: {}", e);
36+
String::new()
37+
}
38+
}
2639
}

src/input/selection.rs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,46 @@
1+
//! Module de lecture de la sélection X11
2+
//!
3+
//! Implémentation du trait InputEngine pour la sélection X11.
4+
15
use crate::input::InputEngine;
26
use std::time::Duration;
37
use x11_clipboard::Clipboard;
48

9+
/// Lecteur de la sélection X11
510
pub struct Selection {}
611

712
impl InputEngine for Selection {
813
fn input(&self) -> String {
9-
let clipboard = Clipboard::new().unwrap();
14+
let clipboard = match Clipboard::new() {
15+
Ok(cb) => cb,
16+
Err(e) => {
17+
eprintln!(
18+
"Erreur lors de l'initialisation du presse-papiers X11: {}",
19+
e
20+
);
21+
return String::new();
22+
}
23+
};
1024

11-
let selection = clipboard
12-
.load(
13-
clipboard.setter.atoms.primary,
14-
clipboard.setter.atoms.utf8_string,
15-
clipboard.setter.atoms.property,
16-
Duration::from_secs(3),
17-
)
18-
.unwrap();
25+
let selection = match clipboard.load(
26+
clipboard.setter.atoms.primary,
27+
clipboard.setter.atoms.utf8_string,
28+
clipboard.setter.atoms.property,
29+
Duration::from_secs(3),
30+
) {
31+
Ok(s) => s,
32+
Err(e) => {
33+
eprintln!("Erreur lors de la lecture de la sélection X11: {}", e);
34+
return String::new();
35+
}
36+
};
1937

20-
String::from_utf8(selection).unwrap()
38+
match String::from_utf8(selection) {
39+
Ok(s) => s,
40+
Err(e) => {
41+
eprintln!("Erreur lors de la conversion UTF-8: {}", e);
42+
String::new()
43+
}
44+
}
2145
}
2246
}

0 commit comments

Comments
 (0)