mod error;
mod safe;
pub(crate) mod self_referential;
mod union_variants_per_type_lookup;
pub use {error::SchemaError, safe::*, self_referential::Schema};
pub(crate) use union_variants_per_type_lookup::UnionVariantLookupKey;
impl std::str::FromStr for Schema {
type Err = SchemaError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
safe::SchemaMut::from_str(s)?.try_into()
}
}
impl Schema {
pub fn from_schemata(
main_schema: &str,
dep_schemas: impl IntoIterator<Item = impl AsRef<str>>,
) -> Result<Self, SchemaError> {
safe::SchemaMut::from_schemata(main_schema, dep_schemas)?.try_into()
}
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Fixed {
pub size: usize,
pub name: Name,
}
impl Fixed {
pub fn new(name: Name, size: usize) -> Self {
Self { size, name }
}
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Name {
fully_qualified_name: String,
namespace_delimiter_idx: Option<usize>,
}
impl std::fmt::Debug for Name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(&self.fully_qualified_name, f)
}
}
impl Name {
pub fn name(&self) -> &str {
match self.namespace_delimiter_idx {
None => &self.fully_qualified_name,
Some(delimiter_idx) => &self.fully_qualified_name[delimiter_idx + 1..],
}
}
pub fn namespace(&self) -> Option<&str> {
self.namespace_delimiter_idx
.map(|idx| &self.fully_qualified_name[..idx])
}
pub fn fully_qualified_name(&self) -> &str {
&self.fully_qualified_name
}
pub fn from_fully_qualified_name(fully_qualified_name: impl Into<String>) -> Self {
fn non_generic_inner(mut fully_qualified_name: String) -> Name {
Name {
namespace_delimiter_idx: match fully_qualified_name.rfind('.') {
Some(0) => {
fully_qualified_name.remove(0);
None
}
other => other,
},
fully_qualified_name,
}
}
non_generic_inner(fully_qualified_name.into())
}
}