#[expect(unused_imports, reason = "Used in a doc comment.")]
use std::str::FromStr;
#[expect(unused_imports, reason = "Used in a doc comment.")]
use std::borrow::Cow;
#[expect(unused_imports, reason = "Used in doc comments.")]
use crate::types::*;
macro_rules! string_or_struct_magic {
($type:ty) => {
impl Serialize for $type {
fn serialize<S: serde::ser::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
<$type>::serialize(self, serializer)
}
}
impl<'de> Deserialize<'de> for $type {
fn deserialize<D: serde::de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct V;
impl<'de> serde::de::Visitor<'de> for V {
type Value = $type;
fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
f.write_str("Expected a string or a map.")
}
fn visit_str<E: serde::de::Error>(self, s: &str) -> Result<Self::Value, E> {
Self::Value::from_str(s).map_err(E::custom)
}
fn visit_map<M: serde::de::MapAccess<'de>>(self, map: M) -> Result<Self::Value, M::Error> {
Self::Value::deserialize(serde::de::value::MapAccessDeserializer::new(map))
}
}
deserializer.deserialize_any(V)
}
}
}
}
macro_rules! get_option_string {
($value:expr, $task_state:expr) => {
get_option_cow!($value, $task_state).map(std::borrow::Cow::into_owned)
}
}
macro_rules! get_option_str {
($value:expr, $task_state:expr) => {
get_option_cow!($value, $task_state).as_deref()
}
}
macro_rules! get_new_option_str {
($value:expr, $task_state:expr) => {
get_option_string!($value, $task_state).as_deref()
}
}
macro_rules! get_option_cow {
($value:expr, $task_state:expr) => {
match $value {
StringSource::String(value) => Some(std::borrow::Cow::Borrowed(value.as_str())),
StringSource::None => None,
value => value.get(&$task_state.to_view())?
}
}
}
macro_rules! get_string {
($value:expr, $task_state:expr, $error:ty) => {
get_cow!($value, $task_state, $error).into_owned()
}
}
macro_rules! get_str {
($value:expr, $task_state:expr, $error:ty) => {
&*get_cow!($value, $task_state, $error)
}
}
macro_rules! get_new_str {
($value:expr, $task_state:expr, $error:ty) => {
&*get_string!($value, $task_state, $error)
}
}
macro_rules! get_cow {
($value:expr, $task_state:expr, $error:ty) => {
match $value.get_self() {
StringSource::String(value) => std::borrow::Cow::Borrowed(value.as_str()),
value => value.get(&$task_state.to_view())?.ok_or(<$error>::StringSourceIsNone)?
}
}
}
pub(crate) use string_or_struct_magic;
pub(crate) use get_str;
pub(crate) use get_new_str;
pub(crate) use get_string;
pub(crate) use get_cow;
pub(crate) use get_option_str;
pub(crate) use get_new_option_str;
pub(crate) use get_option_string;
pub(crate) use get_option_cow;
pub(crate) use url_cleaner_macros::edoc;