veryl_metadata/
metadata_error.rs

1use crate::metadata::UrlPath;
2use miette::{self, Diagnostic};
3use semver::Version;
4use std::path::{Path, PathBuf};
5use thiserror::Error;
6use url::Url;
7use veryl_path::PathError;
8
9#[derive(Error, Diagnostic, Debug)]
10pub enum MetadataError {
11    #[diagnostic(code(MetadataError::FileIO), help(""))]
12    #[error("file I/O error ({path})")]
13    FileIO {
14        #[source]
15        source: std::io::Error,
16        path: PathBuf,
17    },
18
19    #[diagnostic(code(MetadataError::FileNotFound), help(""))]
20    #[error("Veryl.toml is not found")]
21    FileNotFound,
22
23    #[diagnostic(code(MetadataError::Deserialize), help(""))]
24    #[error("toml load failed")]
25    Deserialize(#[from] toml::de::Error),
26
27    #[diagnostic(code(MetadataError::InvalidSourceLocation), help(""))]
28    #[error("source file \"{0}\" is outside the project")]
29    InvalidSourceLocation(PathBuf),
30
31    #[diagnostic(code(MetadataError::Git), help(""))]
32    #[error("git operation failure: {0}")]
33    Git(Box<dyn std::error::Error + Sync + Send>),
34
35    #[diagnostic(
36        code(MetadataError::InvalidProjectName),
37        help("\"[a-zA-Z_][0-9a-zA-Z_]*\" can be used as project name")
38    )]
39    #[error("project name \"{0}\" is invalid")]
40    InvalidProjectName(String),
41
42    #[diagnostic(
43        code(MetadataError::InvalidProjectName),
44        help("an project name starting with `__` is reserved")
45    )]
46    #[error("project name \"{0}\" is reserved by system")]
47    ReservedProjectName(String),
48
49    #[diagnostic(
50        code(MetadataError::InvalidLicense),
51        help("license text should follow SPDX expression")
52    )]
53    #[error("license parse failed")]
54    InvalidLicense(#[from] spdx::ParseError),
55
56    #[diagnostic(code(MetadataError::PublishedVersion), help("bump up version"))]
57    #[error("\"{0}\" is already published")]
58    PublishedVersion(Version),
59
60    #[diagnostic(code(MetadataError::ModifiedProject), help(""))]
61    #[error("There are modified files in {0}")]
62    ModifiedProject(PathBuf),
63
64    #[diagnostic(code(MetadataError::Toml), help(""))]
65    #[error("toml serialization error")]
66    TomlSer(#[from] toml::ser::Error),
67
68    #[diagnostic(code(MetadataError::VersionNotFound), help(""))]
69    #[error("{version} @ {url} is not found")]
70    VersionNotFound { url: UrlPath, version: String },
71
72    #[diagnostic(code(MetadataError::ProjectNotFound), help(""))]
73    #[error("{project} @ {url} is not found")]
74    ProjectNotFound { url: UrlPath, project: String },
75
76    #[diagnostic(code(MetadataError::InvalidDependency), help(""))]
77    #[error("dependency to {name} is invalid because {cause}")]
78    InvalidDependency { name: String, cause: String },
79
80    #[diagnostic(code(MetadataError::GitSpec), help(""))]
81    #[error("no version/rev/tag/branch specification of {0}")]
82    GitSpec(Url),
83
84    #[diagnostic(code(MetadataError::NameConflict), help(""))]
85    #[error("project name \"{0}\" is used multiply in dependencies")]
86    NameConflict(String),
87
88    #[diagnostic(code(MetadataError::Path), help(""))]
89    #[error("path error")]
90    Path(#[from] PathError),
91}
92
93impl MetadataError {
94    pub fn file_io(source: std::io::Error, path: &Path) -> MetadataError {
95        MetadataError::FileIO {
96            source,
97            path: path.into(),
98        }
99    }
100}