1use std::{convert::Infallible, path::PathBuf};
4
5use thiserror::Error;
6use valico::json_schema::{SchemaError, ValidationState};
7
8use crate::common::PRNumber;
9
10pub type Result<T> = std::result::Result<T, PRdocLibError>;
12
13#[allow(missing_docs)]
15#[derive(Error, Debug)]
16pub enum PRdocLibError {
17 #[error("ValidationErrors {0:?}")]
18 IO(std::io::Error),
19
20 #[error("Serde JSON error {0:?}")]
21 SerdeJsonError(serde_json::Error),
22
23 #[error("Serde YAML error {0:?}")]
24 SerdeYamlError(serde_yaml::Error),
25
26 #[error("ValidationErrors {0:?}")]
27 ValidationErrors(ValidationState),
28
29 #[error("Could not find the PRdoc for Pull Request #{0}. Did you forget to create a PRDoc?")]
30 NumberNotFound(PRNumber),
31
32 #[error("PRDoc file already exists: {0}")]
33 FileAlreadyExists(PathBuf),
34
35 #[error("The filename is not valid: {0}")]
36 InvalidFilename(PathBuf),
37
38 #[error("The config is not valid: {0}")]
39 InvalidConfig(PathBuf),
40
41 #[error("No valid config found")]
42 MissingConfig,
43
44 #[error("Template file at {0} was not found")]
45 MissingTemplateFile(PathBuf),
46
47 #[error("No valid file found in {0}")]
48 NoValidFileFound(PathBuf),
49
50 #[error("Some valid files in {0}")]
51 SomeInvalidFiles(PathBuf),
52
53 #[error("Schema error with {0}")]
54 SchemaError(SchemaError),
55
56 #[error("Unknown error")]
58 Unknown,
59}
60
61impl From<std::io::Error> for PRdocLibError {
62 fn from(e: std::io::Error) -> Self {
63 PRdocLibError::IO(e)
64 }
65}
66
67impl From<serde_json::Error> for PRdocLibError {
68 fn from(e: serde_json::Error) -> Self {
69 PRdocLibError::SerdeJsonError(e)
70 }
71}
72
73impl From<serde_yaml::Error> for PRdocLibError {
74 fn from(e: serde_yaml::Error) -> Self {
75 PRdocLibError::SerdeYamlError(e)
76 }
77}
78
79impl From<SchemaError> for PRdocLibError {
80 fn from(e: SchemaError) -> Self {
81 PRdocLibError::SchemaError(e)
82 }
83}
84
85impl From<Infallible> for PRdocLibError {
86 fn from(_value: Infallible) -> Self {
87 PRdocLibError::Unknown
88 }
89}