diff --git a/libs/rust/src/abstractions.rs b/libs/rust/src/abstractions.rs index aea42f1..68aa44f 100644 --- a/libs/rust/src/abstractions.rs +++ b/libs/rust/src/abstractions.rs @@ -1,9 +1,14 @@ 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)] pub enum Tag { El(NotNullBody), - H(String, HeadingArgument), + H(String, HeadingLevel), P(NotNullBody), Br, Ul(Vec), @@ -12,7 +17,7 @@ pub enum Tag { Link(Body, String), Navlink(Body, String), Btn(Body, String), - NavBtn(Body, String), + Navbtn(Body, String), Img(String), Table(Vec), Tcol(Vec), @@ -21,9 +26,9 @@ pub enum Tag { B(String), I(String), Bq(NotNullBody), - Footlnk(StringOrNumberArgument), - Footn(String, StringOrNumberArgument), - A(StringOrNumberArgument), + Footlnk(TextOrNumberArgument), + Footn(String, TextOrNumberArgument), + A(TextOrNumberArgument), S(String), Sup(String), Sub(String), @@ -32,9 +37,56 @@ pub enum Tag { Carousel(Vec), } +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)] #[repr(u8)] -pub enum HeadingArgument { +pub enum HeadingLevel { One, Two, Three, @@ -47,6 +99,23 @@ pub enum HeadingArgument { 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)] #[repr(u8)] pub enum AlignArgument { @@ -55,12 +124,31 @@ pub enum AlignArgument { 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)] -pub enum StringOrNumberArgument { - String(String), +pub enum TextOrNumberArgument { + Text(String), 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)] pub enum Body { Text(String), @@ -68,24 +156,44 @@ pub enum Body { 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)] pub enum Argument { Text(String), - Number(i8), + Number(u8), 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)] pub enum NotNullArgument { Text(String), - Number(i8), + Number(u8), } -impl NotNullArgument { - pub fn to_argument(self) -> Argument { +impl ToDaletlArgument for NotNullArgument { + fn to_daletl_argument(self) -> daletl::Argument { match self { - NotNullArgument::Number(v) => Argument::Number(v), - NotNullArgument::Text(v) => Argument::Text(v), + NotNullArgument::Number(v) => v.to_daletl_argument(), + NotNullArgument::Text(v) => v.to_daletl_argument(), } } } @@ -96,11 +204,35 @@ pub enum NotNullBody { Tags(Vec), } -impl NotNullBody { - pub fn to_body(self) -> Body { +impl ToDaletlBody for NotNullBody { + fn to_daletl_body(self) -> daletl::Body { match self { - NotNullBody::Text(v) => Body::Text(v), - NotNullBody::Tags(v) => Body::Tags(v), + NotNullBody::Text(v) => v.to_daletl_body(), + NotNullBody::Tags(v) => v.to_daletl_body(), } } } + +impl ToDaletlBody for Vec { + 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) + } +} diff --git a/libs/rust/src/daletl.rs b/libs/rust/src/daletl.rs index 2e681f6..52bcf8c 100644 --- a/libs/rust/src/daletl.rs +++ b/libs/rust/src/daletl.rs @@ -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)] #[serde(untagged)] pub enum Body { @@ -29,7 +33,7 @@ pub enum Body { #[serde(untagged)] pub enum Argument { Text(String), - Number(i8), + Number(u8), Null, } diff --git a/specification/tags.md b/specification/tags.md index d67b56f..dd2ca5f 100644 --- a/specification/tags.md +++ b/specification/tags.md @@ -152,7 +152,7 @@ end -> 1 | name | link | | id | 7 | | body | any | -| argument | string | +| argument | text | 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 | | id | 8 | | body | any | -| argument | string | +| argument | text | Link to the same site. On click the link opens in current tab. @@ -186,7 +186,7 @@ navlink[/specification]: I am Navlink | name | btn | | id | 9 | | body | any | -| argument | string | +| argument | text | Same as link, but with button style. @@ -203,7 +203,7 @@ btn[https://example.com]: I am Button | name | navbtn | | id | 9 | | body | any | -| argument | string | +| argument | text | Same as navlink, but with button style. @@ -220,7 +220,7 @@ navbtn[https://example.com]: I am NavButton | name | img | | id | 11 | | body | no | -| argument | string | +| argument | text | Displays an image. @@ -365,7 +365,7 @@ bq: I am Blockquote | name | footlnk | | id | 19 | | body | no | -| argument | string, number | +| argument | text , number | Link to footnote. @@ -382,7 +382,7 @@ footlnk[1] | name | footn | | id | 20 | | body | text | -| argument | string, number | +| argument | text , number | Creates footnote. @@ -399,7 +399,7 @@ footn[1]: I am Footnote | name | a | | id | 21 | | body | no | -| argument | string, number | +| argument | text , number | Creates anchor. Like `` in HTML. @@ -467,7 +467,7 @@ sub: I am Subscript | name | disc | | id | 25 | | body | text, tags | -| argument | string | +| argument | text | Creates disclosure element.