serde_graphql_input/formatter/
pretty.rs

1use serde::Serialize;
2
3use crate::error::Result;
4use crate::{Formatter, Serializer};
5
6#[allow(dead_code)]
7pub struct PrettyFormatter<'a> {
8    current_indent: usize,
9    has_value: bool,
10    indent: &'a [u8],
11}
12
13impl<'a> PrettyFormatter<'a> {
14    pub fn new() -> Self {
15        PrettyFormatter::with_indent(b" ")
16    }
17
18    pub fn with_indent(indent: &'a [u8]) -> Self {
19        PrettyFormatter {
20            current_indent: 0,
21            has_value: false,
22            indent,
23        }
24    }
25}
26
27impl<'a> Default for PrettyFormatter<'a> {
28    fn default() -> Self {
29        PrettyFormatter::new()
30    }
31}
32
33impl<'a> Formatter for PrettyFormatter<'a> {}
34
35pub fn to_string_pretty<T>(value: &T) -> Result<String>
36where
37    T: ?Sized + Serialize,
38{
39    let mut writer = Vec::with_capacity(128);
40
41    let mut ser = Serializer::pretty(&mut writer);
42    value.serialize(&mut ser)?;
43
44    let string = unsafe { String::from_utf8_unchecked(writer) };
45
46    Ok(string)
47}