use alloc::borrow::Cow;
use alloc::string::String;
use core::fmt;
#[cfg(all(
any(feature = "ssr", feature = "hydrate", feature = "csr"),
any(feature = "leptos", feature = "leptos-0-8")
))]
use crate::leptos::state::TextUse;
#[cfg(not(feature = "native"))]
use crate::leptos::text::{self, Description};
#[cfg(feature = "native")]
use crate::native::store::TextOf;
use crate::{Tr, TrArgs, TrDyn, TrRich};
#[cfg(feature = "native")]
use mf2_runtime::{Formatter, MsgId, NoErrors, Sink};
#[cfg(not(feature = "native"))]
fn ambient<D: Description>(description: &D, use_: TextUse) -> String {
text::to_string(description, use_)
}
#[cfg(all(
feature = "native",
any(feature = "ssr", feature = "hydrate", feature = "csr"),
any(feature = "leptos", feature = "leptos-0-8")
))]
fn ambient(m: &dyn TextOf, plain: bool) -> Cow<'static, str> {
let use_ = if plain {
TextUse::Plain
} else {
TextUse::Displayed
};
if let Some(text) = crate::leptos::text::requested(m, use_) {
return Cow::Owned(text);
}
match crate::native::store::text(m, plain) {
Some(text) => text,
None => Cow::Owned(crate::leptos::text::unrequested(m, use_)),
}
}
#[cfg(all(
feature = "native",
not(all(
any(feature = "ssr", feature = "hydrate", feature = "csr"),
any(feature = "leptos", feature = "leptos-0-8")
))
))]
fn ambient(m: &dyn TextOf, plain: bool) -> Cow<'static, str> {
match crate::native::store::text(m, plain) {
Some(text) => text,
None => crate::native::store::not_installed(),
}
}
#[cfg(feature = "native")]
fn display(m: &dyn TextOf, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.pad(&ambient(m, false))
}
#[cfg(feature = "native")]
macro_rules! text_of {
($($ty:ty),*) => {$(
impl TextOf for $ty {
fn msg_id(&self) -> MsgId {
self.id()
}
fn write_text(&self, f: &Formatter<'_>, out: &mut dyn Sink) {
self.write(f, out, &mut NoErrors);
}
}
)*};
}
#[cfg(feature = "native")]
text_of!(Tr, TrArgs, TrRich, TrDyn);
#[cfg(not(feature = "native"))]
macro_rules! string_conversions {
($ty:ty) => {
impl $ty {
#[must_use]
#[allow(
clippy::inherent_to_string_shadow_display,
reason = "the fmt-free form of what `Display` writes, which browser code calls; \
both produce the same text"
)]
pub fn to_string(&self) -> String {
ambient(self, TextUse::Displayed)
}
#[must_use]
pub fn to_plain_string(&self) -> String {
ambient(self, TextUse::Plain)
}
#[must_use]
pub fn to_cow(&self) -> Cow<'static, str> {
Cow::Owned(ambient(self, TextUse::Displayed))
}
#[must_use]
pub fn to_display_string(&self) -> String {
ambient(self, TextUse::Displayed)
}
}
impl From<$ty> for String {
fn from(description: $ty) -> String {
ambient(&description, TextUse::Displayed)
}
}
impl fmt::Display for $ty {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.pad(&ambient(self, TextUse::Displayed))
}
}
};
}
#[cfg(feature = "native")]
macro_rules! string_conversions {
($ty:ty) => {
impl $ty {
#[must_use]
#[allow(
clippy::inherent_to_string_shadow_display,
reason = "the fmt-free form of what `Display` writes, one allocation where \
`ToString` makes two; both produce the same text"
)]
pub fn to_string(&self) -> String {
ambient(self, false).into_owned()
}
#[must_use]
pub fn to_plain_string(&self) -> String {
ambient(self, true).into_owned()
}
#[must_use]
pub fn to_cow(&self) -> Cow<'static, str> {
ambient(self, false)
}
#[cfg(all(
any(feature = "ssr", feature = "hydrate", feature = "csr"),
any(feature = "leptos", feature = "leptos-0-8")
))]
#[must_use]
pub fn to_display_string(&self) -> String {
ambient(self, false).into_owned()
}
}
impl From<$ty> for String {
fn from(description: $ty) -> String {
ambient(&description, false).into_owned()
}
}
impl fmt::Display for $ty {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
display(self, f)
}
}
};
}
string_conversions!(Tr);
string_conversions!(TrArgs);
string_conversions!(TrRich);
string_conversions!(TrDyn);