ion/format/
date.rs

1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 */
6
7use std::fmt;
8use std::fmt::{Display, Formatter};
9
10use colored::Colorize as _;
11
12use crate::format::Config;
13use crate::{Context, Date};
14
15/// Formats a [JavaScript Date](Date) using the given [configuration](Config).
16pub fn format_date<'cx>(cx: &'cx Context, cfg: Config, date: &'cx Date<'cx>) -> DateDisplay<'cx> {
17	DateDisplay { cx, date, cfg }
18}
19
20#[must_use]
21pub struct DateDisplay<'cx> {
22	cx: &'cx Context,
23	date: &'cx Date<'cx>,
24	cfg: Config,
25}
26
27impl Display for DateDisplay<'_> {
28	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
29		if let Some(date) = self.date.to_date(self.cx) {
30			date.to_string().color(self.cfg.colours.date).fmt(f)
31		} else {
32			panic!("Failed to unbox Date");
33		}
34	}
35}