feat(rust): abstract tag to daletl

This commit is contained in:
Artemy Egorov 2024-08-01 18:22:40 +03:00
parent f2a30d0289
commit 3b758d82aa
3 changed files with 164 additions and 28 deletions

View file

@ -1,9 +1,14 @@
use num_enum::TryFromPrimitive; use num_enum::TryFromPrimitive;
use crate::daletl::{self, t_new, Tid};
const BN: daletl::Body = daletl::Body::Null;
const AN: daletl::Argument = daletl::Argument::Null;
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum Tag { pub enum Tag {
El(NotNullBody), El(NotNullBody),
H(String, HeadingArgument), H(String, HeadingLevel),
P(NotNullBody), P(NotNullBody),
Br, Br,
Ul(Vec<Tag>), Ul(Vec<Tag>),
@ -12,7 +17,7 @@ pub enum Tag {
Link(Body, String), Link(Body, String),
Navlink(Body, String), Navlink(Body, String),
Btn(Body, String), Btn(Body, String),
NavBtn(Body, String), Navbtn(Body, String),
Img(String), Img(String),
Table(Vec<Tag>), Table(Vec<Tag>),
Tcol(Vec<Tag>), Tcol(Vec<Tag>),
@ -21,9 +26,9 @@ pub enum Tag {
B(String), B(String),
I(String), I(String),
Bq(NotNullBody), Bq(NotNullBody),
Footlnk(StringOrNumberArgument), Footlnk(TextOrNumberArgument),
Footn(String, StringOrNumberArgument), Footn(String, TextOrNumberArgument),
A(StringOrNumberArgument), A(TextOrNumberArgument),
S(String), S(String),
Sup(String), Sup(String),
Sub(String), Sub(String),
@ -32,9 +37,56 @@ pub enum Tag {
Carousel(Vec<Tag>), Carousel(Vec<Tag>),
} }
pub trait ToDaletlTag {
fn to_daletl_tag(self) -> daletl::Tag;
}
impl ToDaletlTag for Tag {
fn to_daletl_tag(self) -> daletl::Tag {
match self {
Tag::El(b) => t_new(Tid::El, b.to_daletl_body(), AN),
Tag::H(b, a) => t_new(Tid::H, b.to_daletl_body(), a.to_daletl_argument()),
Tag::P(b) => t_new(Tid::P, b.to_daletl_body(), AN),
Tag::Br => t_new(Tid::Br, BN, AN),
Tag::Ul(b) => t_new(Tid::Ul, b.to_daletl_body(), AN),
Tag::Ol(b) => t_new(Tid::Ol, b.to_daletl_body(), AN),
Tag::Row(b, a) => t_new(Tid::Row, b.to_daletl_body(), a.to_daletl_argument()),
Tag::Link(b, a) => t_new(Tid::Link, b.to_daletl_body(), a.to_daletl_argument()),
Tag::Navlink(b, a) => t_new(Tid::Navlink, b.to_daletl_body(), a.to_daletl_argument()),
Tag::Btn(b, a) => t_new(Tid::Btn, b.to_daletl_body(), a.to_daletl_argument()),
Tag::Navbtn(b, a) => t_new(Tid::Navbtn, b.to_daletl_body(), a.to_daletl_argument()),
Tag::Img(a) => t_new(Tid::Img, BN, a.to_daletl_argument()),
Tag::Table(b) => t_new(Tid::Table, b.to_daletl_body(), AN),
Tag::Tcol(b) => t_new(Tid::Tcol, b.to_daletl_body(), AN),
Tag::Tpcol(b) => t_new(Tid::Tpcol, b.to_daletl_body(), AN),
Tag::Hr => t_new(Tid::Hr, BN, AN),
Tag::B(b) => t_new(Tid::B, b.to_daletl_body(), AN),
Tag::I(b) => t_new(Tid::I, b.to_daletl_body(), AN),
Tag::Bq(b) => t_new(Tid::Bq, b.to_daletl_body(), AN),
Tag::Footlnk(a) => t_new(Tid::Footlnk, BN, a.to_daletl_argument()),
Tag::Footn(b, a) => t_new(Tid::Footn, b.to_daletl_body(), a.to_daletl_argument()),
Tag::A(a) => t_new(Tid::A, BN, a.to_daletl_argument()),
Tag::S(b) => t_new(Tid::S, b.to_daletl_body(), AN),
Tag::Sup(b) => t_new(Tid::Sup, b.to_daletl_body(), AN),
Tag::Sub(b) => t_new(Tid::Sub, b.to_daletl_body(), AN),
Tag::Disc(b) => t_new(Tid::Disc, b.to_daletl_body(), AN),
Tag::Bl(b, a) => t_new(Tid::Bl, b.to_daletl_body(), a.to_daletl_argument()),
Tag::Carousel(b) => t_new(Tid::Disc, b.to_daletl_body(), AN),
}
}
}
pub trait ToDaletlBody {
fn to_daletl_body(self) -> daletl::Body;
}
pub trait ToDaletlArgument {
fn to_daletl_argument(self) -> daletl::Argument;
}
#[derive(Debug, Clone, PartialEq, Eq, TryFromPrimitive)] #[derive(Debug, Clone, PartialEq, Eq, TryFromPrimitive)]
#[repr(u8)] #[repr(u8)]
pub enum HeadingArgument { pub enum HeadingLevel {
One, One,
Two, Two,
Three, Three,
@ -47,6 +99,23 @@ pub enum HeadingArgument {
Ten, Ten,
} }
impl ToDaletlArgument for HeadingLevel {
fn to_daletl_argument(self) -> daletl::Argument {
match self {
HeadingLevel::One => 1u8.to_daletl_argument(),
HeadingLevel::Two => 2u8.to_daletl_argument(),
HeadingLevel::Three => 3u8.to_daletl_argument(),
HeadingLevel::Four => 4u8.to_daletl_argument(),
HeadingLevel::Five => 5u8.to_daletl_argument(),
HeadingLevel::Six => 6u8.to_daletl_argument(),
HeadingLevel::Seven => 7u8.to_daletl_argument(),
HeadingLevel::Eight => 8u8.to_daletl_argument(),
HeadingLevel::Nine => 9u8.to_daletl_argument(),
HeadingLevel::Ten => 10u8.to_daletl_argument(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, TryFromPrimitive)] #[derive(Debug, Clone, PartialEq, Eq, TryFromPrimitive)]
#[repr(u8)] #[repr(u8)]
pub enum AlignArgument { pub enum AlignArgument {
@ -55,12 +124,31 @@ pub enum AlignArgument {
End, End,
} }
impl ToDaletlArgument for AlignArgument {
fn to_daletl_argument(self) -> daletl::Argument {
match self {
Self::Start => 0u8.to_daletl_argument(),
Self::Center => 1u8.to_daletl_argument(),
Self::End => 2u8.to_daletl_argument(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum StringOrNumberArgument { pub enum TextOrNumberArgument {
String(String), Text(String),
Number(u8), Number(u8),
} }
impl ToDaletlArgument for TextOrNumberArgument {
fn to_daletl_argument(self) -> daletl::Argument {
match self {
Self::Number(n) => n.to_daletl_argument(),
Self::Text(s) => s.to_daletl_argument(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum Body { pub enum Body {
Text(String), Text(String),
@ -68,24 +156,44 @@ pub enum Body {
Null, Null,
} }
impl ToDaletlBody for Body {
fn to_daletl_body(self) -> daletl::Body {
match self {
Body::Null => daletl::Body::Null,
Body::Tags(v) => v.to_daletl_body(),
Body::Text(v) => v.to_daletl_body(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum Argument { pub enum Argument {
Text(String), Text(String),
Number(i8), Number(u8),
Null, Null,
} }
impl ToDaletlArgument for Argument {
fn to_daletl_argument(self) -> daletl::Argument {
match self {
Argument::Null => daletl::Argument::Null,
Argument::Number(v) => v.to_daletl_argument(),
Argument::Text(v) => v.to_daletl_argument(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum NotNullArgument { pub enum NotNullArgument {
Text(String), Text(String),
Number(i8), Number(u8),
} }
impl NotNullArgument { impl ToDaletlArgument for NotNullArgument {
pub fn to_argument(self) -> Argument { fn to_daletl_argument(self) -> daletl::Argument {
match self { match self {
NotNullArgument::Number(v) => Argument::Number(v), NotNullArgument::Number(v) => v.to_daletl_argument(),
NotNullArgument::Text(v) => Argument::Text(v), NotNullArgument::Text(v) => v.to_daletl_argument(),
} }
} }
} }
@ -96,11 +204,35 @@ pub enum NotNullBody {
Tags(Vec<Tag>), Tags(Vec<Tag>),
} }
impl NotNullBody { impl ToDaletlBody for NotNullBody {
pub fn to_body(self) -> Body { fn to_daletl_body(self) -> daletl::Body {
match self { match self {
NotNullBody::Text(v) => Body::Text(v), NotNullBody::Text(v) => v.to_daletl_body(),
NotNullBody::Tags(v) => Body::Tags(v), NotNullBody::Tags(v) => v.to_daletl_body(),
} }
} }
} }
impl ToDaletlBody for Vec<Tag> {
fn to_daletl_body(self) -> daletl::Body {
daletl::Body::Tags(self.into_iter().map(|tag| tag.to_daletl_tag()).collect())
}
}
impl ToDaletlBody for String {
fn to_daletl_body(self) -> daletl::Body {
daletl::Body::Text(self)
}
}
impl ToDaletlArgument for String {
fn to_daletl_argument(self) -> daletl::Argument {
daletl::Argument::Text(self)
}
}
impl ToDaletlArgument for u8 {
fn to_daletl_argument(self) -> daletl::Argument {
daletl::Argument::Number(self)
}
}

View file

@ -17,6 +17,10 @@ impl Tag {
} }
} }
pub fn t_new(id: Tid, body: Body, argument: Argument) -> Tag {
Tag::new(id, body, argument)
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(untagged)] #[serde(untagged)]
pub enum Body { pub enum Body {
@ -29,7 +33,7 @@ pub enum Body {
#[serde(untagged)] #[serde(untagged)]
pub enum Argument { pub enum Argument {
Text(String), Text(String),
Number(i8), Number(u8),
Null, Null,
} }

View file

@ -152,7 +152,7 @@ end -> 1
| name | link | | name | link |
| id | 7 | | id | 7 |
| body | any | | body | any |
| argument | string | | argument | text |
Link to other sites. On click the link opens in new tab. Link to other sites. On click the link opens in new tab.
@ -169,7 +169,7 @@ link[https://example.com]: I am Link
| name | navlink | | name | navlink |
| id | 8 | | id | 8 |
| body | any | | body | any |
| argument | string | | argument | text |
Link to the same site. On click the link opens in current tab. Link to the same site. On click the link opens in current tab.
@ -186,7 +186,7 @@ navlink[/specification]: I am Navlink
| name | btn | | name | btn |
| id | 9 | | id | 9 |
| body | any | | body | any |
| argument | string | | argument | text |
Same as link, but with button style. Same as link, but with button style.
@ -203,7 +203,7 @@ btn[https://example.com]: I am Button
| name | navbtn | | name | navbtn |
| id | 9 | | id | 9 |
| body | any | | body | any |
| argument | string | | argument | text |
Same as navlink, but with button style. Same as navlink, but with button style.
@ -220,7 +220,7 @@ navbtn[https://example.com]: I am NavButton
| name | img | | name | img |
| id | 11 | | id | 11 |
| body | no | | body | no |
| argument | string | | argument | text |
Displays an image. Displays an image.
@ -365,7 +365,7 @@ bq: I am Blockquote
| name | footlnk | | name | footlnk |
| id | 19 | | id | 19 |
| body | no | | body | no |
| argument | string, number | | argument | text , number |
Link to footnote. Link to footnote.
@ -382,7 +382,7 @@ footlnk[1]
| name | footn | | name | footn |
| id | 20 | | id | 20 |
| body | text | | body | text |
| argument | string, number | | argument | text , number |
Creates footnote. Creates footnote.
@ -399,7 +399,7 @@ footn[1]: I am Footnote
| name | a | | name | a |
| id | 21 | | id | 21 |
| body | no | | body | no |
| argument | string, number | | argument | text , number |
Creates anchor. Like `<a href="#argument"></a>` in HTML. Creates anchor. Like `<a href="#argument"></a>` in HTML.
@ -467,7 +467,7 @@ sub: I am Subscript
| name | disc | | name | disc |
| id | 25 | | id | 25 |
| body | text, tags | | body | text, tags |
| argument | string | | argument | text |
Creates disclosure element. Creates disclosure element.