use super::{Buffer, Format, Formatter};
use crate::FormatResult;
pub struct Argument<'fmt, Context> {
value: &'fmt dyn Format<Context>,
}
impl<Context> Clone for Argument<'_, Context> {
fn clone(&self) -> Self {
*self
}
}
impl<Context> Copy for Argument<'_, Context> {}
impl<'fmt, Context> Argument<'fmt, Context> {
#[doc(hidden)]
#[inline]
pub const fn new<F: Format<Context>>(value: &'fmt F) -> Self {
Self { value }
}
#[inline]
#[allow(clippy::trivially_copy_pass_by_ref)]
pub(super) fn format(&self, f: &mut Formatter<Context>) -> FormatResult<()> {
self.value.fmt(f)
}
}
pub struct Arguments<'fmt, Context>(pub &'fmt [Argument<'fmt, Context>]);
impl<'fmt, Context> Arguments<'fmt, Context> {
#[doc(hidden)]
#[inline]
pub const fn new(arguments: &'fmt [Argument<'fmt, Context>]) -> Self {
Self(arguments)
}
#[inline]
#[allow(clippy::trivially_copy_pass_by_ref)] pub(super) fn items(&self) -> &'fmt [Argument<'fmt, Context>] {
self.0
}
}
impl<Context> Copy for Arguments<'_, Context> {}
impl<Context> Clone for Arguments<'_, Context> {
fn clone(&self) -> Self {
*self
}
}
impl<Context> Format<Context> for Arguments<'_, Context> {
#[inline]
fn fmt(&self, formatter: &mut Formatter<Context>) -> FormatResult<()> {
formatter.write_fmt(*self)
}
}
impl<Context> std::fmt::Debug for Arguments<'_, Context> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("Arguments[...]")
}
}
impl<'fmt, Context> From<&'fmt Argument<'fmt, Context>> for Arguments<'fmt, Context> {
fn from(argument: &'fmt Argument<'fmt, Context>) -> Self {
Arguments::new(std::slice::from_ref(argument))
}
}
#[cfg(test)]
mod tests {
use crate::format_element::tag::Tag;
use crate::prelude::*;
use crate::{FormatState, VecBuffer, format_args, write};
#[test]
fn test_nesting() {
let mut context = FormatState::new(SimpleFormatContext::default());
let mut buffer = VecBuffer::new(&mut context);
write!(
&mut buffer,
[
token("function"),
space(),
token("a"),
space(),
group(&format_args!(token("("), token(")")))
]
)
.unwrap();
assert_eq!(
buffer.into_vec(),
vec![
FormatElement::Token { text: "function" },
FormatElement::Space,
FormatElement::Token { text: "a" },
FormatElement::Space,
FormatElement::Tag(Tag::StartGroup(tag::Group::new())),
FormatElement::Token { text: "(" },
FormatElement::Token { text: ")" },
FormatElement::Tag(Tag::EndGroup)
]
);
}
}