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
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE file at the
// root of this repository, or online at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Formatting utilities.

use std::fmt;
use std::io;

#[cfg(feature = "network")]
use bytes::{BufMut, BytesMut};

/// A trait for objects that can be infallibly written to.
///
/// Like [`std::fmt::Write`], except that the methods do not return errors.
/// Implementations are provided for [`String`], `bytes::BytesMut`, and
/// `Vec<u8>`, as writing to these types cannot fail.
///
/// Objects that implement `FormatBuffer` can be passed to the [`write!`] macro:
///
/// ```
/// use ore::fmt::FormatBuffer;
///
/// let mut buf = String::new();
/// write!(buf, "{:.02}", 1.0 / 7.0);
/// assert_eq!(buf, "0.14");
/// ```
///
/// The trait is particularly useful for functions that need to generically
/// write to either a [`String`] or a byte buffer:
///
/// ```
/// use ore::fmt::FormatBuffer;
///
/// fn write_timezone_offset<F>(buf: &mut F, mut offset_seconds: i32)
/// where
///     F: FormatBuffer,
/// {
///     if offset_seconds >= 0 {
///         buf.write_char('+');
///     } else {
///         buf.write_char('-');
///     }
///     let offset_seconds = offset_seconds.abs();
///     write!(buf, "{:02}:{:02}", offset_seconds / 60 / 60, offset_seconds / 60 % 60);
/// }
///
/// let offset_seconds = -18000;
///
/// let mut s = String::new();
/// write_timezone_offset(&mut s, offset_seconds);
///
/// let mut v = Vec::new();
/// write_timezone_offset(&mut v, offset_seconds);
///
/// assert_eq!(s, "-05:00");
/// assert_eq!(v, s.as_bytes());
/// ```
///
/// The implementations of `FormatBuffer` for `Vec<u8>` and `BytesMut` are
/// guaranteed to only write valid UTF-8 bytes into the underlying buffer.
pub trait FormatBuffer: AsRef<[u8]> {
    /// Glue for usage of the [`write!`] macro with implementors of this trait.
    ///
    /// This method should not be invoked manually, but rather through the
    /// `write!` macro itself.
    fn write_fmt(&mut self, fmt: fmt::Arguments);

    /// Writes a [`char`] into this buffer.
    fn write_char(&mut self, c: char);

    /// Writes a string into this buffer.
    fn write_str(&mut self, s: &str);

    /// Returns the number of bytes in the buffer.
    fn len(&self) -> usize;

    /// Reports whether the buffer is empty.
    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns a mutable reference to the bytes in the buffer.
    ///
    /// # Safety
    ///
    /// If the byte slice was valid UTF-8, it must remain valid UTF-8 when the
    // returned mutable reference is dropped. `FormatBuffer`s may be
    /// [`String`]s or other data types whose memory safety depends upon only
    /// containing valid UTF-8.
    unsafe fn as_bytes_mut(&mut self) -> &mut [u8];
}

impl FormatBuffer for String {
    fn write_fmt(&mut self, fmt: fmt::Arguments) {
        fmt::Write::write_fmt(self, fmt).expect("fmt::Write::write_fmt cannot fail on a String");
    }

    fn write_char(&mut self, c: char) {
        self.push(c)
    }

    fn write_str(&mut self, s: &str) {
        self.push_str(s)
    }

    fn len(&self) -> usize {
        self.len()
    }

    unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {
        str::as_bytes_mut(self)
    }
}

impl FormatBuffer for Vec<u8> {
    fn write_fmt(&mut self, fmt: fmt::Arguments) {
        io::Write::write_fmt(self, fmt).expect("io::Write::write_fmt cannot fail on Vec<u8>")
    }

    fn write_char(&mut self, c: char) {
        self.extend(c.encode_utf8(&mut [0; 4]).as_bytes())
    }

    fn write_str(&mut self, s: &str) {
        self.extend(s.as_bytes())
    }

    fn len(&self) -> usize {
        self.len()
    }

    unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {
        self
    }
}

#[cfg(feature = "network")]
impl FormatBuffer for BytesMut {
    fn write_fmt(&mut self, fmt: fmt::Arguments) {
        io::Write::write_fmt(&mut self.writer(), fmt)
            .expect("io::Write::write_fmt cannot fail on BytesMut")
    }

    fn write_char(&mut self, c: char) {
        self.put(c.encode_utf8(&mut [0; 4]).as_bytes())
    }

    fn write_str(&mut self, s: &str) {
        self.put(s.as_bytes())
    }

    fn len(&self) -> usize {
        self.len()
    }

    unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {
        self
    }
}