mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
feat(span): format_compact_str! macro (#5610)
Add `format_compact_str!` macro to create a `CompactStr`, like `format!`.
This commit is contained in:
parent
3d190a5728
commit
b3cbd56520
2 changed files with 45 additions and 0 deletions
|
|
@ -126,6 +126,12 @@ impl From<String> for CompactStr {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<CompactString> for CompactStr {
|
||||
fn from(s: CompactString) -> Self {
|
||||
Self(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s> From<&'s CompactStr> for Cow<'s, str> {
|
||||
fn from(value: &'s CompactStr) -> Self {
|
||||
Self::Borrowed(value.as_str())
|
||||
|
|
@ -251,10 +257,38 @@ impl schemars::JsonSchema for CompactStr {
|
|||
}
|
||||
}
|
||||
|
||||
/// Creates a `CompactStr` using interpolation of runtime expressions.
|
||||
///
|
||||
/// The first argument `format_compact_str!` receives is a format string.
|
||||
/// This must be a string literal.
|
||||
/// The power of the formatting string is in the `{}`s contained.
|
||||
///
|
||||
/// Additional parameters passed to `format_compact_str!` replace the `{}`s within
|
||||
/// the formatting string in the order given unless named or
|
||||
/// positional parameters are used; see [`std::fmt`] for more information.
|
||||
///
|
||||
/// A common use for `format_compact_str!` is concatenation and interpolation
|
||||
/// of strings.
|
||||
/// The same convention is used with [`print!`] and [`write!`] macros,
|
||||
/// depending on the intended destination of the string.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// `format_compact_str!` panics if a formatting trait implementation returns
|
||||
/// an error.
|
||||
#[macro_export]
|
||||
macro_rules! format_compact_str {
|
||||
($($arg:tt)*) => {
|
||||
$crate::CompactStr::from($crate::__internal::format_compact!($($arg)*))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use compact_str::CompactString;
|
||||
|
||||
use crate::format_compact_str;
|
||||
|
||||
use super::CompactStr;
|
||||
|
||||
#[test]
|
||||
|
|
@ -266,4 +300,9 @@ mod test {
|
|||
assert_eq!("foo", &foo);
|
||||
assert_eq!(foo.into_compact_string(), CompactString::new("foo"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_compact_str() {
|
||||
assert_eq!(format_compact_str!("foo{}bar", 123), "foo123bar");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,3 +18,9 @@ pub use crate::{
|
|||
},
|
||||
span::{GetSpan, GetSpanMut, Span, SPAN},
|
||||
};
|
||||
|
||||
#[doc(hidden)]
|
||||
pub mod __internal {
|
||||
// Used by `format_compact_str!` macro defined in `compact_str.rs`
|
||||
pub use ::compact_str::format_compact;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue