1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use crate as rune;

use crate::alloc::fmt::TryWrite;
use crate::alloc::{self, String};
use crate::Any;

/// A formatter for the rune virtual machine.
///
/// This is used as a receiver to functions implementing the [`STRING_DEBUG`]
/// and [`STRING_DISPLAY`] protocols.
///
/// [`STRING_DEBUG`]: crate::runtime::Protocol::STRING_DEBUG
/// [`STRING_DISPLAY`]: crate::runtime::Protocol::STRING_DISPLAY
#[derive(Any)]
#[rune(item = ::std::fmt)]
pub struct Formatter {
    pub(crate) string: String,
    pub(crate) buf: String,
}

impl Formatter {
    /// Construct a new empty formatter.
    ///
    /// # Examples
    ///
    /// ```
    /// use rune::runtime::Formatter;
    ///
    /// let mut f = Formatter::new();
    /// ```
    #[inline]
    pub fn new() -> Self {
        Self {
            string: String::new(),
            buf: String::new(),
        }
    }

    #[inline]
    pub(crate) fn with_capacity(capacity: usize) -> alloc::Result<Self> {
        Ok(Self {
            string: String::try_with_capacity(capacity)?,
            buf: String::new(),
        })
    }

    #[inline]
    pub(crate) fn parts_mut(&mut self) -> (&mut String, &str) {
        (&mut self.string, &self.buf)
    }

    #[inline]
    pub(crate) fn buf_mut(&mut self) -> &mut String {
        &mut self.buf
    }

    #[inline]
    pub(crate) fn push(&mut self, c: char) -> alloc::Result<()> {
        self.string.try_push(c)
    }

    #[inline]
    pub(crate) fn push_str(&mut self, s: &str) -> alloc::Result<()> {
        self.string.try_push_str(s)
    }

    #[inline]
    pub(crate) fn into_string(self) -> String {
        self.string
    }

    #[inline]
    pub(crate) fn as_str(&self) -> &str {
        &self.string
    }
}

impl Default for Formatter {
    fn default() -> Self {
        Self::new()
    }
}

impl TryWrite for Formatter {
    #[inline]
    fn try_write_str(&mut self, s: &str) -> alloc::Result<()> {
        self.string.try_push_str(s)
    }

    #[inline]
    fn try_write_char(&mut self, c: char) -> alloc::Result<()> {
        self.string.try_push(c)
    }
}