spreadsheet_ods/format/
create.rs

1use crate::format::FormatNumberStyle;
2use crate::{
3    ValueFormatBoolean, ValueFormatCurrency, ValueFormatDateTime, ValueFormatNumber,
4    ValueFormatPercentage, ValueFormatTimeDuration,
5};
6use icu_locale_core::Locale;
7
8/// Creates a new number format.
9pub fn create_loc_boolean_format<S: AsRef<str>>(name: S, locale: Locale) -> ValueFormatBoolean {
10    let mut v = ValueFormatBoolean::new_localized(name, locale);
11    v.part_boolean().build();
12    v
13}
14
15/// Creates a new number format.
16pub fn create_loc_number_format<S: AsRef<str>>(
17    name: S,
18    locale: Locale,
19    decimal: u8,
20    grouping: bool,
21) -> ValueFormatNumber {
22    let mut v = ValueFormatNumber::new_localized(name, locale);
23    v.part_number()
24        .min_integer_digits(1)
25        .decimal_places(decimal)
26        .if_then(grouping, |p| p.grouping())
27        .build();
28    v
29}
30
31/// Creates a new number format with a fixed number of decimal places.
32pub fn create_loc_number_format_fixed<S: AsRef<str>>(
33    name: S,
34    locale: Locale,
35    decimal: u8,
36    grouping: bool,
37) -> ValueFormatNumber {
38    let mut v = ValueFormatNumber::new_localized(name, locale);
39    v.part_number()
40        .min_integer_digits(1)
41        .fixed_decimal_places(decimal)
42        .if_then(grouping, |p| p.grouping())
43        .build();
44    v
45}
46
47/// Creates a new percentage format.
48pub fn create_loc_percentage_format<S: AsRef<str>>(
49    name: S,
50    locale: Locale,
51    decimal: u8,
52) -> ValueFormatPercentage {
53    let mut v = ValueFormatPercentage::new_localized(name, locale);
54    v.part_number().decimal_places(decimal).build();
55    v.part_text("%").build();
56    v
57}
58
59/// Creates a new currency format.
60pub fn create_loc_currency_prefix<S1, S2>(
61    name: S1,
62    locale: Locale,
63    symbol_locale: Locale,
64    symbol: S2,
65) -> ValueFormatCurrency
66where
67    S1: AsRef<str>,
68    S2: Into<String>,
69{
70    let mut v = ValueFormatCurrency::new_localized(name, locale);
71    v.part_currency()
72        .locale(symbol_locale)
73        .symbol(symbol)
74        .build();
75    v.part_text(" ").build();
76    v.part_number()
77        .decimal_places(2)
78        .min_decimal_places(2)
79        .grouping()
80        .build();
81    v.part_number()
82        .decimal_places(2)
83        .min_decimal_places(2)
84        .grouping()
85        .build();
86    v
87}
88
89/// Creates a new currency format.
90pub fn create_loc_currency_suffix<S1, S2>(
91    name: S1,
92    locale: Locale,
93    symbol_locale: Locale,
94    symbol: S2,
95) -> ValueFormatCurrency
96where
97    S1: AsRef<str>,
98    S2: Into<String>,
99{
100    let mut v = ValueFormatCurrency::new_localized(name, locale);
101    v.part_number()
102        .decimal_places(2)
103        .min_decimal_places(2)
104        .grouping()
105        .build();
106    v.part_text(" ").build();
107    v.part_currency()
108        .locale(symbol_locale)
109        .symbol(symbol)
110        .build();
111    v
112}
113
114/// Creates a new date format D.M.Y
115pub fn create_loc_date_dmy_format<S: AsRef<str>>(name: S, locale: Locale) -> ValueFormatDateTime {
116    let mut v = ValueFormatDateTime::new_localized(name, locale);
117    v.part_day().style(FormatNumberStyle::Long).build();
118    v.part_text(".").build();
119    v.part_month().style(FormatNumberStyle::Long).build();
120    v.part_text(".").build();
121    v.part_year().style(FormatNumberStyle::Long).build();
122    v
123}
124
125/// Creates a new date format M/D/Y
126pub fn create_loc_date_mdy_format<S: AsRef<str>>(name: S, locale: Locale) -> ValueFormatDateTime {
127    let mut v = ValueFormatDateTime::new_localized(name, locale);
128    v.part_month().style(FormatNumberStyle::Long).build();
129    v.part_text("/").build();
130    v.part_day().style(FormatNumberStyle::Long).build();
131    v.part_text("/").build();
132    v.part_year().style(FormatNumberStyle::Long).build();
133    v
134}
135
136/// Creates a datetime format Y-M-D H:M:S
137pub fn create_loc_datetime_format<S: AsRef<str>>(name: S, locale: Locale) -> ValueFormatDateTime {
138    let mut v = ValueFormatDateTime::new_localized(name, locale);
139    v.part_day().style(FormatNumberStyle::Long).build();
140    v.part_text(".").build();
141    v.part_month().style(FormatNumberStyle::Long).build();
142    v.part_text(".").build();
143    v.part_year().style(FormatNumberStyle::Long).build();
144    v.part_text(" ").build();
145    v.part_hours().style(FormatNumberStyle::Long).build();
146    v.part_text(":").build();
147    v.part_minutes().style(FormatNumberStyle::Long).build();
148    v.part_text(":").build();
149    v.part_seconds().style(FormatNumberStyle::Long).build();
150    v
151}
152
153/// Creates a new time-Duration format H:M:S
154pub fn create_loc_time_format<S: AsRef<str>>(name: S, locale: Locale) -> ValueFormatTimeDuration {
155    let mut v = ValueFormatTimeDuration::new_localized(name, locale);
156    v.part_hours().style(FormatNumberStyle::Long).build();
157    v.part_text(":").build();
158    v.part_minutes().style(FormatNumberStyle::Long).build();
159    v.part_text(":").build();
160    v.part_seconds().style(FormatNumberStyle::Long).build();
161    v
162}
163
164/// Creates a new time-Duration format H:M:S
165pub fn create_loc_time_interval_format<S: AsRef<str>>(
166    name: S,
167    locale: Locale,
168) -> ValueFormatTimeDuration {
169    let mut v = ValueFormatTimeDuration::new_localized(name, locale);
170    v.set_truncate_on_overflow(false);
171
172    v.part_hours().style(FormatNumberStyle::Long).build();
173    v.part_text(":").build();
174    v.part_minutes().style(FormatNumberStyle::Long).build();
175    v.part_text(":").build();
176    v.part_seconds().style(FormatNumberStyle::Long).build();
177    v
178}
179
180/// Creates a new number format.
181pub fn create_boolean_format<S: AsRef<str>>(name: S) -> ValueFormatBoolean {
182    let mut v = ValueFormatBoolean::new_named(name);
183    v.part_boolean().build();
184    v
185}
186
187/// Creates a new number format.
188pub fn create_number_format<S: AsRef<str>>(
189    name: S,
190    decimal: u8,
191    grouping: bool,
192) -> ValueFormatNumber {
193    let mut v = ValueFormatNumber::new_named(name);
194    v.part_number()
195        .decimal_places(decimal)
196        .if_then(grouping, |p| p.grouping())
197        .build();
198    v
199}
200
201/// Creates a new number format with a fixed number of decimal places.
202pub fn create_number_format_fixed<S: AsRef<str>>(
203    name: S,
204    decimal: u8,
205    grouping: bool,
206) -> ValueFormatNumber {
207    let mut v = ValueFormatNumber::new_named(name);
208    v.part_number()
209        .min_integer_digits(1)
210        .fixed_decimal_places(decimal)
211        .if_then(grouping, |p| p.grouping())
212        .build();
213    v
214}
215
216/// Creates a new percentage format.
217pub fn create_percentage_format<S: AsRef<str>>(name: S, decimal: u8) -> ValueFormatPercentage {
218    let mut v = ValueFormatPercentage::new_named(name);
219    v.part_number().fixed_decimal_places(decimal).build();
220    v.part_text("%").build();
221    v
222}
223
224/// Creates a new currency format.
225pub fn create_currency_prefix<S1, S2>(
226    name: S1,
227    symbol_locale: Locale,
228    symbol: S2,
229) -> ValueFormatCurrency
230where
231    S1: AsRef<str>,
232    S2: Into<String>,
233{
234    let mut v = ValueFormatCurrency::new_named(name);
235    v.part_currency()
236        .locale(symbol_locale)
237        .symbol(symbol)
238        .build();
239    v.part_text(" ").build();
240    v.part_number().fixed_decimal_places(2).grouping().build();
241    v
242}
243
244/// Creates a new currency format.
245pub fn create_currency_suffix<S1, S2>(
246    name: S1,
247    symbol_locale: Locale,
248    symbol: S2,
249) -> ValueFormatCurrency
250where
251    S1: AsRef<str>,
252    S2: Into<String>,
253{
254    let mut v = ValueFormatCurrency::new_named(name);
255    v.part_number().fixed_decimal_places(2).grouping().build();
256    v.part_text(" ").build();
257    v.part_currency()
258        .locale(symbol_locale)
259        .symbol(symbol)
260        .build();
261    v
262}
263
264/// Creates a new date format YYYY-MM-DD
265pub fn create_date_iso_format<S: AsRef<str>>(name: S) -> ValueFormatDateTime {
266    let mut v = ValueFormatDateTime::new_named(name);
267    v.part_year().style(FormatNumberStyle::Long).build();
268    v.part_text("-").build();
269    v.part_month().style(FormatNumberStyle::Long).build();
270    v.part_text("-").build();
271    v.part_day().style(FormatNumberStyle::Long).build();
272    v
273}
274
275/// Creates a new date format D.M.Y
276pub fn create_date_dmy_format<S: AsRef<str>>(name: S) -> ValueFormatDateTime {
277    let mut v = ValueFormatDateTime::new_named(name);
278    v.part_day().style(FormatNumberStyle::Long).build();
279    v.part_text(".").build();
280    v.part_month().style(FormatNumberStyle::Long).build();
281    v.part_text(".").build();
282    v.part_year().style(FormatNumberStyle::Long).build();
283    v
284}
285
286/// Creates a new date format M/D/Y
287pub fn create_date_mdy_format<S: AsRef<str>>(name: S) -> ValueFormatDateTime {
288    let mut v = ValueFormatDateTime::new_named(name);
289    v.part_month().style(FormatNumberStyle::Long).build();
290    v.part_text("/").build();
291    v.part_day().style(FormatNumberStyle::Long).build();
292    v.part_text("/").build();
293    v.part_year().style(FormatNumberStyle::Long).build();
294    v
295}
296
297/// Creates a datetime format Y-M-D H:M:S
298pub fn create_datetime_format<S: AsRef<str>>(name: S) -> ValueFormatDateTime {
299    let mut v = ValueFormatDateTime::new_named(name);
300    v.part_year().style(FormatNumberStyle::Long).build();
301    v.part_text("-").build();
302    v.part_month().style(FormatNumberStyle::Long).build();
303    v.part_text("-").build();
304    v.part_day().style(FormatNumberStyle::Long).build();
305    v.part_text(" ").build();
306    v.part_hours().style(FormatNumberStyle::Long).build();
307    v.part_text(":").build();
308    v.part_minutes().style(FormatNumberStyle::Long).build();
309    v.part_text(":").build();
310    v.part_seconds().style(FormatNumberStyle::Long).build();
311    v
312}
313
314/// Creates a new time format H:M:S
315pub fn create_time_of_day_format<S: AsRef<str>>(name: S) -> ValueFormatTimeDuration {
316    let mut v = ValueFormatTimeDuration::new_named(name);
317
318    v.part_hours().style(FormatNumberStyle::Long).build();
319    v.part_text(":").build();
320    v.part_minutes().style(FormatNumberStyle::Long).build();
321    v.part_text(":").build();
322    v.part_seconds().style(FormatNumberStyle::Long).build();
323    v
324}
325
326/// Creates a new time-Duration format H:M:S
327pub fn create_time_interval_format<S: AsRef<str>>(name: S) -> ValueFormatTimeDuration {
328    let mut v = ValueFormatTimeDuration::new_named(name);
329    v.set_truncate_on_overflow(false);
330
331    v.part_hours().style(FormatNumberStyle::Long).build();
332    v.part_text(":").build();
333    v.part_minutes().style(FormatNumberStyle::Long).build();
334    v.part_text(":").build();
335    v.part_seconds().style(FormatNumberStyle::Long).build();
336    v
337}