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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
#![cfg(all(feature = "std", any(unix, windows)))] mod unix; mod windows; use std::collections::HashSet; use crate::error::Error; use crate::format::Format; use crate::grouping::Grouping; use crate::strings::{ DecString, DecimalStr, InfString, InfinityStr, MinString, MinusSignStr, NanStr, NanString, PlusSignStr, PlusString, SepString, SeparatorStr, }; /// <b><u>A key type</u></b>. Represents formats obtained from your operating system. Implements /// [`Format`]. /// /// # Example /// ```rust /// use num_format::SystemLocale; /// /// fn main() { /// let locale = SystemLocale::default().unwrap(); /// println!("My system's default locale is..."); /// println!("{:#?}", &locale); /// /// let available = SystemLocale::available_names().unwrap(); /// println!("My available locale names are..."); /// println!("{:#?}", available); /// /// match SystemLocale::from_name("en_US") { /// Ok(_) => println!("My system has the 'en_US' locale."), /// Err(_) => println!("The 'en_US' locale is not included with my system."), /// } /// } /// ``` /// /// [`Format`]: trait.Format.html #[derive(Clone, Debug, Eq, PartialEq, Hash)] #[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))] pub struct SystemLocale { pub(crate) dec: DecString, pub(crate) grp: Grouping, pub(crate) inf: InfString, pub(crate) min: MinString, pub(crate) name: String, pub(crate) nan: NanString, pub(crate) plus: PlusString, pub(crate) sep: SepString, } impl SystemLocale { /// Same as [`default`]. /// /// [`default`]: struct.SystemLocale.html#method.default pub fn new() -> Result<SystemLocale, Error> { SystemLocale::default() } /// Constucts a [`SystemLocale`] based on your operating system's default locale. /// /// * **Unix-based systems (including macOS)**: The default locale is controlled by "locale /// category environment variables," such as `LANG`, `LC_MONETARY`, `LC_NUMERIC`, and /// `LC_ALL`. For more information, see /// [here](https://www.gnu.org/software/libc/manual/html_node/Locale-Categories.html#Locale-Categories). /// * **Windows**: The default locale is controlled by the regional and language options portion /// of the Control Panel. For more information, see /// [here](https://docs.microsoft.com/en-us/windows/desktop/intl/locales-and-languages). /// /// # Errors /// /// Returns an error if the operating system returned something unexpected (such as a null /// pointer). /// /// [`SystemLocale`]: struct.SystemLocale.html pub fn default() -> Result<SystemLocale, Error> { #[cfg(unix)] return self::unix::new(None); #[cfg(windows)] return self::windows::new(None); #[cfg(not(any(unix, windows)))] unreachable!() } /// Constucts a [`SystemLocale`] from the provided locale name. For a list of locale names /// available on your system, see [`available_names`]. /// /// # Errors /// /// Returns an error if the name provided could not be parsed into a [`SystemLocale`] or if the /// operating system returned something unexpected (such as a null pointer). /// /// [`available_names`]: struct.SystemLocale.html#method.available_names /// [`SystemLocale`]: struct.SystemLocale.html pub fn from_name<S>(name: S) -> Result<SystemLocale, Error> where S: Into<String>, { #[cfg(unix)] return self::unix::new(Some(name.into())); #[cfg(windows)] return self::windows::new(Some(name.into())); #[cfg(not(any(unix, windows)))] unreachable!() } /// Returns a set of the locale names available on your operating system. /// /// * **Unix-based systems (including macOS)**: The underlying implementation uses the /// [`locale`] command. /// * **Windows**: The underlying implementation uses the [`EnumSystemLocalesEx`] function. /// # Errors /// /// Returns an error if the operating system returned something unexpected (such as a null /// pointer). /// /// [`EnumSystemLocalesEx`]: https://docs.microsoft.com/en-us/windows/desktop/api/winnls/nf-winnls-enumsystemlocalesex /// [`locale`]: http://man7.org/linux/man-pages/man1/locale.1.html pub fn available_names() -> Result<HashSet<String>, Error> { #[cfg(unix)] return Ok(self::unix::available_names()); #[cfg(windows)] return self::windows::available_names(); #[cfg(not(any(unix, windows)))] unreachable!() } /// Returns this locale's string representation of a decimal point. pub fn decimal(&self) -> &str { &self.dec } /// Returns this locale's [`Grouping`], which governs how digits are separated /// (see [`Grouping`]). /// /// [`Grouping`]: enum.Grouping.html pub fn grouping(&self) -> Grouping { self.grp } /// Returns this locale's string representation of infinity. pub fn infinity(&self) -> &str { &self.inf } /// Returns this locale's string representation of a minus sign. pub fn minus_sign(&self) -> &str { &self.min } /// Returns this locale's name. pub fn name(&self) -> &str { &self.name } /// Returns this locale's string representation of NaN. pub fn nan(&self) -> &str { &self.nan } /// Returns this locale's string representation of a plus sign. pub fn plus_sign(&self) -> &str { &self.plus } /// Returns this locale's string representation of a thousands separator. pub fn separator(&self) -> &str { &self.sep } #[cfg(unix)] /// Unix-based operating systems (including macOS) do not provide information on how to /// represent infinity symbols; so num-format uses `"∞"` (U+221E) as a default. This /// method allows you to change that default. /// /// # Errors /// /// Returns an error the provided string is longer than 128 bytes. pub fn set_infinity<S>(&mut self, s: S) -> Result<(), Error> where S: AsRef<str>, { self.inf = InfString::new(s)?; Ok(()) } #[cfg(unix)] /// Unix-based operating systems (including macOS) do not provide information on how to /// represent NaN; so num-format uses `"NaN"` as a default. This method allows you to change /// that default. /// /// # Errors /// /// Returns an error the provided string is longer than 64 bytes. pub fn set_nan<S>(&mut self, s: S) -> Result<(), Error> where S: AsRef<str>, { self.nan = NanString::new(s)?; Ok(()) } } impl std::str::FromStr for SystemLocale { type Err = Error; /// Same as [`from_name`]. /// /// [`from_name`]: struct.SystemLocale.html#method.from_name fn from_str(s: &str) -> Result<Self, Self::Err> { SystemLocale::from_name(s) } } impl Format for SystemLocale { #[inline(always)] fn decimal(&self) -> DecimalStr<'_> { DecimalStr::new(self.decimal()).unwrap() } #[inline(always)] fn grouping(&self) -> Grouping { self.grouping() } #[inline(always)] fn infinity(&self) -> InfinityStr<'_> { InfinityStr::new(self.infinity()).unwrap() } #[inline(always)] fn minus_sign(&self) -> MinusSignStr<'_> { MinusSignStr::new(self.minus_sign()).unwrap() } #[inline(always)] fn nan(&self) -> NanStr<'_> { NanStr::new(self.nan()).unwrap() } #[inline(always)] fn plus_sign(&self) -> PlusSignStr<'_> { PlusSignStr::new(self.plus_sign()).unwrap() } #[inline(always)] fn separator(&self) -> SeparatorStr<'_> { SeparatorStr::new(self.separator()).unwrap() } }