Skip to main content

linera_version/serde_pretty/
type.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4/// A wrapper that serializes a value of type `T` through a more human-readable
5/// representation type `Repr`.
6#[derive(Debug, Clone, PartialEq, Eq, Hash)]
7pub struct Pretty<T, Repr> {
8    /// The wrapped value.
9    pub value: T,
10    _phantom: std::marker::PhantomData<fn(Repr) -> Repr>,
11}
12
13impl<T, Repr> Pretty<T, Repr> {
14    /// Wraps the given value.
15    pub const fn new(value: T) -> Self {
16        Pretty {
17            value,
18            _phantom: std::marker::PhantomData,
19        }
20    }
21
22    /// Converts the wrapped value into its representation type.
23    pub fn repr(self) -> Repr
24    where
25        Repr: From<T>,
26    {
27        Repr::from(self.value)
28    }
29}
30
31impl<T, Repr> std::fmt::Display for Pretty<T, Repr>
32where
33    T: Clone,
34    Repr: std::fmt::Display + From<T>,
35{
36    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
37        write!(formatter, "{}", Repr::from(self.value.clone()))
38    }
39}