mirror of
https://github.com/TxtDot/dalet-rs.git
synced 2025-03-13 19:24:38 +03:00
feat: convert to typed from daletl
This commit is contained in:
parent
c121a53314
commit
5a42997045
8 changed files with 239 additions and 29 deletions
195
src/traits/from_daletl.rs
Normal file
195
src/traits/from_daletl.rs
Normal file
|
@ -0,0 +1,195 @@
|
|||
use crate::{
|
||||
daletl::*,
|
||||
typed::{
|
||||
Tag::{self, *},
|
||||
*,
|
||||
},
|
||||
};
|
||||
|
||||
impl TryFrom<DlTag> for Tag {
|
||||
type Error = ConversionError;
|
||||
|
||||
fn try_from(tag: DlTag) -> Result<Self, Self::Error> {
|
||||
let result = match tag.id {
|
||||
DlTid::El => El(tag.body.try_into()?),
|
||||
DlTid::H => H(tag.body.try_into()?, tag.argument.try_into()?),
|
||||
DlTid::P => P(tag.body.try_into()?),
|
||||
DlTid::Br => Br,
|
||||
DlTid::Ul => Ul(tag.body.try_into()?),
|
||||
DlTid::Ol => Ol(tag.body.try_into()?),
|
||||
DlTid::Row => Row(tag.body.try_into()?, tag.argument.try_into()?),
|
||||
DlTid::Link => Link(tag.body.try_into()?, tag.argument.try_into()?),
|
||||
DlTid::Navlink => Navlink(tag.body.try_into()?, tag.argument.try_into()?),
|
||||
DlTid::Btn => Btn(tag.body.try_into()?, tag.argument.try_into()?),
|
||||
DlTid::Navbtn => Navbtn(tag.body.try_into()?, tag.argument.try_into()?),
|
||||
DlTid::Img => Img(tag.argument.try_into()?),
|
||||
DlTid::Table => Table(tag.body.try_into()?),
|
||||
DlTid::Tcol => Tcol(tag.body.try_into()?),
|
||||
DlTid::Tpcol => Tpcol(tag.body.try_into()?),
|
||||
DlTid::Hr => Hr,
|
||||
DlTid::B => B(tag.body.try_into()?),
|
||||
DlTid::I => I(tag.body.try_into()?),
|
||||
DlTid::Bq => Bq(tag.body.try_into()?),
|
||||
DlTid::Footlnk => Footlnk(tag.argument.try_into()?),
|
||||
DlTid::Footn => Footn(tag.body.try_into()?, tag.argument.try_into()?),
|
||||
DlTid::A => A(tag.argument.try_into()?),
|
||||
DlTid::S => S(tag.body.try_into()?),
|
||||
DlTid::Sup => Sup(tag.body.try_into()?),
|
||||
DlTid::Sub => Sub(tag.body.try_into()?),
|
||||
DlTid::Disc => Disc(tag.body.try_into()?),
|
||||
DlTid::Bl => Bl(tag.body.try_into()?, tag.argument.try_into()?),
|
||||
DlTid::Carousel => Carousel(tag.body.try_into()?),
|
||||
DlTid::Code => Code(tag.body.try_into()?, tag.argument.try_into()?),
|
||||
DlTid::Pre => Pre(tag.body.try_into()?),
|
||||
};
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<DlArgument> for Hl {
|
||||
type Error = ConversionError;
|
||||
|
||||
fn try_from(value: DlArgument) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
DlArgument::Number(n) => n.try_into().map_err(|_| ConversionError),
|
||||
_ => Err(ConversionError),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<DlArgument> for AlignArg {
|
||||
type Error = ConversionError;
|
||||
|
||||
fn try_from(value: DlArgument) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
DlArgument::Number(n) => n.try_into().map_err(|_| ConversionError),
|
||||
_ => Err(ConversionError),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<DlArgument> for TNArg {
|
||||
type Error = ConversionError;
|
||||
|
||||
fn try_from(value: DlArgument) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
DlArgument::Text(t) => Ok(TNArg::Text(t.into())),
|
||||
DlArgument::Null => Ok(TNArg::Null),
|
||||
_ => Err(ConversionError),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<DlBody> for Body {
|
||||
type Error = ConversionError;
|
||||
|
||||
fn try_from(value: DlBody) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
DlBody::Text(t) => Ok(t.into()),
|
||||
DlBody::Tags(t) => Ok(Body::Tags(
|
||||
t.into_iter()
|
||||
.map(|dltag| dltag.try_into())
|
||||
.collect::<Result<Vec<Tag>, Self::Error>>()?,
|
||||
)),
|
||||
DlBody::Null => Ok(Body::Null),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<DlBody> for String {
|
||||
type Error = ConversionError;
|
||||
|
||||
fn try_from(value: DlBody) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
DlBody::Text(s) => Ok(s),
|
||||
_ => Err(ConversionError),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DlArgument> for Arg {
|
||||
fn from(value: DlArgument) -> Self {
|
||||
match value {
|
||||
DlArgument::Text(s) => s.into(),
|
||||
DlArgument::Number(n) => n.into(),
|
||||
DlArgument::Null => Self::Null,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<DlArgument> for NNArg {
|
||||
type Error = ConversionError;
|
||||
|
||||
fn try_from(value: DlArgument) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
DlArgument::Text(t) => Ok(t.into()),
|
||||
DlArgument::Number(n) => Ok(n.into()),
|
||||
DlArgument::Null => Err(ConversionError),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<DlArgument> for String {
|
||||
type Error = ConversionError;
|
||||
|
||||
fn try_from(value: DlArgument) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
DlArgument::Text(s) => Ok(s),
|
||||
_ => Err(ConversionError),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<DlBody> for NNBody {
|
||||
type Error = ConversionError;
|
||||
|
||||
fn try_from(value: DlBody) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
DlBody::Text(t) => Ok(t.into()),
|
||||
DlBody::Tags(t) => Ok(NNBody::Tags(
|
||||
t.into_iter()
|
||||
.map(|dltag| dltag.try_into())
|
||||
.collect::<Result<Vec<Tag>, Self::Error>>()?,
|
||||
)),
|
||||
DlBody::Null => Err(ConversionError),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<DlBody> for Vec<Tag> {
|
||||
type Error = ConversionError;
|
||||
|
||||
fn try_from(value: DlBody) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
DlBody::Tags(t) => t.into_iter().map(|dltag| dltag.try_into()).collect(),
|
||||
_ => Err(ConversionError),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<DlPage> for Vec<Tag> {
|
||||
type Error = ConversionError;
|
||||
|
||||
fn try_from(value: DlPage) -> Result<Self, Self::Error> {
|
||||
value
|
||||
.data
|
||||
.into_iter()
|
||||
.map(|dltag| dltag.try_into())
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<DlPage> for Page {
|
||||
type Error = ConversionError;
|
||||
|
||||
fn try_from(value: DlPage) -> Result<Self, Self::Error> {
|
||||
Ok(Self {
|
||||
data: value
|
||||
.data
|
||||
.into_iter()
|
||||
.map(|dltag| dltag.try_into())
|
||||
.collect::<Result<Vec<Tag>, Self::Error>>()?,
|
||||
})
|
||||
}
|
||||
}
|
|
@ -116,26 +116,18 @@ impl From<Vec<Tag>> for DlBody {
|
|||
}
|
||||
}
|
||||
|
||||
impl ToDaletlPage for Page {
|
||||
fn to_dl_page(self) -> DlPage {
|
||||
self.into_iter().map(|tag| tag.into()).collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl IsNull for DlBody {
|
||||
fn is_null(&self) -> bool {
|
||||
match self {
|
||||
Self::Null => true,
|
||||
_ => false,
|
||||
impl From<Vec<Tag>> for DlPage {
|
||||
fn from(value: Vec<Tag>) -> Self {
|
||||
Self {
|
||||
data: value.into_iter().map(|t| t.into()).collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl IsNull for DlArgument {
|
||||
fn is_null(&self) -> bool {
|
||||
match self {
|
||||
Self::Null => true,
|
||||
_ => false,
|
||||
impl From<Page> for DlPage {
|
||||
fn from(value: Page) -> Self {
|
||||
Self {
|
||||
data: value.data.into_iter().map(|t| t.into()).collect(),
|
||||
}
|
||||
}
|
||||
}
|
19
src/traits/is_null_daletl.rs
Normal file
19
src/traits/is_null_daletl.rs
Normal file
|
@ -0,0 +1,19 @@
|
|||
use crate::daletl::{DlArgument, DlBody, IsNull};
|
||||
|
||||
impl IsNull for DlBody {
|
||||
fn is_null(&self) -> bool {
|
||||
match self {
|
||||
Self::Null => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl IsNull for DlArgument {
|
||||
fn is_null(&self) -> bool {
|
||||
match self {
|
||||
Self::Null => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1 +1,3 @@
|
|||
mod typed;
|
||||
mod from_daletl;
|
||||
mod from_typed;
|
||||
mod is_null_daletl;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue