### UpperExp Trait Examples Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/trait.UpperExp.html Examples demonstrating how to use the UpperExp trait for formatting floating-point numbers and implementing it for custom types. ```APIDOC ## Examples ### Basic usage with `f64`: ```rust let x = 42.0; // 42.0 is '4.2E1' in scientific notation assert_eq!(format!("{x:E}"), "4.2E1"); ``` ### Implementing `UpperExp` on a type: ```rust use std::fmt; struct Length(i32); impl fmt::UpperExp for Length { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let val = f64::from(self.0); fmt::UpperExp::fmt(&val, f) // delegate to f64's implementation } } let l = Length(100); assert_eq!( format!("l in scientific notation is: {l:E}"), "l in scientific notation is: 1E2" ); assert_eq!( format!("l in scientific notation is: {l:05E}"), "l in scientific notation is: 001E2" ); ``` ``` -------------------------------- ### Complex DebugSet Example with Format Args Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/struct.Formatter.html A more advanced example demonstrating the use of `format_args!` and `debug_set` to construct a debug representation for a table-like structure, including match arms. ```rust use std::fmt; struct Arm<'a, L, R>(&'a (L, R)); struct Table<'a, K, V>(&'a [(K, V)], V); impl<'a, L, R> fmt::Debug for Arm<'a, L, R> where L: 'a + fmt::Debug, R: 'a + fmt::Debug { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { L::fmt(&(self.0).0, fmt)?; fmt.write_str(" => ")?; R::fmt(&(self.0).1, fmt) } } impl<'a, K, V> fmt::Debug for Table<'a, K, V> where K: 'a + fmt::Debug, V: 'a + fmt::Debug { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { fmt.debug_set() .entries(self.0.iter().map(Arm)) .entry(&Arm(&(format_args!("_"), &self.1))) .finish() } } ``` -------------------------------- ### Implement write_fmt for a writer Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/trait.Write.html Example demonstrating the use of write_fmt to write formatted arguments into a buffer. ```rust use std::fmt::{Error, Write}; fn writer(f: &mut W, s: &str) -> Result<(), Error> { f.write_fmt(format_args!("{s}")) } let mut buf = String::new(); writer(&mut buf, "world")?; assert_eq!(&buf, "world"); ``` -------------------------------- ### Example Usage of Display Trait Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/type.Result.html This example demonstrates how to implement the `fmt::Display` trait for a custom struct `Triangle` and how to use it with the `format!` macro. This showcases a common pattern for custom type formatting, which the `font8x8::unicode::fmt::Result` type is designed to support. ```APIDOC ## Example Usage of Display Trait ### Description This example shows how to implement the `fmt::Display` trait for a custom struct `Triangle` and use it with the `format!` macro. ### Code Example ```rust use std::fmt; #[derive(Debug)] struct Triangle { a: f32, b: f32, c: f32 } impl fmt::Display for Triangle { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "({}, {}, {})", self.a, self.b, self.c) } } let pythagorean_triple = Triangle { a: 3.0, b: 4.0, c: 5.0 }; assert_eq!(format!("{{pythagorean_triple}}"), "(3, 4, 5)"); ``` ``` -------------------------------- ### Example Usage of fmt::Error in Rust Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/struct.Error.html Demonstrates how to handle potential formatting errors using `std::fmt::write`. This example shows checking for `fmt::Error` and panicking if an error occurs during the write operation. ```rust use std::fmt::{self, write}; let mut output = String::new(); if let Err(fmt::Error) = write(&mut output, format_args!("Hello {}!", "world")) { panic!("An error occurred"); } ``` -------------------------------- ### LowerHex Trait Examples Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/trait.LowerHex.html Examples demonstrating the basic usage of the LowerHex trait with primitive types and custom implementations. ```APIDOC ## §Examples ### Basic usage with `i32`: ```rust let y = 42; // 42 is '2a' in hex assert_eq!(format!("{y:x}"), "2a"); assert_eq!(format!("{y:#x}"), "0x2a"); assert_eq!(format!("{:x}", -16), "fffffff0"); ``` ### Implementing `LowerHex` on a type: ```rust use std::fmt; struct Length(i32); impl fmt::LowerHex for Length { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let val = self.0; fmt::LowerHex::fmt(&val, f) // delegate to i32's implementation } } let l = Length(9); assert_eq!(format!("l as hex is: {l:x}"), "l as hex is: 9"); assert_eq!(format!("l as hex is: {l:#010x}"), "l as hex is: 0x00000009"); ``` ``` -------------------------------- ### Implement Display and Binary traits for a custom struct Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/index.html Example showing how to implement fmt::Display and fmt::Binary for a Vector2D struct, including usage of the Formatter object. ```rust use std::fmt; #[derive(Debug)] struct Vector2D { x: isize, y: isize, } impl fmt::Display for Vector2D { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { // The `f` value implements the `Write` trait, which is what the // write! macro is expecting. Note that this formatting ignores the // various flags provided to format strings. write!(f, "({}, {})", self.x, self.y) } } // Different traits allow different forms of output of a type. The meaning // of this format is to print the magnitude of a vector. impl fmt::Binary for Vector2D { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let magnitude = (self.x * self.x + self.y * self.y) as f64; let magnitude = magnitude.sqrt(); // Respect the formatting flags by using the helper method // `pad_integral` on the Formatter object. See the method // documentation for details, and the function `pad` can be used // to pad strings. let decimals = f.precision().unwrap_or(3); let string = format!("{magnitude:.decimals$}"); f.pad_integral(true, "", &string) } } fn main() { let myvector = Vector2D { x: 3, y: 4 }; println!("{myvector}"); // => "(3, 4)" println!("{myvector:?}"); // => "Vector2D {x: 3, y:4}" println!("{myvector:10.3b}"); // => " 5.000" } ``` -------------------------------- ### Implement write_str for a writer Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/trait.Write.html Example demonstrating the use of write_str to write a string slice into a buffer. ```rust use std::fmt::{Error, Write}; fn writer(f: &mut W, s: &str) -> Result<(), Error> { f.write_str(s) } let mut buf = String::new(); writer(&mut buf, "hola")?; assert_eq!(&buf, "hola"); ``` -------------------------------- ### Implement Debug for Foo using DebugMap::key and DebugMap::value Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/struct.DebugMap.html This example shows an alternative way to implement `fmt::Debug` for `Foo` using `fmt.debug_map().key()` and `fmt.debug_map().value()`. This approach is useful when the key and value are not known at the same time, though `entry` is preferred when possible. ```rust use std::fmt; struct Foo(Vec<(String, i32)>); impl fmt::Debug for Foo { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { fmt.debug_map() .key(&"whole").value(&self.0) // We add the "whole" entry. .finish() } } assert_eq!( format!("{:?}", Foo(vec![("A".to_string(), 10), ("B".to_string(), 11)])), r#"{"whole": [("A", 10), ("B", 11)]}"#, // Corrected raw string literal ); ``` -------------------------------- ### Implement Debug for Foo using DebugMap::entry Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/struct.DebugMap.html This example demonstrates implementing `fmt::Debug` for `Foo` using `fmt.debug_map().entry()`. It adds a single entry with a predefined key "whole" and the struct's data as the value. ```rust use std::fmt; struct Foo(Vec<(String, i32)>); impl fmt::Debug for Foo { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { fmt.debug_map() .entry(&"whole", &self.0) // We add the "whole" entry. .finish() } } assert_eq!( format!("{:?}", Foo(vec![("A".to_string(), 10), ("B".to_string(), 11)])), r#"{"whole": [("A", 10), ("B", 11)]}"#, // Corrected raw string literal ); ``` -------------------------------- ### Implement write_char for a writer Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/trait.Write.html Example demonstrating the use of write_char to write individual characters into a buffer. ```rust use std::fmt::{Error, Write}; fn writer(f: &mut W, c: char) -> Result<(), Error> { f.write_char(c) } let mut buf = String::new(); writer(&mut buf, 'a')?; writer(&mut buf, 'b')?; assert_eq!(&buf, "ab"); ``` -------------------------------- ### Implement Display for Struct with Formatting Options Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/struct.Formatter.html Example of implementing `fmt::Display` for a struct, asserting specific formatting options like sign awareness and width. Note that formatter options are ignored in this specific implementation. ```rust use std::fmt; struct Foo(i32); impl fmt::Display for Foo { fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { assert!(formatter.sign_aware_zero_pad()); assert_eq!(formatter.width(), Some(4)); // We ignore the formatter's options. write!(formatter, "{}", self.0) } } assert_eq!(format!("{:04}", Foo(23)), "23"); ``` -------------------------------- ### Debug Trait Example (Position) Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/trait.Debug.html A detailed example of implementing `Debug` for a `Position` struct using `debug_tuple`. ```APIDOC ## §Examples ```rust use std::fmt; struct Position { longitude: f32, latitude: f32, } impl fmt::Debug for Position { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("") .field(&self.longitude) .field(&self.latitude) .finish() } } let position = Position { longitude: 1.987, latitude: 2.983 }; assert_eq!(format!("{{position:?}}"), "(1.987, 2.983)"); assert_eq!(format!("{{position:#?}}"), "( 1.987, 2.983, )"); ``` ``` -------------------------------- ### Implementing Display Trait Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/type.Result.html Example demonstrating how to implement the `std::fmt::Display` trait for a custom struct. This allows the struct to be formatted using methods like `format!()`. ```rust use std::fmt; #[derive(Debug)] struct Triangle { a: f32, b: f32, c: f32 } impl fmt::Display for Triangle { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "({}, {}, {})", self.a, self.b, self.c) } } let pythagorean_triple = Triangle { a: 3.0, b: 4.0, c: 5.0 }; assert_eq!(format!("{pythagorean_triple}"), "(3, 4, 5)"); ``` -------------------------------- ### Custom Debug Formatting Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/trait.Debug.html Shows an example of a completely custom `Debug` implementation that does not use the standard builder methods. ```APIDOC ## Custom Debug Formatting ```rust use std::fmt; struct Point { x: i32, y: i32, } impl fmt::Debug for Point { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "Point [{} {}]", self.x, self.y) } } ``` ``` -------------------------------- ### Implement Debug for Foo using DebugMap::entries with iterator mapping Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/struct.DebugMap.html This example demonstrates implementing `fmt::Debug` for `Foo` using `fmt.debug_map().entries()`. It specifically maps the struct's internal vector to extract keys and values for the debug output. ```rust use std::fmt; struct Foo(Vec<(String, i32)>); impl fmt::Debug for Foo { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { fmt.debug_map() // We map our vec so each entries' first field will become // the "key". .entries(self.0.iter().map(|&(ref k, ref v)| (k, v))) .finish() } } assert_eq!( format!("{:?}", Foo(vec![("A".to_string(), 10), ("B".to_string(), 11)])), r#"{"A": 10, "B": 11}"#, // Corrected raw string literal ); ``` -------------------------------- ### Implement Debug for Map-like Structure with DebugMap Builder Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/struct.Formatter.html Example of implementing `fmt::Debug` for a map-like structure using the `debug_map` builder. This is suitable for types that represent key-value mappings. ```rust use std::fmt; struct Foo(Vec<(String, i32)>); impl fmt::Debug for Foo { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { fmt.debug_map().entries(self.0.iter().map(|&(ref k, ref v)| (k, v))).finish() } } assert_eq!( format!("{:?}", Foo(vec![("A".to_string(), 10), ("B".to_string(), 11)])), r#"{"A": 10, "B": 11}"# ); ``` -------------------------------- ### Implement Debug for Foo using DebugMap::entries Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/struct.DebugMap.html This example shows how to implement the `fmt::Debug` trait for a custom struct `Foo` that contains a vector of key-value pairs. It uses `fmt.debug_map().entries()` to format the vector as a map. ```rust use std::fmt; struct Foo(Vec<(String, i32)>); impl fmt::Debug for Foo { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { fmt.debug_map().entries(self.0.iter().map(|&(ref k, ref v)| (k, v))).finish() } } assert_eq!( format!("{:?}", Foo(vec![("A".to_string(), 10), ("B".to_string(), 11)])), r#"{"A": 10, "B": 11}"#, // Corrected raw string literal ); ``` -------------------------------- ### Implement Debug for List-like Structure with DebugList Builder Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/struct.Formatter.html Example of implementing `fmt::Debug` for a list-like structure using the `debug_list` builder. This is appropriate for types that behave like lists or sequences. ```rust use std::fmt; struct Foo(Vec); impl fmt::Debug for Foo { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { fmt.debug_list().entries(self.0.iter()).finish() } } assert_eq!(format!("{:?}", Foo(vec![10, 11])), "[10, 11]"); ``` -------------------------------- ### Initialize FormattingOptions Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/struct.FormattingOptions.html Constructs a new instance with default formatting settings. ```rust pub const fn new() -> FormattingOptions ``` -------------------------------- ### BlockFonts UnicodeFonts get() Implementation Source: https://docs.rs/font8x8/0.3.1/src/font8x8/block.rs.html Implements the `get` method from the `UnicodeFonts` trait for `BlockFonts`. It attempts to retrieve a font by character and converts it to a `[u8; 8]` array. ```rust impl UnicodeFonts for BlockFonts { fn get(&self, key: char) -> Option<[u8; 8]> { match self.get_font(key) { Some(font) => Some(font.into()), ``` -------------------------------- ### LatinFonts get Method Source: https://docs.rs/font8x8/0.3.1/src/font8x8/latin.rs.html Implements the `get` method from the `UnicodeFonts` trait for `LatinFonts`. It attempts to retrieve a font by character, converting the `FontUnicode` to a `[u8; 8]` byte array if found. ```rust fn get(&self, key: char) -> Option<[u8; 8]> { match self.get_font(key) { Some(font) => Some(font.into()), None => None, } } ``` -------------------------------- ### Format Arguments to String Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/fn.format.html Demonstrates using the format function with the format_args! macro to create a formatted string. ```rust use std::fmt; let s = fmt::format(format_args!("Hello, {}!", "world")); assert_eq!(s, "Hello, world!"); ``` -------------------------------- ### Pad String Formatting Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/struct.Formatter.html Demonstrates using pad to apply width, alignment, and precision to string output. ```rust use std::fmt; struct Foo; impl fmt::Display for Foo { fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { formatter.pad("Foo") } } assert_eq!(format!("{Foo:<4}"), "Foo "); assert_eq!(format!("{Foo:0>4}"), "0Foo"); ``` -------------------------------- ### GET /fonts/basic_unicode Source: https://docs.rs/font8x8/0.3.1/src/font8x8/basic.rs.html Retrieves the bitmap representation for a specific Unicode character from the BASIC_UNICODE set. ```APIDOC ## GET /fonts/basic_unicode ### Description Retrieves the 8x8 bitmap representation for a character in the BASIC_UNICODE set. ### Method GET ### Endpoint /fonts/basic_unicode/{unicode_id} ### Parameters #### Path Parameters - **unicode_id** (integer) - Required - The index of the character (e.g., 108 for 'l', 126 for '~'). ### Response #### Success Response (200) - **bitmap** (string) - The 8x8 visual representation of the character. #### Response Example { "char": "l", "unicode": "U+006C", "bitmap": "███░░░░░\n░██░░░░░\n░██░░██░\n░██░██░░\n░████░░░\n░██░██░░\n███░░██░\n░░░░░░░░" } ``` -------------------------------- ### Character Bitmap Representations Source: https://docs.rs/font8x8/0.3.1/font8x8/constant.BASIC_FONTS.html Visual 8x8 grid representations for characters starting from U+0021. ```text ░░░██░░░ ░░████░░ ░░████░░ ░░░██░░░ ░░░██░░░ ░░░░░░░░ ░░░██░░░ ░░░░░░░░ ``` ```text ░██░██░░ ░██░██░░ ░░░░░░░░ ░░░░░░░░ ░░░░░░░░ ░░░░░░░░ ░░░░░░░░ ░░░░░░░░ ``` ```text ░██░██░░ ░██░██░░ ███████░ ░██░██░░ ███████░ ░██░██░░ ░██░██░░ ░░░░░░░░ ``` ```text ░░██░░░░ ░█████░░ ██░░░░░░ ░████░░░ ░░░░██░░ █████░░░ ░░██░░░░ ░░░░░░░░ ``` ```text ░░░░░░░░ ██░░░██░ ██░░██░░ ░░░██░░░ ░░██░░░░ ░██░░██░ ██░░░██░ ░░░░░░░░ ``` ```text ░░███░░░ ░██░██░░ ░░███░░░ ░███░██░ ██░███░░ ██░░██░░ ░███░██░ ░░░░░░░░ ``` ```text ░██░░░░░ ░██░░░░░ ██░░░░░░ ░░░░░░░░ ░░░░░░░░ ░░░░░░░░ ░░░░░░░░ ░░░░░░░░ ``` ```text ░░░██░░░ ░░██░░░░ ░██░░░░░ ░██░░░░░ ░██░░░░░ ░░██░░░░ ░░░██░░░ ░░░░░░░░ ``` ```text ░██░░░░░ ░░██░░░░ ░░░██░░░ ░░░██░░░ ░░░██░░░ ░░██░░░░ ░██░░░░░ ░░░░░░░░ ``` ```text ░░░░░░░░ ░██░░██░ ░░████░░ ████████ ░░████░░ ░██░░██░ ░░░░░░░░ ░░░░░░░░ ``` ```text ░░░░░░░░ ░░██░░░░ ░░██░░░░ ██████░░ ░░██░░░░ ░░██░░░░ ░░░░░░░░ ░░░░░░░░ ``` ```text ░░░░░░░░ ░░░░░░░░ ░░░░░░░░ ░░░░░░░░ ░░░░░░░░ ░░██░░░░ ░░██░░░░ ░██░░░░░ ``` ```text ░░░░░░░░ ░░░░░░░░ ░░░░░░░░ ██████░░ ░░░░░░░░ ░░░░░░░░ ░░░░░░░░ ░░░░░░░░ ``` ```text ░░░░░░░░ ░░░░░░░░ ░░░░░░░░ ░░░░░░░░ ░░░░░░░░ ░░██░░░░ ░░██░░░░ ░░░░░░░░ ``` ```text ░░░░░██░ ░░░░██░░ ░░░██░░░ ░░██░░░░ ░██░░░░░ ██░░░░░░ █░░░░░░░ ░░░░░░░░ ``` ```text ░█████░░ ██░░░██░ ██░░███░ ██░████░ ████░██░ ███░░██░ ░█████░░ ░░░░░░░░ ``` ```text ░░██░░░░ ░███░░░░ ░░██░░░░ ░░██░░░░ ░░██░░░░ ░░██░░░░ ██████░░ ░░░░░░░░ ``` ```text ░████░░░ ██░░██░░ ░░░░██░░ ░░███░░░ ░██░░░░░ ██░░██░░ ██████░░ ░░░░░░░░ ``` ```text ░████░░░ ██░░██░░ ░░░░██░░ ░░███░░░ ░░░░██░░ ██░░██░░ ░████░░░ ░░░░░░░░ ``` ```text ░░░███░░ ░░████░░ ░██░██░░ ██░░██░░ ███████░ ░░░░██░░ ░░░████░ ░░░░░░░░ ``` ```text ██████░░ ██░░░░░░ █████░░░ ░░░░██░░ ░░░░██░░ ██░░██░░ ░████░░░ ░░░░░░░░ ``` ```text ░░███░░░ ░██░░░░░ ██░░░░░░ █████░░░ ██░░██░░ ██░░██░░ ░████░░░ ░░░░░░░░ ``` ```text ██████░░ ██░░██░░ ░░░░██░░ ░░░██░░░ ░░██░░░░ ░░██░░░░ ░░██░░░░ ░░░░░░░░ ``` ```text ░████░░░ ██░░██░░ ██░░██░░ ░████░░░ ██░░██░░ ██░░██░░ ░████░░░ ░░░░░░░░ ``` -------------------------------- ### Format Arguments and Display Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/struct.Arguments.html Demonstrates using `format_args!` to create `Arguments` and then formatting them with `Debug` and `Display`. Both traits format to the same interpolated string. ```rust let debug = format!("{:?}", format_args!("{} foo 2", 1)); let display = format!("{}", format_args!("{} foo 2", 1)); assert_eq!("1 foo 2", display); assert_eq!(display, debug); ``` -------------------------------- ### Basic format! macro usage Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/index.html Demonstrates various ways to use the format! macro for string interpolation and formatting. ```rust format!("Hello"); // => "Hello" format!("Hello, {}!", "world"); // => "Hello, world!" format!("The number is {}", 1); // => "The number is 1" format!("{:?}", (3, 4)); // => "(3, 4)" format!("{value}", value=4); // => "4" let people = "Rustaceans"; format!("Hello {people}!"); // => "Hello Rustaceans!" format!("{} {}", 1, 2); // => "1 2" format!("{:04}", 42); // => "0042" with leading zeros format!("{:#?}", (100, 200)); // => "( // 100, // 200, // )" ``` -------------------------------- ### Basic Binary Formatting with i32 Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/trait.Binary.html Demonstrates basic binary formatting for i32, including the alternate flag '#'. Negative numbers are shown in two's complement. ```rust let x = 42; // 42 is '101010' in binary assert_eq!(format!("{x:b}"), "101010"); assert_eq!(format!("{x:#b}"), "0b101010"); assert_eq!(format!({":b}", -16), "11111111111111111111111111110000"); ``` -------------------------------- ### Print to stdout with print! and println! Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/index.html Outputs formatted strings to standard output. ```rust print!("Hello {}!", "world"); println!("I have a newline {}", "character at the end"); ``` -------------------------------- ### Get Default BasicFonts Value Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/struct.BasicFonts.html Returns the default value for the BasicFonts type. This is an implementation of the Default trait. ```rust fn default() -> Self ``` -------------------------------- ### Generic Any Implementation Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/enum.DebugAsHex.html Provides a way to get the `TypeId` of any type that implements `Any`, useful for dynamic type checking. ```rust fn type_id(&self) -> TypeId ``` -------------------------------- ### Basic Usage of font8x8::unicode::fmt::write Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/fn.write.html Demonstrates basic usage of the `write` function to format a string into a `String` output. Ensure the `std::fmt` module is imported. The function returns a `Result`, so error handling with `.expect()` is shown. ```rust use std::fmt; let mut output = String::new(); fmt::write(&mut output, format_args!("Hello {}!", "world")) .expect("Error occurred while trying to write in String"); assert_eq!(output, "Hello world!"); ``` -------------------------------- ### Implementing Display for Position Struct Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/trait.Display.html Example of implementing the Display trait for a Position struct to format floating-point coordinates. ```rust use std::fmt; struct Position { longitude: f32, latitude: f32, } impl fmt::Display for Position { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "({}, {})", self.longitude, self.latitude) } } assert_eq!( "(1.987, 2.983)", format!("{}", Position { longitude: 1.987, latitude: 2.983, }), ); ``` -------------------------------- ### Create BasicFonts Instance Source: https://docs.rs/font8x8/0.3.1/src/font8x8/basic.rs.html Creates a new `BasicFonts` instance initialized with the `BASIC_UNICODE` font collection. This is the primary way to obtain a font set for use. ```rust impl BasicFonts { /// Create a new collection of `BASIC_UNICODE` fonts. pub fn new() -> Self { BasicFonts(BASIC_UNICODE) } } ``` -------------------------------- ### Pointer Trait Documentation Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/trait.Pointer.html Documentation for the font8x8::unicode::fmt::Pointer trait, including its definition, purpose, and examples. ```APIDOC ## Trait Pointer font8x8::unicode::fmt ### Summary ``` pub trait Pointer { // Required method fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>; } ``` ### Description `p` formatting. The `Pointer` trait should format its output as a memory location. This is commonly presented as hexadecimal. For more information on formatters, see the module-level documentation. Printing of pointers is not a reliable way to discover how Rust programs are implemented. The act of reading an address changes the program itself, and may change how the data is represented in memory, and may affect which optimizations are applied to the code. The printed pointer values are not guaranteed to be stable nor unique identifiers of objects. Rust allows moving values to different memory locations, and may reuse the same memory locations for different purposes. There is no guarantee that the printed value can be converted back to a pointer. ### Examples #### Basic usage with `&i32`: ```rust let x = &42; let address = format!("{x:p}"); // this produces something like '0x7f06092ac6d0' ``` #### Implementing `Pointer` on a type: ```rust use std::fmt; struct Length(i32); impl fmt::Pointer for Length { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { // use `as` to convert to a `*const T`, which implements Pointer, which we can use let ptr = self as *const Self; fmt::Pointer::fmt(&ptr, f) } } let l = Length(42); println!("l is in memory here: {{l:p}}"); let l_ptr = format!("{l:018p}"); assert_eq!(l_ptr.len(), 18); assert_eq!(&l_ptr[..2], "0x"); ``` ### Required Methods #### fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> Formats the value using the given formatter. ##### Errors This function should return `Err` if, and only if, the provided `Formatter` returns `Err`. String formatting is considered an infallible operation; this function only returns a `Result` because writing to the underlying stream might fail and it must provide a way to propagate the fact that an error has occurred back up the stack. ### Implementors * `impl Pointer for F where F: FnPtr` * `impl Pointer for Pin where Ptr: Pointer` * `impl Pointer for *const T where T: ?Sized` * `impl Pointer for *mut T where T: ?Sized` * `impl Pointer for &T where T: ?Sized` * `impl Pointer for &mut T where T: ?Sized` * `impl Pointer for NonNull where T: ?Sized` * `impl Pointer for AtomicPtr` * `impl Pointer for Box where A: Allocator, T: ?Sized` * `impl Pointer for Rc where A: Allocator, T: ?Sized` * `impl Pointer for UniqueRc where A: Allocator, T: ?Sized` * `impl Pointer for Arc where A: Allocator, T: ?Sized` * `impl Pointer for UniqueArc where A: Allocator, T: ?Sized` ``` -------------------------------- ### Implement LowerExp for Custom Types Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/trait.LowerExp.html Example of implementing the LowerExp trait for a custom struct by delegating to an existing implementation. ```rust use std::fmt; struct Length(i32); impl fmt::LowerExp for Length { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let val = f64::from(self.0); fmt::LowerExp::fmt(&val, f) // delegate to f64's implementation } } let l = Length(100); assert_eq!( format!("l in scientific notation is: {l:e}"), "l in scientific notation is: 1e2" ); assert_eq!( format!("l in scientific notation is: {l:05e}"), "l in scientific notation is: 001e2" ); ``` -------------------------------- ### Retrieve formatting settings Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/struct.FormattingOptions.html Methods to inspect the current state of the formatting options. ```rust pub const fn get_sign(&self) -> Option ``` ```rust pub const fn get_sign_aware_zero_pad(&self) -> bool ``` ```rust pub const fn get_alternate(&self) -> bool ``` -------------------------------- ### Define GreekFonts Struct Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/struct.GreekFonts.html Defines the GreekFonts struct, a private collection wrapper for GREEK_UNICODE. No setup is required to use this struct. ```rust pub struct GreekFonts(/* private fields */); ``` -------------------------------- ### Implementing Display for a Custom Struct Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/trait.Display.html Example of implementing the Display trait for a custom Point struct to enable user-friendly string representation. ```rust use std::fmt; struct Point { x: i32, y: i32, } impl fmt::Display for Point { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "({}, {})", self.x, self.y) } } let origin = Point { x: 0, y: 0 }; assert_eq!(format!("The origin is: {origin}"), "The origin is: (0, 0)"); ``` -------------------------------- ### Pad Integral Formatting Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/struct.Formatter.html Demonstrates using pad_integral to handle integer padding and sign placement. ```rust use std::fmt; struct Foo { nb: i32 } impl Foo { fn new(nb: i32) -> Foo { Foo { nb, } } } impl fmt::Display for Foo { fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { // We need to remove "-" from the number output. let tmp = self.nb.abs().to_string(); formatter.pad_integral(self.nb >= 0, "Foo ", &tmp) } } assert_eq!(format!("{}", Foo::new(2)), "2"); assert_eq!(format!("{}", Foo::new(-1)), "-1"); assert_eq!(format!("{}", Foo::new(0)), "0"); assert_eq!(format!("{:#}", Foo::new(-1)), "-Foo 1"); assert_eq!(format!("{:0>#8}", Foo::new(-1)), "00-Foo 1"); ``` -------------------------------- ### GET /hiragana/unicode Source: https://docs.rs/font8x8/0.3.1/src/font8x8/hiragana.rs.html Retrieves the Hiragana font mapping constant, which provides access to specific Unicode characters and their associated 8x8 bitmap glyphs. ```APIDOC ## GET /hiragana/unicode ### Description Access the HIRAGANA_UNICODE constant array to retrieve character glyphs for Hiragana characters ranging from U+3040 to U+309F. ### Method GET ### Endpoint /hiragana/unicode ### Response #### Success Response (200) - **HIRAGANA_UNICODE** (Array) - A collection of FontUnicode objects containing the character mapping and 8x8 bitmap data. #### Response Example { "index": 1, "unicode": "U+3041", "char": "ぁ", "glyph": [ "░░█░░░░░", "██████░░", "░░█░░░░░", "░░████░░", "░██░█░█░", "█░██░░█░", "░██░░█░░", "░░░░░░░░" ] } ``` -------------------------------- ### Write Formatted Arguments Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/struct.Formatter.html Demonstrates using write_fmt to write formatted arguments to the formatter. ```rust use std::fmt; struct Foo(i32); impl fmt::Display for Foo { fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { formatter.write_fmt(format_args!("Foo {}", self.0)) } } assert_eq!(format!("{}", Foo(-1)), "Foo -1"); assert_eq!(format!("{:0>8}", Foo(2)), "Foo 2"); ``` -------------------------------- ### Get FontUnicode by Character Key Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/struct.BasicFonts.html Retrieves a FontUnicode object for a given character key from the BasicFonts collection. Returns an Option if the character is found. ```rust fn get_font(&self, key: char) -> Option ``` -------------------------------- ### Get FontUnicode by Character Source: https://docs.rs/font8x8/0.3.1/src/font8x8/basic.rs.html Searches for and returns a `FontUnicode` struct for a given character using binary search. This is an efficient way to look up fonts by character. ```rust fn get_font(&self, key: char) -> Option { match self.0.binary_search_by_key(&key, |&f| f.char()) { Ok(idx) => Some(self.0[idx]), _ => None, } } ``` -------------------------------- ### Finish DebugTuple Formatting Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/struct.DebugTuple.html The `.finish()` method is required to complete the debug tuple formatting. It should be called after all fields have been added. This example shows the complete implementation for `Foo`. ```rust use std::fmt; struct Foo(i32, String); impl fmt::Debug for Foo { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { fmt.debug_tuple("Foo") .field(&self.0) .field(&self.1) .finish() // You need to call it to "finish" the // tuple formatting. } } assert_eq!( format!("{:?}", Foo(10, "Hello World".to_string())), r#"Foo(10, \"Hello World\")"#, ); ``` -------------------------------- ### FormattingOptions Create Formatter Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/struct.FormattingOptions.html Creates a Formatter instance for writing formatted output. This is a nightly-only experimental API. ```APIDOC ## POST /formatting_options/create_formatter ### Description Creates a `Formatter` that writes its output to the given `Write` trait. You may alternatively use `Formatter::new()`. ### Method POST ### Endpoint /formatting_options/create_formatter ### Parameters #### Request Body - **write** (dyn Write) - Required - A mutable reference to a type implementing the `Write` trait. ### Request Example ```json { "write": "" } ``` ### Response #### Success Response (200) - **formatter** (Formatter) - The created Formatter instance. #### Response Example ```json { "formatter": "" } ``` ``` -------------------------------- ### Get Font by Character Key Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/struct.BasicFonts.html Retrieves the font data for a given character key from the BasicFonts collection. Returns an Option containing the font data if found. ```rust fn get(&self, key: char) -> Option<[u8; 8]> ``` -------------------------------- ### List of format! family macros Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/index.html A summary of available macros in the format! family. ```rust format! // described above write! // first argument is either a &mut io::Write or a &mut fmt::Write, the destination writeln! // same as write but appends a newline print! // the format string is printed to the standard output println! // same as print but appends a newline eprint! // the format string is printed to the standard error eprintln! // same as eprint but appends a newline format_args! // described below. ``` -------------------------------- ### UnicodeFonts Trait Implementation for GreekFonts Source: https://docs.rs/font8x8/0.3.1/src/font8x8/greek.rs.html Implements the UnicodeFonts trait for GreekFonts, providing methods to get font data by character, iterate over fonts, and print the character set. ```rust impl UnicodeFonts for GreekFonts { fn get(&self, key: char) -> Option<[u8; 8]> { match self.get_font(key) { Some(font) => Some(font.into()), None => None, } } fn get_font(&self, key: char) -> Option { match self.0.binary_search_by_key(&key, |&f| f.char()) { Ok(idx) => Some(self.0[idx]), _ => None, } } fn iter(&self) -> ::core::slice::Iter { self.0.iter() } #[cfg(feature = "std")] fn print_set(&self) { println!(); println!("# `{:?}`", self); for (idx, font) in self.0.iter().enumerate() { print!("## `{:?}[{:?}]: U+{:04X}`", self, idx, font.char() as u32,); if font.is_whitespace() { println!(" `WHITESPACE`"); } else { println!(); println!("`{:?}`", font.to_string()); println!(); println!("```text"); for x in &font.byte_array() { for bit in 0..8 { match *x & 1 << bit { 0 => print!("░"), _ => print!("█"), } } println!(); } println!("```"); println!(); } } } #[cfg(feature = "std")] fn to_vec(&self) -> Vec<(char, FontUnicode)> { self.0.iter().fold(Vec::with_capacity(128), |mut v, font| { v.push((font.char(), *font)); v }) } } ``` -------------------------------- ### Use LowerExp for Formatting Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/trait.LowerExp.html Basic usage of the 'e' format specifier with f64. ```rust let x = 42.0; // 42.0 is '4.2e1' in scientific notation assert_eq!(format!("{x:e}"), "4.2e1"); ``` -------------------------------- ### Functions Source: https://docs.rs/font8x8/0.3.1/font8x8/all.html List of all available functions in the font8x8-0.3.1 crate. ```APIDOC ## Functions ### unicode::fmt::format ### unicode::fmt::from_fn ### unicode::fmt::write ``` -------------------------------- ### Basic Usage of std::fmt::write! macro Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/fn.write.html Shows the equivalent functionality using the standard library's `write!` macro. This macro requires importing the `std::fmt::Write` trait. Similar to the function, it returns a `Result` that should be handled. ```rust use std::fmt::Write; let mut output = String::new(); write!(&mut output, "Hello {}!", "world") .expect("Error occurred while trying to write in String"); assert_eq!(output, "Hello world!"); ``` -------------------------------- ### Rust format! Macro with Named Parameters Source: https://docs.rs/font8x8/0.3.1/font8x8/unicode/fmt/index.html Demonstrates the use of named parameters in Rust's format! macro. Named parameters allow for more readable formatting by referencing arguments by name. ```rust format!("{argument}", argument = "test"); // => "test" format!("{name} {}", 1, name = 2); // => "2 1" format!("{a} {c} {b}", a="a", b='b', c=3); // => "a 3 b" ``` ```rust let argument = 2 + 2; format!("{argument}"); // => "4" ``` ```rust fn make_string(a: u32, b: &str) -> String { format!("{b} {a}") } make_string(927, "label"); // => "label 927" ```