1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
// This file is part of ICU4X. For terms of use, please see the file // called LICENSE at the top level of the ICU4X source tree // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). use crate::error::Error; use crate::resource::ResourceKey; use crate::resource::ResourcePath; use icu_locid::LanguageIdentifier; use std::borrow::Cow; use std::fmt; use std::fmt::Debug; /// A struct to request a certain piece of data from a data provider. #[derive(Clone, Debug, PartialEq)] pub struct DataRequest { pub resource_path: ResourcePath, } impl fmt::Display for DataRequest { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!( f, "{}/{}", self.resource_path.key, self.resource_path.options ) } } /// Create a [`DataRequest`] to a particular [`ResourceKey`] with default options. impl From<ResourceKey> for DataRequest { fn from(key: ResourceKey) -> Self { Self { resource_path: ResourcePath { key, options: Default::default(), }, } } } impl DataRequest { /// Returns the [`LanguageIdentifier`] for this [`DataRequest`], or an error if it is not present. /// /// # Examples /// /// ``` /// use icu_provider::prelude::*; /// /// const FOO_BAR: ResourceKey = icu_provider::resource_key!(x, "foo", "bar", 1); /// /// let req_no_langid = DataRequest { /// resource_path: ResourcePath { /// key: FOO_BAR, /// options: ResourceOptions::default(), /// } /// }; /// /// let req_with_langid = DataRequest { /// resource_path: ResourcePath { /// key: FOO_BAR, /// options: ResourceOptions { /// variant: None, /// langid: Some(icu_locid_macros::langid!("ar-EG")), /// }, /// } /// }; /// /// assert!(matches!(req_no_langid.try_langid(), Err(DataError::NeedsLanguageIdentifier(_)))); /// assert!(matches!(req_with_langid.try_langid(), Ok(_))); /// ``` pub fn try_langid(&self) -> Result<&LanguageIdentifier, Error> { self.resource_path .options .langid .as_ref() .ok_or_else(|| Error::NeedsLanguageIdentifier(self.clone())) } } /// A response object containing metadata about the returned data. #[derive(Debug, Clone, PartialEq, Default)] pub struct DataResponseMetadata { /// The language of the returned data, or None if the resource key isn't localized. pub data_langid: Option<LanguageIdentifier>, } /// A wrapper around the payload returned in a [`DataResponse`]. /// /// # Examples /// /// ``` /// use icu_provider::prelude::*; /// use std::borrow::Cow; /// /// let payload = DataPayload { cow: Some(Cow::Borrowed("Demo")) }; /// /// assert_eq!("Demo", payload.borrow().expect("Payload is present")); /// ``` #[derive(Debug, Clone)] pub struct DataPayload<'d, T> where T: ToOwned + ?Sized, <T as ToOwned>::Owned: Debug, { pub cow: Option<Cow<'d, T>>, } /// A response object containing an object as payload and metadata about it. #[derive(Debug, Clone)] pub struct DataResponse<'d, T> where T: ToOwned + ?Sized, <T as ToOwned>::Owned: Debug, { /// Metadata about the returned object. pub metadata: DataResponseMetadata, /// The object itself; None if it was not loaded. pub payload: DataPayload<'d, T>, } impl<'d, T> DataPayload<'d, T> where T: ToOwned + ?Sized, <T as ToOwned>::Owned: Debug, { /// Creates a new, empty [`DataPayload`]. /// /// Default is not implemented because it would be misleading: does the DataPayload start /// empty, or does it start with the Default value of T? #[allow(clippy::new_without_default)] pub fn new() -> Self { Self { cow: None } } /// Borrows the underlying payload. Error if not present. pub fn borrow(&self) -> Result<&T, Error> { use std::borrow::Borrow; self.cow .as_ref() .map(|cow| cow.borrow()) .ok_or(Error::MissingPayload) } /// Takes ownership of the underlying payload. Error if not present. pub fn take(&mut self) -> Result<Cow<'d, T>, Error> { self.cow.take().ok_or(Error::MissingPayload) } } /// A generic data provider that loads a payload of a specific type. /// /// See examples on some of the concrete implementations: /// /// - [`HelloWorldProvider`](crate::hello_world::HelloWorldProvider) /// - [`StructProvider`](crate::struct_provider::StructProvider) /// - [`InvariantDataProvider`](crate::inv::InvariantDataProvider) pub trait DataProvider<'d, T> where T: ToOwned + ?Sized, <T as ToOwned>::Owned: Debug, { /// Query the provider for data, returning the result. /// /// Returns [`Ok`] if the request successfully loaded data. If data failed to load, returns an /// Error with more information. fn load_payload(&self, req: &DataRequest) -> Result<DataResponse<'d, T>, Error>; }