veryl_metadata/
metadata_error.rs1use 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(
20 code(MetadataError::FileNotFound),
21 help("Veryl.toml is searched from the given path toward the root directory")
22 )]
23 #[error("Veryl.toml is not found from \"{0}\"")]
24 FileNotFound(PathBuf),
25
26 #[diagnostic(code(MetadataError::Deserialize), help(""))]
27 #[error("toml load failed")]
28 Deserialize(#[from] toml::de::Error),
29
30 #[diagnostic(code(MetadataError::InvalidSourceLocation), help(""))]
31 #[error("source file \"{0}\" is outside the project")]
32 InvalidSourceLocation(PathBuf),
33
34 #[diagnostic(
35 code(MetadataError::ReservedSourceDir),
36 help("remove it from [build] sources")
37 )]
38 #[error(
39 "\"{0}\" cannot be used as a source directory: `examples/` is reserved and analyzed automatically for the root project"
40 )]
41 ReservedSourceDir(PathBuf),
42
43 #[diagnostic(code(MetadataError::Git), help(""))]
44 #[error("git operation failure: {0}")]
45 Git(Box<dyn std::error::Error + Sync + Send>),
46
47 #[diagnostic(
48 code(MetadataError::InvalidProjectName),
49 help("\"[a-zA-Z_][0-9a-zA-Z_]*\" can be used as project name")
50 )]
51 #[error("project name \"{0}\" is invalid")]
52 InvalidProjectName(String),
53
54 #[diagnostic(
55 code(MetadataError::InvalidProjectName),
56 help("an project name starting with `__` is reserved")
57 )]
58 #[error("project name \"{0}\" is reserved by system")]
59 ReservedProjectName(String),
60
61 #[diagnostic(
62 code(MetadataError::InvalidLicense),
63 help("license text should follow SPDX expression")
64 )]
65 #[error("license parse failed")]
66 InvalidLicense(#[from] spdx::ParseError),
67
68 #[diagnostic(code(MetadataError::PublishedVersion), help("bump up version"))]
69 #[error("\"{0}\" is already published")]
70 PublishedVersion(Version),
71
72 #[diagnostic(code(MetadataError::ModifiedProject), help(""))]
73 #[error("There are modified files in {0}")]
74 ModifiedProject(PathBuf),
75
76 #[diagnostic(code(MetadataError::Toml), help(""))]
77 #[error("toml serialization error")]
78 TomlSer(#[from] toml::ser::Error),
79
80 #[diagnostic(code(MetadataError::VersionNotFound), help(""))]
81 #[error("{version} @ {url} is not found")]
82 VersionNotFound { url: UrlPath, version: String },
83
84 #[diagnostic(code(MetadataError::ProjectNotFound), help(""))]
85 #[error("{project} @ {url} is not found")]
86 ProjectNotFound { url: UrlPath, project: String },
87
88 #[diagnostic(
89 code(MetadataError::UnpublishedDependency),
90 help(
91 "run `veryl publish` in the dependency to create Veryl.pub; for local development, a path dependency (`{name} = {{ path = \"...\" }}`) does not require publishing"
92 )
93 )]
94 #[error("dependency `{name}` has no published release (Veryl.pub not found in the repository)")]
95 UnpublishedDependency { name: String },
96
97 #[diagnostic(code(MetadataError::InvalidDependency), help(""))]
98 #[error("dependency to {name} is invalid because {cause}")]
99 InvalidDependency { name: String, cause: String },
100
101 #[diagnostic(code(MetadataError::GitSpec), help(""))]
102 #[error("no version/rev/tag/branch specification of {0}")]
103 GitSpec(Url),
104
105 #[diagnostic(code(MetadataError::NameConflict), help(""))]
106 #[error("project name \"{0}\" is used multiply in dependencies")]
107 NameConflict(String),
108
109 #[diagnostic(code(MetadataError::Path), help(""))]
110 #[error("path error")]
111 Path(#[from] PathError),
112
113 #[diagnostic(code(MetadataError::MissingVersion), help(""))]
114 #[error("Version field is required in Veryl.toml to publish")]
115 MissingVersion,
116
117 #[diagnostic(code(MetadataError::UnknownProperty), help(""))]
118 #[error("property \"{property}\" is not defined in project \"{project}\"")]
119 UnknownProperty { property: String, project: String },
120
121 #[diagnostic(code(MetadataError::MismatchType), help(""))]
122 #[error("\"{name}\" is expected to \"{expected}\", but it is \"{actual}\"")]
123 MismatchType {
124 name: String,
125 expected: String,
126 actual: String,
127 },
128}
129
130impl MetadataError {
131 pub fn file_io(source: std::io::Error, path: &Path) -> MetadataError {
132 MetadataError::FileIO {
133 source,
134 path: path.into(),
135 }
136 }
137}