/// A type that describes a format. /// /// Implementors of [`Formattable`] are [format descriptions](crate::format_description). /// /// [`Date::format`] and [`Time::format`] each use a format description to generate /// a String from their data. See the respective methods for usage examples. #[cfg_attr(docsrs, doc(notable_trait))] pubtrait Formattable: sealed::Sealed {} impl Formattable for BorrowedFormatItem<'_> {} impl Formattable for [BorrowedFormatItem<'_>] {} impl Formattable for OwnedFormatItem {} impl Formattable for [OwnedFormatItem] {} impl Formattable for Rfc3339 {} impl Formattable for Rfc2822 {} impl<const CONFIG: EncodedConfig> Formattable for Iso8601<CONFIG> {} impl<T> Formattable for T where T: Deref<Target: Formattable> {}
/// Seal the trait to prevent downstream users from implementing it. mod sealed { usesuper::*; usecrate::formatting::ComponentProvider;
/// Format the item using a format description, the intended output, and the various components. #[expect(
private_bounds,
private_interfaces,
reason = "irrelevant due to being a sealed trait"
)] pubtrait Sealed { /// Format the item into the provided output, returning the number of bytes written. fn format_into<V>(
&self,
output: &mut (impl io::Write + ?Sized),
value: &V,
state: &mut V::State,
) -> Result<usize, error::Format> where
V: ComponentProvider;
/// Format the item directly to a `String`. #[inline] fn format<V>(&self, value: &V, state: &mut V::State) -> Result<String, error::Format> where
V: ComponentProvider,
{ letmut buf = Vec::new(); self.format_into(&mut buf, value, state)?;
Ok(String::from_utf8_lossy(&buf).into_owned())
}
}
}
impl sealed::Sealed for [OwnedFormatItem] { #[expect(
private_bounds,
private_interfaces,
reason = "irrelevant due to being a sealed trait"
)] #[inline] fn format_into<V>(
&self,
output: &mut (impl io::Write + ?Sized),
value: &V,
state: &mut V::State,
) -> Result<usize, error::Format> where
V: ComponentProvider,
{ letmut bytes = 0; for item inself.iter() {
bytes += item.format_into(output, value, state)?;
}
Ok(bytes)
}
}
impl<T> sealed::Sealed for T where
T: Deref<Target: sealed::Sealed>,
{ #[expect(
private_bounds,
private_interfaces,
reason = "irrelevant due to being a sealed trait"
)] #[inline] fn format_into<V>(
&self,
output: &mut (impl io::Write + ?Sized),
value: &V,
state: &mut V::State,
) -> Result<usize, error::Format> where
V: ComponentProvider,
{ self.deref().format_into(output, value, state)
}
}
impl sealed::Sealed for Rfc2822 { #[expect(
private_bounds,
private_interfaces,
reason = "irrelevant due to being a sealed trait"
)] fn format_into<V>(
&self,
output: &mut (impl io::Write + ?Sized),
value: &V,
state: &mut V::State,
) -> Result<usize, error::Format> where
V: ComponentProvider,
{ const {
assert!(
V::SUPPLIES_DATE && V::SUPPLIES_TIME && V::SUPPLIES_OFFSET, "Rfc2822 requires date, time, and offset components, but not all can be provided \
by this type"
);
}
letmut bytes = 0;
if value.calendar_year(state) < 1900 { return Err(error::Format::InvalidComponent("year"));
} if value.offset_second(state) != 0 { return Err(error::Format::InvalidComponent("offset_second"));
}
impl sealed::Sealed for Rfc3339 { #[expect(
private_bounds,
private_interfaces,
reason = "irrelevant due to being a sealed trait"
)] fn format_into<V>(
&self,
output: &mut (impl io::Write + ?Sized),
value: &V,
state: &mut V::State,
) -> Result<usize, error::Format> where
V: ComponentProvider,
{ const {
assert!(
V::SUPPLIES_DATE && V::SUPPLIES_TIME && V::SUPPLIES_OFFSET, "Rfc3339 requires date, time, and offset components, but not all can be provided \
by this type"
);
}
let offset_hour = value.offset_hour(state); letmut bytes = 0;
if !(0..10_000).contains(&value.calendar_year(state)) { return Err(error::Format::InvalidComponent("year"));
} if offset_hour.unsigned_abs() > 23 { return Err(error::Format::InvalidComponent("offset_hour"));
} if value.offset_second(state) != 0 { return Err(error::Format::InvalidComponent("offset_second"));
}
impl<const CONFIG: EncodedConfig> sealed::Sealed for Iso8601<CONFIG> { #[expect(
private_bounds,
private_interfaces,
reason = "irrelevant due to being a sealed trait"
)] #[inline] fn format_into<V>(
&self,
output: &mut (impl io::Write + ?Sized),
value: &V,
state: &mut V::State,
) -> Result<usize, error::Format> where
V: ComponentProvider,
{ letmut bytes = 0;
const {
assert!(
!Self::FORMAT_DATE || V::SUPPLIES_DATE, "this Iso8601 configuration formats date components, but this type cannot provide \
them"
);
assert!(
!Self::FORMAT_TIME || V::SUPPLIES_TIME, "this Iso8601 configuration formats time components, but this type cannot provide \
them"
);
assert!(
!Self::FORMAT_OFFSET || V::SUPPLIES_OFFSET, "this Iso8601 configuration formats offset components, but this type cannot \
provide them"
);
assert!( Self::FORMAT_DATE || Self::FORMAT_TIME || Self::FORMAT_OFFSET, "this Iso8601 configuration does not format any components"
);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.