use crate::{Error, Result, RootType, TupleSpecifier};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TypeStem<'a> {
Root(RootType<'a>),
Tuple(TupleSpecifier<'a>),
}
impl<'a> TryFrom<&'a str> for TypeStem<'a> {
type Error = Error;
#[inline]
fn try_from(value: &'a str) -> Result<Self> {
Self::parse(value)
}
}
impl AsRef<str> for TypeStem<'_> {
#[inline]
fn as_ref(&self) -> &str {
self.span()
}
}
impl<'a> TypeStem<'a> {
#[inline]
pub const fn as_root(&self) -> Option<&RootType<'a>> {
match self {
Self::Root(root) => Some(root),
Self::Tuple(_) => None,
}
}
#[inline]
pub const fn as_tuple(&self) -> Option<&TupleSpecifier<'a>> {
match self {
Self::Root(_) => None,
Self::Tuple(tuple) => Some(tuple),
}
}
#[inline]
pub fn parse(s: &'a str) -> Result<Self> {
if s.starts_with('(') || s.starts_with("tuple(") {
s.try_into().map(Self::Tuple)
} else {
s.try_into().map(Self::Root)
}
}
#[inline]
pub const fn span(&self) -> &'a str {
match self {
Self::Root(root) => root.span(),
Self::Tuple(tuple) => tuple.span(),
}
}
#[inline]
pub fn try_basic_solidity(&self) -> Result<()> {
match self {
Self::Root(root) => root.try_basic_solidity(),
Self::Tuple(tuple) => tuple.try_basic_solidity(),
}
}
}