#[doc(hidden)]
#[macro_export]
macro_rules! __language_crate_functions {
($lang:literal) => {
#[doc = concat!("The format of ", $lang, ", registered the first time it is asked for.")]
pub fn format() -> ::core::result::Result<$crate::Format, $crate::Error> {
static FORMAT: ::std::sync::OnceLock<$crate::Format> = ::std::sync::OnceLock::new();
if let ::core::option::Option::Some(format) = FORMAT.get() {
return ::core::result::Result::Ok(*format);
}
let format = register()?;
::core::result::Result::Ok(*FORMAT.get_or_init(|| format))
}
#[doc = concat!("Deserialize ", $lang, " text into a typed value: [`fig::from_slice`] in [`format()`].")]
#[cfg(feature = "serde")]
pub fn from_str<T: ::serde::de::DeserializeOwned>(
s: &str,
) -> ::core::result::Result<T, $crate::Error> {
$crate::from_slice(s.as_bytes(), format()?)
}
#[doc = concat!("Deserialize ", $lang, " bytes into a typed value; see [`from_str`].")]
#[cfg(feature = "serde")]
pub fn from_slice<T: ::serde::de::DeserializeOwned>(
bytes: &[u8],
) -> ::core::result::Result<T, $crate::Error> {
$crate::from_slice(bytes, format()?)
}
#[doc = concat!("Deserialize ", $lang, " read to its end from `reader` into a typed value; see [`from_str`].")]
#[cfg(feature = "serde")]
pub fn from_reader<T: ::serde::de::DeserializeOwned, R: ::std::io::Read>(
mut reader: R,
) -> ::core::result::Result<T, $crate::Error> {
let mut bytes = ::std::vec::Vec::new();
reader.read_to_end(&mut bytes)?;
from_slice(&bytes)
}
#[doc = concat!("Serialize a value as ", $lang, " text: [`fig::to_value`], then [`fig::Value::serialize`] in [`format()`].")]
#[cfg(feature = "serde")]
pub fn to_string<T: ::serde::Serialize + ?Sized>(
value: &T,
) -> ::core::result::Result<::std::string::String, $crate::Error> {
$crate::to_value(value)?.serialize(format()?)
}
#[doc = concat!("Serialize a value as ", $lang, " bytes; see [`to_string`].")]
#[cfg(feature = "serde")]
pub fn to_vec<T: ::serde::Serialize + ?Sized>(
value: &T,
) -> ::core::result::Result<::std::vec::Vec<u8>, $crate::Error> {
to_string(value).map(::std::string::String::into_bytes)
}
#[doc = concat!("Serialize a value as ", $lang, " into `writer`; see [`to_string`].")]
#[cfg(feature = "serde")]
pub fn to_writer<W: ::std::io::Write, T: ::serde::Serialize + ?Sized>(
mut writer: W,
value: &T,
) -> ::core::result::Result<(), $crate::Error> {
writer.write_all(to_string(value)?.as_bytes())?;
::core::result::Result::Ok(())
}
};
}