Skip to content

Commit

Permalink
feat(span): format_compact_str! macro (#5610)
Browse files Browse the repository at this point in the history
Add `format_compact_str!` macro to create a `CompactStr`, like `format!`.
  • Loading branch information
overlookmotel committed Sep 8, 2024
1 parent 3d190a5 commit b3cbd56
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
39 changes: 39 additions & 0 deletions crates/oxc_span/src/compact_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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]
Expand All @@ -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");
}
}
6 changes: 6 additions & 0 deletions crates/oxc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit b3cbd56

Please sign in to comment.