use super::NextValueProducer;
use crate::{Conf, ConfContext, InitializationStateMachine, InnerError, Subcommands};
pub trait ConfSerde: Conf + Sized {
#[doc(hidden)]
type ISM<'a>: From<ConfSerdeContext<'a>>
+ for<'de> InitializationStateMachine<'de, Value = Self>;
#[doc(hidden)]
const STRUCT_NAME: &str;
#[doc(hidden)]
const STRUCT_KEYS: Option<&[&str]>;
#[doc(hidden)]
fn expecting(f: &mut core::fmt::Formatter) -> core::fmt::Result;
}
pub trait SubcommandsSerde: Subcommands + Sized {
#[doc(hidden)]
const SERDE_NAMES: &'static [(&'static str, &'static str)];
#[doc(hidden)]
fn from_conf_serde_context<'de, NVP>(
command_name: &str,
ctxt: ConfSerdeContext,
next_value_producer: NVP,
) -> Result<Self, Vec<InnerError>>
where
NVP: NextValueProducer<'de>;
}
#[doc(hidden)]
#[non_exhaustive]
pub struct ConfSerdeContext<'a> {
pub conf_context: ConfContext<'a>,
pub document_name: &'a str,
pub depth: usize,
}
impl<'a> ConfSerdeContext<'a> {
pub(crate) fn new(mut conf_context: ConfContext<'a>, document_name: &'a str) -> Self {
conf_context.serde_source_is_present = true;
Self {
conf_context,
document_name,
depth: 0,
}
}
pub fn for_flattened(&self, id_prefix: &str) -> Self {
Self {
conf_context: self.conf_context.for_flattened(id_prefix),
document_name: self.document_name,
depth: self.depth + 1,
}
}
pub fn for_subcommand(&self) -> Option<(String, Self)> {
self.conf_context
.for_subcommand()
.map(|(subcommand_name, conf_context)| {
(
subcommand_name,
Self {
conf_context,
document_name: self.document_name,
depth: self.depth,
},
)
})
}
}