mirror of
https://github.com/TxtDot/dalet-rs.git
synced 2025-03-13 11:14:37 +03:00
feat: AutoFrom
This commit is contained in:
parent
fd698859b8
commit
5058bb812b
5 changed files with 149 additions and 164 deletions
|
@ -1,7 +1,9 @@
|
|||
use enum_procs::AutoFrom;
|
||||
use num_enum::TryFromPrimitive;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_repr::{Deserialize_repr, Serialize_repr};
|
||||
|
||||
use num_enum::TryFromPrimitive;
|
||||
pub type Root = Vec<Tag>;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Tag {
|
||||
|
@ -22,7 +24,7 @@ pub fn t_new(id: Tid, body: Body, argument: Argument) -> Tag {
|
|||
Tag::new(id, body, argument)
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
|
||||
#[derive(AutoFrom, Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
|
||||
#[serde(untagged)]
|
||||
pub enum Body {
|
||||
Text(String),
|
||||
|
@ -30,7 +32,7 @@ pub enum Body {
|
|||
Null,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
|
||||
#[derive(AutoFrom, Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
|
||||
#[serde(untagged)]
|
||||
pub enum Argument {
|
||||
Text(String),
|
||||
|
@ -96,22 +98,7 @@ pub trait IsNull {
|
|||
fn is_null(&self) -> bool;
|
||||
}
|
||||
|
||||
pub trait ToDaletl {
|
||||
pub trait ToDaletlRoot {
|
||||
/// Convert to daletl root
|
||||
fn to_dl(self) -> Vec<Tag>;
|
||||
}
|
||||
|
||||
pub trait ToDaletlTag {
|
||||
/// Convert to daletl tag
|
||||
fn to_dl_tag(self) -> Tag;
|
||||
}
|
||||
|
||||
pub trait ToDaletlBody {
|
||||
/// Convert to daletl body
|
||||
fn to_dl_body(self) -> Body;
|
||||
}
|
||||
|
||||
pub trait ToDaletlArgument {
|
||||
/// Convert to daletl arg
|
||||
fn to_dl_arg(self) -> Argument;
|
||||
fn to_dl_root(self) -> Root;
|
||||
}
|
||||
|
|
185
src/typed.rs
185
src/typed.rs
|
@ -1,10 +1,13 @@
|
|||
use enum_procs::AutoFrom;
|
||||
use num_enum::TryFromPrimitive;
|
||||
|
||||
use crate::daletl::{self, t_new, Tid, ToDaletl, ToDaletlArgument, ToDaletlBody, ToDaletlTag};
|
||||
use crate::daletl::{self, t_new, Tid, ToDaletlRoot};
|
||||
|
||||
const NB: daletl::Body = daletl::Body::Null;
|
||||
const NA: daletl::Argument = daletl::Argument::Null;
|
||||
|
||||
pub type Root = Vec<Tag>;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Tag {
|
||||
El(NNBody),
|
||||
|
@ -39,39 +42,39 @@ pub enum Tag {
|
|||
Pre(String),
|
||||
}
|
||||
|
||||
impl ToDaletlTag for Tag {
|
||||
fn to_dl_tag(self) -> daletl::Tag {
|
||||
match self {
|
||||
Tag::El(b) => t_new(Tid::El, b.to_dl_body(), NA),
|
||||
Tag::H(b, a) => t_new(Tid::H, b.to_dl_body(), a.to_dl_arg()),
|
||||
Tag::P(b) => t_new(Tid::P, b.to_dl_body(), NA),
|
||||
impl From<Tag> for daletl::Tag {
|
||||
fn from(item: Tag) -> daletl::Tag {
|
||||
match item {
|
||||
Tag::El(b) => t_new(Tid::El, b.into(), NA),
|
||||
Tag::H(b, a) => t_new(Tid::H, b.into(), a.into()),
|
||||
Tag::P(b) => t_new(Tid::P, b.into(), NA),
|
||||
Tag::Br => t_new(Tid::Br, NB, NA),
|
||||
Tag::Ul(b) => t_new(Tid::Ul, b.to_dl_body(), NA),
|
||||
Tag::Ol(b) => t_new(Tid::Ol, b.to_dl_body(), NA),
|
||||
Tag::Row(b, a) => t_new(Tid::Row, b.to_dl_body(), a.to_dl_arg()),
|
||||
Tag::Link(b, a) => t_new(Tid::Link, b.to_dl_body(), a.to_dl_arg()),
|
||||
Tag::Navlink(b, a) => t_new(Tid::Navlink, b.to_dl_body(), a.to_dl_arg()),
|
||||
Tag::Btn(b, a) => t_new(Tid::Btn, b.to_dl_body(), a.to_dl_arg()),
|
||||
Tag::Navbtn(b, a) => t_new(Tid::Navbtn, b.to_dl_body(), a.to_dl_arg()),
|
||||
Tag::Img(a) => t_new(Tid::Img, NB, a.to_dl_arg()),
|
||||
Tag::Table(b) => t_new(Tid::Table, b.to_dl_body(), NA),
|
||||
Tag::Tcol(b) => t_new(Tid::Tcol, b.to_dl_body(), NA),
|
||||
Tag::Tpcol(b) => t_new(Tid::Tpcol, b.to_dl_body(), NA),
|
||||
Tag::Ul(b) => t_new(Tid::Ul, b.into(), NA),
|
||||
Tag::Ol(b) => t_new(Tid::Ol, b.into(), NA),
|
||||
Tag::Row(b, a) => t_new(Tid::Row, b.into(), a.into()),
|
||||
Tag::Link(b, a) => t_new(Tid::Link, b.into(), a.into()),
|
||||
Tag::Navlink(b, a) => t_new(Tid::Navlink, b.into(), a.into()),
|
||||
Tag::Btn(b, a) => t_new(Tid::Btn, b.into(), a.into()),
|
||||
Tag::Navbtn(b, a) => t_new(Tid::Navbtn, b.into(), a.into()),
|
||||
Tag::Img(a) => t_new(Tid::Img, NB, a.into()),
|
||||
Tag::Table(b) => t_new(Tid::Table, b.into(), NA),
|
||||
Tag::Tcol(b) => t_new(Tid::Tcol, b.into(), NA),
|
||||
Tag::Tpcol(b) => t_new(Tid::Tpcol, b.into(), NA),
|
||||
Tag::Hr => t_new(Tid::Hr, NB, NA),
|
||||
Tag::B(b) => t_new(Tid::B, b.to_dl_body(), NA),
|
||||
Tag::I(b) => t_new(Tid::I, b.to_dl_body(), NA),
|
||||
Tag::Bq(b) => t_new(Tid::Bq, b.to_dl_body(), NA),
|
||||
Tag::Footlnk(a) => t_new(Tid::Footlnk, NB, a.to_dl_arg()),
|
||||
Tag::Footn(b, a) => t_new(Tid::Footn, b.to_dl_body(), a.to_dl_arg()),
|
||||
Tag::A(a) => t_new(Tid::A, NB, a.to_dl_arg()),
|
||||
Tag::S(b) => t_new(Tid::S, b.to_dl_body(), NA),
|
||||
Tag::Sup(b) => t_new(Tid::Sup, b.to_dl_body(), NA),
|
||||
Tag::Sub(b) => t_new(Tid::Sub, b.to_dl_body(), NA),
|
||||
Tag::Disc(b) => t_new(Tid::Disc, b.to_dl_body(), NA),
|
||||
Tag::Bl(b, a) => t_new(Tid::Bl, b.to_dl_body(), a.to_dl_arg()),
|
||||
Tag::Carousel(b) => t_new(Tid::Carousel, b.to_dl_body(), NA),
|
||||
Tag::Code(s, a) => t_new(Tid::Code, s.to_dl_body(), a.to_dl_arg()),
|
||||
Tag::Pre(s) => t_new(Tid::Pre, s.to_dl_body(), NA),
|
||||
Tag::B(b) => t_new(Tid::B, b.into(), NA),
|
||||
Tag::I(b) => t_new(Tid::I, b.into(), NA),
|
||||
Tag::Bq(b) => t_new(Tid::Bq, b.into(), NA),
|
||||
Tag::Footlnk(a) => t_new(Tid::Footlnk, NB, a.into()),
|
||||
Tag::Footn(b, a) => t_new(Tid::Footn, b.into(), a.into()),
|
||||
Tag::A(a) => t_new(Tid::A, NB, a.into()),
|
||||
Tag::S(b) => t_new(Tid::S, b.into(), NA),
|
||||
Tag::Sup(b) => t_new(Tid::Sup, b.into(), NA),
|
||||
Tag::Sub(b) => t_new(Tid::Sub, b.into(), NA),
|
||||
Tag::Disc(b) => t_new(Tid::Disc, b.into(), NA),
|
||||
Tag::Bl(b, a) => t_new(Tid::Bl, b.into(), a.into()),
|
||||
Tag::Carousel(b) => t_new(Tid::Carousel, b.into(), NA),
|
||||
Tag::Code(s, a) => t_new(Tid::Code, s.into(), a.into()),
|
||||
Tag::Pre(s) => t_new(Tid::Pre, s.into(), NA),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -88,15 +91,15 @@ pub enum Hl {
|
|||
Six,
|
||||
}
|
||||
|
||||
impl ToDaletlArgument for Hl {
|
||||
fn to_dl_arg(self) -> daletl::Argument {
|
||||
match self {
|
||||
impl From<Hl> for daletl::Argument {
|
||||
fn from(item: Hl) -> daletl::Argument {
|
||||
match item {
|
||||
Hl::One => NA,
|
||||
Hl::Two => 2u8.to_dl_arg(),
|
||||
Hl::Three => 3u8.to_dl_arg(),
|
||||
Hl::Four => 4u8.to_dl_arg(),
|
||||
Hl::Five => 5u8.to_dl_arg(),
|
||||
Hl::Six => 6u8.to_dl_arg(),
|
||||
Hl::Two => 2u8.into(),
|
||||
Hl::Three => 3u8.into(),
|
||||
Hl::Four => 4u8.into(),
|
||||
Hl::Five => 5u8.into(),
|
||||
Hl::Six => 6u8.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -109,122 +112,104 @@ pub enum AlignArgument {
|
|||
End,
|
||||
}
|
||||
|
||||
impl ToDaletlArgument for AlignArgument {
|
||||
fn to_dl_arg(self) -> daletl::Argument {
|
||||
match self {
|
||||
Self::Start => NA,
|
||||
Self::Center => 1u8.to_dl_arg(),
|
||||
Self::End => 2u8.to_dl_arg(),
|
||||
impl From<AlignArgument> for daletl::Argument {
|
||||
fn from(item: AlignArgument) -> daletl::Argument {
|
||||
match item {
|
||||
AlignArgument::Start => NA,
|
||||
AlignArgument::Center => 1u8.into(),
|
||||
AlignArgument::End => 2u8.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[derive(AutoFrom, Debug, Clone, PartialEq, Eq)]
|
||||
pub enum TNArgument {
|
||||
Text(String),
|
||||
Null,
|
||||
}
|
||||
|
||||
impl ToDaletlArgument for TNArgument {
|
||||
fn to_dl_arg(self) -> daletl::Argument {
|
||||
match self {
|
||||
Self::Text(s) => s.to_dl_arg(),
|
||||
Self::Null => NA,
|
||||
impl From<TNArgument> for daletl::Argument {
|
||||
fn from(item: TNArgument) -> daletl::Argument {
|
||||
match item {
|
||||
TNArgument::Text(s) => s.into(),
|
||||
TNArgument::Null => NA,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[derive(AutoFrom, Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Body {
|
||||
Text(String),
|
||||
Tags(Vec<Tag>),
|
||||
Null,
|
||||
}
|
||||
|
||||
impl ToDaletlBody for Body {
|
||||
fn to_dl_body(self) -> daletl::Body {
|
||||
match self {
|
||||
impl From<Body> for daletl::Body {
|
||||
fn from(item: Body) -> daletl::Body {
|
||||
match item {
|
||||
Body::Null => NB,
|
||||
Body::Tags(v) => v.to_dl_body(),
|
||||
Body::Text(v) => v.to_dl_body(),
|
||||
Body::Tags(v) => v.into(),
|
||||
Body::Text(v) => v.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[derive(AutoFrom, Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Arg {
|
||||
Text(String),
|
||||
Number(u8),
|
||||
Null,
|
||||
}
|
||||
|
||||
impl ToDaletlArgument for Arg {
|
||||
fn to_dl_arg(self) -> daletl::Argument {
|
||||
match self {
|
||||
impl From<Arg> for daletl::Argument {
|
||||
fn from(item: Arg) -> daletl::Argument {
|
||||
match item {
|
||||
Arg::Null => NA,
|
||||
Arg::Number(v) => v.to_dl_arg(),
|
||||
Arg::Text(v) => v.to_dl_arg(),
|
||||
Arg::Number(v) => v.into(),
|
||||
Arg::Text(v) => v.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[derive(AutoFrom, Debug, Clone, PartialEq, Eq)]
|
||||
/// Not null argument
|
||||
pub enum NNArg {
|
||||
Text(String),
|
||||
Number(u8),
|
||||
}
|
||||
|
||||
impl ToDaletlArgument for NNArg {
|
||||
fn to_dl_arg(self) -> daletl::Argument {
|
||||
match self {
|
||||
NNArg::Number(v) => v.to_dl_arg(),
|
||||
NNArg::Text(v) => v.to_dl_arg(),
|
||||
impl From<NNArg> for daletl::Argument {
|
||||
fn from(item: NNArg) -> daletl::Argument {
|
||||
match item {
|
||||
NNArg::Number(v) => v.into(),
|
||||
NNArg::Text(v) => v.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[derive(AutoFrom, Debug, Clone, PartialEq, Eq)]
|
||||
pub enum NNBody {
|
||||
Text(String),
|
||||
Tags(Vec<Tag>),
|
||||
}
|
||||
|
||||
impl ToDaletlBody for NNBody {
|
||||
fn to_dl_body(self) -> daletl::Body {
|
||||
match self {
|
||||
NNBody::Text(v) => v.to_dl_body(),
|
||||
NNBody::Tags(v) => v.to_dl_body(),
|
||||
impl From<NNBody> for daletl::Body {
|
||||
fn from(item: NNBody) -> daletl::Body {
|
||||
match item {
|
||||
NNBody::Text(v) => v.into(),
|
||||
NNBody::Tags(v) => v.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToDaletlBody for Vec<Tag> {
|
||||
fn to_dl_body(self) -> daletl::Body {
|
||||
daletl::Body::Tags(self.to_dl())
|
||||
impl From<Vec<Tag>> for daletl::Body {
|
||||
fn from(item: Vec<Tag>) -> daletl::Body {
|
||||
daletl::Body::Tags(item.into_iter().map(|tag| tag.into()).collect())
|
||||
}
|
||||
}
|
||||
|
||||
impl ToDaletl for Vec<Tag> {
|
||||
fn to_dl(self) -> Vec<daletl::Tag> {
|
||||
self.into_iter().map(|tag| tag.to_dl_tag()).collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl ToDaletlBody for String {
|
||||
fn to_dl_body(self) -> daletl::Body {
|
||||
daletl::Body::Text(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToDaletlArgument for String {
|
||||
fn to_dl_arg(self) -> daletl::Argument {
|
||||
daletl::Argument::Text(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToDaletlArgument for u8 {
|
||||
fn to_dl_arg(self) -> daletl::Argument {
|
||||
daletl::Argument::Number(self)
|
||||
impl ToDaletlRoot for Root {
|
||||
fn to_dl_root(self) -> daletl::Root {
|
||||
self.into_iter().map(|tag| tag.into()).collect()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue