ion/format/
regexp.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::Context;
13use crate::format::Config;
14use crate::object::RegExp;
15
16/// Formats a [RegExp object](RegExp) using the given [configuration](Config).
17pub fn format_regexp<'cx>(cx: &'cx Context, cfg: Config, regexp: &'cx RegExp<'cx>) -> RegExpDisplay<'cx> {
18	RegExpDisplay { cx, regexp, cfg }
19}
20
21#[must_use]
22pub struct RegExpDisplay<'cx> {
23	cx: &'cx Context,
24	regexp: &'cx RegExp<'cx>,
25	cfg: Config,
26}
27
28impl Display for RegExpDisplay<'_> {
29	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
30		self.regexp.to_string(self.cx)?.color(self.cfg.colours.regexp).fmt(f)
31	}
32}