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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use std::path::PathBuf;

use miette::Diagnostic;

/// The library-wide error space for d3j.
///
/// Unparsable input and an unknown language are distinguished from merge
/// conflicts at this level so the CLI can map them to a different exit
/// code than a clean merge with unresolved conflicts.
#[derive(Debug, thiserror::Error, Diagnostic)]
pub enum Error {
    #[error("cannot detect language for {}", path.display())]
    #[diagnostic(
        code(d3j::unknown_language),
        help("pass --lang; supported: rust, java, json")
    )]
    UnknownLanguage { path: PathBuf },

    #[error("{} does not parse as {lang}", path.display())]
    #[diagnostic(
        code(d3j::parse),
        help("structural merge requires syntactically valid inputs")
    )]
    Parse { path: PathBuf, lang: String },

    #[error("io error on {}: {source}", path.display())]
    #[diagnostic(code(d3j::io))]
    Io {
        path: PathBuf,
        #[source]
        source: std::io::Error,
    },
}

#[cfg(test)]
mod tests {
    use super::*;

    fn code_of(err: &Error) -> Option<String> {
        err.code().map(|c| c.to_string())
    }

    fn help_of(err: &Error) -> Option<String> {
        err.help().map(|h| h.to_string())
    }

    #[test]
    fn errors_render_with_diagnostic_codes() {
        let err = Error::UnknownLanguage {
            path: "x.zig".into(),
        };
        assert!(err.to_string().contains("x.zig"));
        assert_eq!(code_of(&err).as_deref(), Some("d3j::unknown_language"));
        assert_eq!(
            help_of(&err).as_deref(),
            Some("pass --lang; supported: rust, java, json")
        );
    }

    #[test]
    fn parse_error_renders_path_and_language() {
        let err = Error::Parse {
            path: "x.rs".into(),
            lang: "rust".into(),
        };
        assert!(err.to_string().contains("x.rs"));
        assert!(err.to_string().contains("rust"));
        assert_eq!(code_of(&err).as_deref(), Some("d3j::parse"));
        assert_eq!(
            help_of(&err).as_deref(),
            Some("structural merge requires syntactically valid inputs")
        );
    }

    #[test]
    fn io_error_renders_path_and_source() {
        let source = std::io::Error::other("permission denied");
        let err = Error::Io {
            path: "x.json".into(),
            source,
        };
        let rendered = err.to_string();
        assert!(rendered.contains("x.json"));
        assert!(rendered.contains("permission denied"));
        assert_eq!(code_of(&err).as_deref(), Some("d3j::io"));
        assert!(std::error::Error::source(&err).is_some());
    }
}