1 // Copyright 2024 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 #![no_std] 15 //! Core support code for code generated by `pw_format` macros 16 //! 17 //! The contents of this crate are primarily used by code which is generated 18 //! by macros using `pw_format`. 19 20 /// Trait used to produce printf style format strings for untyped format 21 /// specifiers. 22 /// 23 /// The const [`PrintfFormatter::FORMAT_ARG`] is used to provide the printf 24 /// format specifier (i.e. the `d` in `%02d`) for a given type in Rust. This 25 /// must be const so that it can be used to build static const format strings at 26 /// compile time. 27 pub trait PrintfFormatter { 28 /// The format specifier for this type. 29 const FORMAT_ARG: &'static str; 30 } 31 32 /// Trait used to produce printf style format strings for hex formatted untyped 33 /// format specifiers. 34 /// 35 /// The const [`PrintfHexFormatter::FORMAT_ARG`] is used to provide the printf 36 /// format specifier (i.e. the `x` in `%02x`) for a given type in Rust. This 37 /// must be const so that it can be used to build static const format strings at 38 /// compile time. 39 pub trait PrintfHexFormatter { 40 /// The format specifier for this type. 41 const FORMAT_ARG: &'static str; 42 } 43 44 /// Trait used to produce printf style format strings for upper case hex 45 /// formatted untyped format specifiers. 46 /// 47 /// The const [`PrintfUpperHexFormatter::FORMAT_ARG`] is used to provide the 48 /// printf format specifier (i.e. the `x` in `%02x`) for a given type in Rust. 49 /// This must be const so that it can be used to build static const format 50 /// strings at compile time. 51 pub trait PrintfUpperHexFormatter { 52 /// The format specifier for this type. 53 const FORMAT_ARG: &'static str; 54 } 55 56 /// A helper to declare a [`PrintfFormatter`] trait and optionally 57 /// [`PrintfHexFormatter`] and [`PrintfUpperHexFormatter`] traits for a given 58 /// type. 59 macro_rules! declare_formatter { 60 ($ty:ty, $specifier:literal, $hex_specifier:literal, $upper_hex_specifier:literal) => { 61 impl PrintfFormatter for $ty { 62 const FORMAT_ARG: &'static str = $specifier; 63 } 64 65 impl PrintfHexFormatter for $ty { 66 const FORMAT_ARG: &'static str = $hex_specifier; 67 } 68 69 impl PrintfUpperHexFormatter for $ty { 70 const FORMAT_ARG: &'static str = $upper_hex_specifier; 71 } 72 }; 73 74 ($ty:ty, $specifier:literal) => { 75 impl PrintfFormatter for $ty { 76 const FORMAT_ARG: &'static str = $specifier; 77 } 78 }; 79 } 80 81 declare_formatter!(i32, "d"); 82 declare_formatter!(u32, "u", "x", "X"); 83 declare_formatter!(&str, "s"); 84