es_fluent_shared/resource/
errors.rs1use std::path::{Path, PathBuf};
2
3use crate::CanonicalLanguageIdentifierError;
4use crate::fluent::FluentIdentifierError;
5use crate::namespace::NamespacePathError;
6
7use super::{LocaleRelativeFtlPathError, ResourceKeyError};
8
9#[derive(Clone, Debug, Eq, thiserror::Error, PartialEq)]
11pub enum ResourcePlanError {
12 #[error("invalid Fluent domain '{domain}': {details}")]
14 InvalidDomain {
15 domain: String,
16 details: FluentIdentifierError,
17 },
18 #[error("invalid domain resource key '{key}': {details}")]
20 InvalidResourceKey {
21 key: String,
23 details: ResourceKeyError,
25 },
26 #[error("invalid locale-relative resource path '{path}': {details}")]
28 InvalidResourcePath {
29 path: String,
31 details: LocaleRelativeFtlPathError,
33 },
34 #[error("invalid namespace '{namespace}': {details}")]
36 InvalidNamespace {
37 namespace: String,
38 details: NamespacePathError,
39 },
40}
41
42#[derive(Debug, thiserror::Error)]
44pub enum SparseAssetResourcePlanError {
45 #[error("Failed to read i18n directory at {path:?}: {source}")]
47 ReadAssetsRoot {
48 path: PathBuf,
50 source: std::io::Error,
52 },
53 #[error("Failed to read directory entry in {path:?}: {source}")]
55 ReadAssetsRootEntry {
56 path: PathBuf,
58 source: std::io::Error,
60 },
61 #[error("Failed to read namespace directory {path:?}: {source}")]
63 ReadNamespaceDirectory {
64 path: PathBuf,
66 source: std::io::Error,
68 },
69 #[error("Failed to read directory entry in {path:?}: {source}")]
71 ReadNamespaceDirectoryEntry {
72 path: PathBuf,
74 source: std::io::Error,
76 },
77 #[error("Locale directory {path:?} contains a non-UTF-8 name")]
79 NonUtf8LocaleDirectory {
80 path: PathBuf,
82 },
83 #[error("{}", format_locale_directory_error(raw_name, path, details))]
85 InvalidLocaleDirectory {
86 raw_name: String,
88 path: PathBuf,
90 details: CanonicalLanguageIdentifierError,
92 },
93 #[error("Failed to derive namespace for asset {path:?} relative to {root:?}: {source}")]
95 NamespaceRelativePath {
96 path: PathBuf,
98 root: PathBuf,
100 source: std::path::StripPrefixError,
102 },
103 #[error("Namespace path {path:?} contains non-UTF-8 components")]
105 NonUtf8NamespacePath {
106 path: PathBuf,
108 },
109 #[error("Discovered invalid namespace '{namespace}' in assets for crate '{domain}': {details}")]
111 InvalidNamespace {
112 namespace: String,
114 domain: String,
116 details: NamespacePathError,
118 },
119}
120
121fn format_locale_directory_error(
122 raw_name: &str,
123 path: &Path,
124 details: &CanonicalLanguageIdentifierError,
125) -> String {
126 match details {
127 CanonicalLanguageIdentifierError::Invalid { source, .. } => format!(
128 "Locale directory '{raw_name}' under \"{}\" is not a valid BCP-47 identifier: {source}",
129 path.display()
130 ),
131 CanonicalLanguageIdentifierError::IcuInvalid { details, .. } => format!(
132 "Locale directory '{raw_name}' under \"{}\" could not be parsed as an ICU locale: {details}",
133 path.display()
134 ),
135 CanonicalLanguageIdentifierError::NonCanonical { canonical, .. } => format!(
136 "Locale directory '{raw_name}' under \"{}\" must use canonical BCP-47 form '{canonical}'",
137 path.display()
138 ),
139 }
140}