errors.rs (2158B)
1 use fluent_bundle::FluentError; 2 use fluent_fallback::types::ResourceId; 3 use std::error::Error; 4 use unic_langid::LanguageIdentifier; 5 6 #[derive(Debug, Clone, PartialEq)] 7 pub enum L10nRegistryError { 8 FluentError { 9 resource_id: ResourceId, 10 loc: Option<(usize, usize)>, 11 error: FluentError, 12 }, 13 MissingResource { 14 locale: LanguageIdentifier, 15 resource_id: ResourceId, 16 }, 17 } 18 19 impl std::fmt::Display for L10nRegistryError { 20 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 21 match self { 22 Self::MissingResource { 23 locale, 24 resource_id, 25 } => { 26 write!( 27 f, 28 "Missing resource in locale {}: {}", 29 locale, resource_id.value 30 ) 31 } 32 Self::FluentError { 33 resource_id, 34 loc, 35 error, 36 } => { 37 if let Some(loc) = loc { 38 write!( 39 f, 40 "Fluent Error in {}[line: {}, col: {}]: {}", 41 resource_id.value, loc.0, loc.1, error 42 ) 43 } else { 44 write!(f, "Fluent Error in {}: {}", resource_id.value, error) 45 } 46 } 47 } 48 } 49 } 50 51 impl Error for L10nRegistryError {} 52 53 #[derive(Debug, Clone, PartialEq)] 54 pub enum L10nRegistrySetupError { 55 RegistryLocked, 56 DuplicatedSource { name: String }, 57 MissingSource { name: String }, 58 } 59 60 impl std::fmt::Display for L10nRegistrySetupError { 61 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 62 match self { 63 Self::RegistryLocked => write!(f, "Can't modify a registry when locked."), 64 Self::DuplicatedSource { name } => { 65 write!(f, "Source with a name {} is already registered.", &name) 66 } 67 Self::MissingSource { name } => { 68 write!(f, "Cannot find a source with a name {}.", &name) 69 } 70 } 71 } 72 } 73 74 impl Error for L10nRegistrySetupError {}