mc_launcher/java/
error.rs

1use std::fmt;
2use std::error::Error;
3
4#[derive(Debug, Clone)]
5pub enum JavaErrorKind {
6  NotFound,
7  OutputReadError,
8  InvalidJavaFolderStructure,
9  DifferentVersion,
10}
11
12#[derive(Debug, Clone)]
13pub struct JavaError(JavaErrorKind, Option<String>);
14
15impl JavaError {
16  pub fn new(kind: JavaErrorKind) -> Self {
17    Self(kind, None)
18  }
19
20  pub fn new_with_details(kind: JavaErrorKind, details: String) -> Self {
21    Self(kind, Some(details))
22  }
23
24  pub fn kind(&self) -> &JavaErrorKind {
25    &self.0
26  }
27
28  pub fn details(&self) -> Option<&str> {
29    self.1.as_deref()
30  }
31}
32
33impl fmt::Display for JavaError {
34  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35    match &self.1 {
36      Some(d) => write!(f, "{:?}: {}", self.0, d),
37      None => write!(f, "{:?}", self.0),
38    }
39  }
40}
41
42impl Error for JavaError {}
43
44impl From<JavaError> for std::io::Error {
45  fn from(v: JavaError) -> Self {
46    let kind = match v.0 {
47      JavaErrorKind::NotFound => std::io::ErrorKind::NotFound,
48      JavaErrorKind::OutputReadError => std::io::ErrorKind::InvalidInput,
49      JavaErrorKind::InvalidJavaFolderStructure => std::io::ErrorKind::InvalidData,
50      JavaErrorKind::DifferentVersion => std::io::ErrorKind::Other,
51    };
52
53    Self::new(kind, v)
54  }
55}
56
57pub type Result<T> = std::result::Result<T, JavaError>;