display.rs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //! Enables base64'd output anywhere you might use a `Display` implementation, like a format string.
  2. //!
  3. //! ```
  4. //! use base64::{display::Base64Display, engine::general_purpose::STANDARD};
  5. //!
  6. //! let data = vec![0x0, 0x1, 0x2, 0x3];
  7. //! let wrapper = Base64Display::new(&data, &STANDARD);
  8. //!
  9. //! assert_eq!("base64: AAECAw==", format!("base64: {}", wrapper));
  10. //! ```
  11. use super::chunked_encoder::ChunkedEncoder;
  12. use crate::engine::Engine;
  13. use core::fmt::{Display, Formatter};
  14. use core::{fmt, str};
  15. /// A convenience wrapper for base64'ing bytes into a format string without heap allocation.
  16. pub struct Base64Display<'a, 'e, E: Engine> {
  17. bytes: &'a [u8],
  18. chunked_encoder: ChunkedEncoder<'e, E>,
  19. }
  20. impl<'a, 'e, E: Engine> Base64Display<'a, 'e, E> {
  21. /// Create a `Base64Display` with the provided engine.
  22. pub fn new(bytes: &'a [u8], engine: &'e E) -> Base64Display<'a, 'e, E> {
  23. Base64Display {
  24. bytes,
  25. chunked_encoder: ChunkedEncoder::new(engine),
  26. }
  27. }
  28. }
  29. impl<'a, 'e, E: Engine> Display for Base64Display<'a, 'e, E> {
  30. fn fmt(&self, formatter: &mut Formatter) -> Result<(), fmt::Error> {
  31. let mut sink = FormatterSink { f: formatter };
  32. self.chunked_encoder.encode(self.bytes, &mut sink)
  33. }
  34. }
  35. struct FormatterSink<'a, 'b: 'a> {
  36. f: &'a mut Formatter<'b>,
  37. }
  38. impl<'a, 'b: 'a> super::chunked_encoder::Sink for FormatterSink<'a, 'b> {
  39. type Error = fmt::Error;
  40. fn write_encoded_bytes(&mut self, encoded: &[u8]) -> Result<(), Self::Error> {
  41. // Avoid unsafe. If max performance is needed, write your own display wrapper that uses
  42. // unsafe here to gain about 10-15%.
  43. self.f
  44. .write_str(str::from_utf8(encoded).expect("base64 data was not utf8"))
  45. }
  46. }
  47. #[cfg(test)]
  48. mod tests {
  49. use super::super::chunked_encoder::tests::{
  50. chunked_encode_matches_normal_encode_random, SinkTestHelper,
  51. };
  52. use super::*;
  53. use crate::engine::general_purpose::STANDARD;
  54. #[test]
  55. fn basic_display() {
  56. assert_eq!(
  57. "~$Zm9vYmFy#*",
  58. format!("~${}#*", Base64Display::new(b"foobar", &STANDARD))
  59. );
  60. assert_eq!(
  61. "~$Zm9vYmFyZg==#*",
  62. format!("~${}#*", Base64Display::new(b"foobarf", &STANDARD))
  63. );
  64. }
  65. #[test]
  66. fn display_encode_matches_normal_encode() {
  67. let helper = DisplaySinkTestHelper;
  68. chunked_encode_matches_normal_encode_random(&helper);
  69. }
  70. struct DisplaySinkTestHelper;
  71. impl SinkTestHelper for DisplaySinkTestHelper {
  72. fn encode_to_string<E: Engine>(&self, engine: &E, bytes: &[u8]) -> String {
  73. format!("{}", Base64Display::new(bytes, engine))
  74. }
  75. }
  76. }