1use std::fmt::{Display, Formatter};
2use std::path::PathBuf;
3
4#[derive(Debug)]
5pub struct Error {
6 pub(crate) kind: ErrorKind,
7}
8
9impl Error {
10 pub(crate) fn new(kind: ErrorKind) -> Self {
11 Error { kind }
12 }
13}
14
15#[derive(Debug)]
16pub(crate) enum ErrorKind {
17 InvalidWorkDir,
18 NoJavaVersionStringFound,
19 LooksNotLikeJavaExecutableFile(PathBuf),
20 JavaOutputFailed(std::io::Error),
21 GettingJavaVersionFailed(PathBuf),
22}
23
24impl Display for Error {
25 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
26 match &self.kind {
27 ErrorKind::InvalidWorkDir => write!(f, "Java home directory not found"),
28 ErrorKind::NoJavaVersionStringFound => write!(f, "Invalid version string"),
29 ErrorKind::LooksNotLikeJavaExecutableFile(path) => {
30 write!(
31 f,
32 "Path looks not like a Java executable file [**/bin/java(.exe)] : {}",
33 path.display()
34 )
35 }
36 ErrorKind::JavaOutputFailed(io_err) => {
37 write!(f, "Failed to read Java output: {}", io_err)
38 }
39 ErrorKind::GettingJavaVersionFailed(path) => {
40 write!(f, "Failed to get Java version: {}", path.display())
41 }
42 }
43 }
44}