summaryrefslogtreecommitdiffstats
path: root/third_party/rust/base64/src/display.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/base64/src/display.rs
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/base64/src/display.rs')
-rw-r--r--third_party/rust/base64/src/display.rs88
1 files changed, 88 insertions, 0 deletions
diff --git a/third_party/rust/base64/src/display.rs b/third_party/rust/base64/src/display.rs
new file mode 100644
index 0000000000..fc292f1b00
--- /dev/null
+++ b/third_party/rust/base64/src/display.rs
@@ -0,0 +1,88 @@
+//! Enables base64'd output anywhere you might use a `Display` implementation, like a format string.
+//!
+//! ```
+//! use base64::{display::Base64Display, engine::general_purpose::STANDARD};
+//!
+//! let data = vec![0x0, 0x1, 0x2, 0x3];
+//! let wrapper = Base64Display::new(&data, &STANDARD);
+//!
+//! assert_eq!("base64: AAECAw==", format!("base64: {}", wrapper));
+//! ```
+
+use super::chunked_encoder::ChunkedEncoder;
+use crate::engine::Engine;
+use core::fmt::{Display, Formatter};
+use core::{fmt, str};
+
+/// A convenience wrapper for base64'ing bytes into a format string without heap allocation.
+pub struct Base64Display<'a, 'e, E: Engine> {
+ bytes: &'a [u8],
+ chunked_encoder: ChunkedEncoder<'e, E>,
+}
+
+impl<'a, 'e, E: Engine> Base64Display<'a, 'e, E> {
+ /// Create a `Base64Display` with the provided engine.
+ pub fn new(bytes: &'a [u8], engine: &'e E) -> Base64Display<'a, 'e, E> {
+ Base64Display {
+ bytes,
+ chunked_encoder: ChunkedEncoder::new(engine),
+ }
+ }
+}
+
+impl<'a, 'e, E: Engine> Display for Base64Display<'a, 'e, E> {
+ fn fmt(&self, formatter: &mut Formatter) -> Result<(), fmt::Error> {
+ let mut sink = FormatterSink { f: formatter };
+ self.chunked_encoder.encode(self.bytes, &mut sink)
+ }
+}
+
+struct FormatterSink<'a, 'b: 'a> {
+ f: &'a mut Formatter<'b>,
+}
+
+impl<'a, 'b: 'a> super::chunked_encoder::Sink for FormatterSink<'a, 'b> {
+ type Error = fmt::Error;
+
+ fn write_encoded_bytes(&mut self, encoded: &[u8]) -> Result<(), Self::Error> {
+ // Avoid unsafe. If max performance is needed, write your own display wrapper that uses
+ // unsafe here to gain about 10-15%.
+ self.f
+ .write_str(str::from_utf8(encoded).expect("base64 data was not utf8"))
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::super::chunked_encoder::tests::{
+ chunked_encode_matches_normal_encode_random, SinkTestHelper,
+ };
+ use super::*;
+ use crate::engine::general_purpose::STANDARD;
+
+ #[test]
+ fn basic_display() {
+ assert_eq!(
+ "~$Zm9vYmFy#*",
+ format!("~${}#*", Base64Display::new(b"foobar", &STANDARD))
+ );
+ assert_eq!(
+ "~$Zm9vYmFyZg==#*",
+ format!("~${}#*", Base64Display::new(b"foobarf", &STANDARD))
+ );
+ }
+
+ #[test]
+ fn display_encode_matches_normal_encode() {
+ let helper = DisplaySinkTestHelper;
+ chunked_encode_matches_normal_encode_random(&helper);
+ }
+
+ struct DisplaySinkTestHelper;
+
+ impl SinkTestHelper for DisplaySinkTestHelper {
+ fn encode_to_string<E: Engine>(&self, engine: &E, bytes: &[u8]) -> String {
+ format!("{}", Base64Display::new(bytes, engine))
+ }
+ }
+}