java_runtimes/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use std::fmt::{Display, Formatter};
use std::path::PathBuf;

#[derive(Debug)]
pub struct Error {
    pub(crate) kind: ErrorKind,
}

impl Error {
    pub(crate) fn new(kind: ErrorKind) -> Self {
        Error { kind }
    }
}

#[derive(Debug)]
pub(crate) enum ErrorKind {
    InvalidWorkDir,
    NoJavaVersionStringFound,
    LooksNotLikeJavaExecutableFile(PathBuf),
    JavaOutputFailed(std::io::Error),
    GettingJavaVersionFailed(PathBuf),
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match &self.kind {
            ErrorKind::InvalidWorkDir => write!(f, "Java home directory not found"),
            ErrorKind::NoJavaVersionStringFound => write!(f, "Invalid version string"),
            ErrorKind::LooksNotLikeJavaExecutableFile(path) => {
                write!(
                    f,
                    "Path looks not like a Java executable file [**/bin/java(.exe)] : {}",
                    path.display()
                )
            }
            ErrorKind::JavaOutputFailed(io_err) => {
                write!(f, "Failed to read Java output: {}", io_err)
            }
            ErrorKind::GettingJavaVersionFailed(path) => {
                write!(f, "Failed to get Java version: {}", path.display())
            }
        }
    }
}