af_core/fmt/
surround.rs

1// Copyright © 2021 Alexandra Frydl
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7use super::*;
8
9/// A wrapper returned from [`surround()`] that displays a value surrounded by
10/// a custom prefix and suffix.
11pub struct Surrounded<'a, T>(&'a str, T, &'a str);
12
13/// Wraps a value so that it displays with the given prefix and suffix strings.
14pub fn surround<'a, T>(prefix: &'a str, value: T, suffix: &'a str) -> Surrounded<'a, T> {
15  Surrounded(prefix, value, suffix)
16}
17
18impl<T: Debug> Debug for Surrounded<'_, T> {
19  fn fmt(&self, f: &mut Formatter) -> Result {
20    write!(f, "{}", self.0)?;
21    self.1.fmt(f)?;
22    write!(f, "{}", self.2)
23  }
24}
25
26impl<T: Display> Display for Surrounded<'_, T> {
27  fn fmt(&self, f: &mut Formatter) -> Result {
28    write!(f, "{}", self.0)?;
29    self.1.fmt(f)?;
30    write!(f, "{}", self.2)
31  }
32}