factorio_belt/core/
error.rs

1//! Error types for BELT.
2
3use std::path::PathBuf;
4use thiserror::Error;
5
6/// All errors than can occur in BELT.
7#[derive(Error, Debug)]
8pub enum BenchmarkError {
9    #[error("Factorio executable not found")]
10    FactorioNotFound,
11
12    #[error("Factorio executable not fund at provided path: {path}")]
13    FactorioNotFoundAtPath { path: PathBuf },
14
15    #[error("Save directory does not exist: {path}")]
16    SaveDirectoryNotFound { path: PathBuf },
17
18    #[error("No save files found matching pattern '{pattern}' in {directory}")]
19    NoSaveFilesFound { pattern: String, directory: PathBuf },
20
21    #[error("Invalid save file: {path} - {reason}")]
22    InvalidSaveFile { path: PathBuf, reason: String },
23
24    #[error("Invalid save file name: {path}")]
25    InvalidSaveFileName { path: PathBuf },
26
27    #[error("Invalid mods file name: {path}")]
28    InvalidModsFileName { path: PathBuf },
29
30    #[error("Invalid UTF-8 in Factorio output")]
31    InvalidUtf8Output,
32
33    #[error("Progress bar template error: {0}")]
34    ProgressBarError(String),
35
36    #[error("Factorio process failed with exit code {code}.")]
37    FactorioProcessFailed { code: i32, hint: Option<String> },
38
39    #[error("No benchmark results found in Factorio output")]
40    NoBenchmarkResults,
41
42    #[error("Failed to parse benchmark output: {reason}")]
43    ParseError { reason: String },
44
45    #[error("Template error: {0}")]
46    TemplateError(#[from] handlebars::RenderError),
47
48    #[error("CSV error: {0}")]
49    CsvError(#[from] csv::Error),
50
51    #[error("IO error: {0}")]
52    IoError(#[from] std::io::Error),
53
54    #[error("Glob error: {0}")]
55    GlobError(#[from] glob::GlobError),
56
57    #[error("Glob pattern error: {0}")]
58    GlobPatternError(#[from] glob::PatternError),
59
60    #[error("JSON Serialization error: {0}")]
61    JsonError(#[from] serde_json::Error),
62
63    #[error("Chart generation error: {0}")]
64    ChartGenerationError(#[from] charming::EchartsError),
65
66    #[error("Failed to create directory: {path}")]
67    DirectoryCreationFailed { path: PathBuf },
68
69    #[error("Invalid run order: {input}. Valid options: sequential, random, grouped")]
70    InvalidRunOrder { input: String },
71}
72
73/// Get a hint for the FactorioProcessFailed error, if it exists
74impl BenchmarkError {
75    pub fn get_hint(&self) -> Option<&str> {
76        if let BenchmarkError::FactorioProcessFailed { hint, .. } = self {
77            hint.as_deref()
78        } else {
79            None
80        }
81    }
82}
83
84/// A convenient result type for BELT
85pub type Result<T> = std::result::Result<T, BenchmarkError>;