Skip to content

Commit

Permalink
Merge pull request #25 from Lurk/ingest
Browse files Browse the repository at this point in the history
* Revert string fun. Make metadata not optional
* Add test for default metadata
* Make tags in metadata more ergonomic
  • Loading branch information
Lurk authored Sep 10, 2023
2 parents 70e4324 + d881f9e commit 8d3db67
Show file tree
Hide file tree
Showing 26 changed files with 539 additions and 487 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "yamd"
version = "0.7.0"
version = "0.8.0"
edition = "2021"
license = "MIT OR Apache-2.0"
description = "Yet Another Markdown Document (flavor)"
Expand Down
32 changes: 16 additions & 16 deletions src/nodes/accordion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ use crate::toolkit::{
use super::accordion_tab::AccordionTab;

#[derive(Debug, PartialEq)]
pub enum AccordionNodes<'text> {
AccordionTab(AccordionTab<'text>),
pub enum AccordionNodes {
AccordionTab(AccordionTab),
}

impl Node<'_> for AccordionNodes<'_> {
impl Node for AccordionNodes {
fn serialize(&self) -> String {
match self {
AccordionNodes::AccordionTab(tab) => tab.serialize(),
Expand All @@ -26,32 +26,32 @@ impl Node<'_> for AccordionNodes<'_> {
}
}

impl<'text> From<AccordionTab<'text>> for AccordionNodes<'text> {
fn from(tab: AccordionTab<'text>) -> Self {
impl From<AccordionTab> for AccordionNodes {
fn from(tab: AccordionTab) -> Self {
AccordionNodes::AccordionTab(tab)
}
}

#[derive(Debug, PartialEq)]
pub struct Accordion<'text> {
pub struct Accordion {
consumed_all_input: bool,
pub nodes: Vec<AccordionNodes<'text>>,
pub nodes: Vec<AccordionNodes>,
}

impl<'text> Accordion<'text> {
impl Accordion {
pub fn new(consumed_all_input: bool) -> Self {
Self::new_with_nodes(consumed_all_input, vec![])
}

pub fn new_with_nodes(consumed_all_input: bool, nodes: Vec<AccordionNodes<'text>>) -> Self {
pub fn new_with_nodes(consumed_all_input: bool, nodes: Vec<AccordionNodes>) -> Self {
Accordion {
consumed_all_input,
nodes,
}
}
}

impl Node<'_> for Accordion<'_> {
impl Node for Accordion {
fn serialize(&self) -> String {
format!(
"///\n{nodes}\n\\\\\\{end}",
Expand All @@ -70,16 +70,16 @@ impl Node<'_> for Accordion<'_> {
}
}

impl<'text> Branch<'text, AccordionNodes<'text>> for Accordion<'text> {
fn push<CanBeNode: Into<AccordionNodes<'text>>>(&mut self, node: CanBeNode) {
impl Branch<AccordionNodes> for Accordion {
fn push<CanBeNode: Into<AccordionNodes>>(&mut self, node: CanBeNode) {
self.nodes.push(node.into());
}

fn get_maybe_nodes() -> Vec<MaybeNode<'text, AccordionNodes<'text>>> {
fn get_maybe_nodes() -> Vec<MaybeNode<AccordionNodes>> {
vec![AccordionTab::maybe_node()]
}

fn get_fallback_node() -> Option<DefinitelyNode<'text, AccordionNodes<'text>>> {
fn get_fallback_node() -> Option<DefinitelyNode<AccordionNodes>> {
None
}

Expand All @@ -88,8 +88,8 @@ impl<'text> Branch<'text, AccordionNodes<'text>> for Accordion<'text> {
}
}

impl<'text> Deserializer<'text> for Accordion<'text> {
fn deserialize_with_context(input: &'text str, _: Option<Context>) -> Option<Self>
impl Deserializer for Accordion {
fn deserialize_with_context(input: &str, _: Option<Context>) -> Option<Self>
where
Self: Sized,
{
Expand Down
102 changes: 51 additions & 51 deletions src/nodes/accordion_tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ use super::{
};

#[derive(Debug, PartialEq)]
pub enum AccordionTabNodes<'text> {
Pargaraph(Paragraph<'text>),
Heading(Heading<'text>),
Image(Image<'text>),
ImageGallery(ImageGallery<'text>),
CloudinaryImageGallery(CloudinaryImageGallery<'text>),
List(List<'text>),
Embed(Embed<'text>),
Accordion(Accordion<'text>),
pub enum AccordionTabNodes {
Pargaraph(Paragraph),
Heading(Heading),
Image(Image),
ImageGallery(ImageGallery),
CloudinaryImageGallery(CloudinaryImageGallery),
List(List),
Embed(Embed),
Accordion(Accordion),
Divider(Divider),
Code(Code<'text>),
Code(Code),
}

impl Node<'_> for AccordionTabNodes<'_> {
impl Node for AccordionTabNodes {
fn serialize(&self) -> String {
match self {
AccordionTabNodes::Pargaraph(node) => node.serialize(),
Expand Down Expand Up @@ -57,91 +57,91 @@ impl Node<'_> for AccordionTabNodes<'_> {
}
}

impl<'text> From<Paragraph<'text>> for AccordionTabNodes<'text> {
fn from(value: Paragraph<'text>) -> Self {
impl From<Paragraph> for AccordionTabNodes {
fn from(value: Paragraph) -> Self {
Self::Pargaraph(value)
}
}

impl<'text> From<Heading<'text>> for AccordionTabNodes<'text> {
fn from(value: Heading<'text>) -> Self {
impl From<Heading> for AccordionTabNodes {
fn from(value: Heading) -> Self {
Self::Heading(value)
}
}

impl<'text> From<Image<'text>> for AccordionTabNodes<'text> {
fn from(value: Image<'text>) -> Self {
impl From<Image> for AccordionTabNodes {
fn from(value: Image) -> Self {
Self::Image(value)
}
}

impl<'text> From<ImageGallery<'text>> for AccordionTabNodes<'text> {
fn from(value: ImageGallery<'text>) -> Self {
impl From<ImageGallery> for AccordionTabNodes {
fn from(value: ImageGallery) -> Self {
Self::ImageGallery(value)
}
}

impl<'text> From<CloudinaryImageGallery<'text>> for AccordionTabNodes<'text> {
fn from(value: CloudinaryImageGallery<'text>) -> Self {
impl From<CloudinaryImageGallery> for AccordionTabNodes {
fn from(value: CloudinaryImageGallery) -> Self {
Self::CloudinaryImageGallery(value)
}
}

impl<'text> From<List<'text>> for AccordionTabNodes<'text> {
fn from(value: List<'text>) -> Self {
impl From<List> for AccordionTabNodes {
fn from(value: List) -> Self {
Self::List(value)
}
}

impl<'text> From<Embed<'text>> for AccordionTabNodes<'text> {
fn from(value: Embed<'text>) -> Self {
impl From<Embed> for AccordionTabNodes {
fn from(value: Embed) -> Self {
Self::Embed(value)
}
}

impl<'text> From<Accordion<'text>> for AccordionTabNodes<'text> {
fn from(value: Accordion<'text>) -> Self {
impl From<Accordion> for AccordionTabNodes {
fn from(value: Accordion) -> Self {
Self::Accordion(value)
}
}

impl From<Divider> for AccordionTabNodes<'_> {
impl From<Divider> for AccordionTabNodes {
fn from(value: Divider) -> Self {
Self::Divider(value)
}
}

impl<'text> From<Code<'text>> for AccordionTabNodes<'text> {
fn from(value: Code<'text>) -> Self {
impl From<Code> for AccordionTabNodes {
fn from(value: Code) -> Self {
Self::Code(value)
}
}

#[derive(Debug, PartialEq)]
pub struct AccordionTab<'text> {
pub header: Option<&'text str>,
pub nodes: Vec<AccordionTabNodes<'text>>,
pub struct AccordionTab {
pub header: Option<String>,
pub nodes: Vec<AccordionTabNodes>,
consumed_all_input: bool,
}

impl<'text> AccordionTab<'text> {
pub fn new(consumed_all_input: bool, header: Option<&'text str>) -> Self {
impl AccordionTab {
pub fn new<S: Into<String>>(consumed_all_input: bool, header: Option<S>) -> Self {
Self::new_with_nodes(consumed_all_input, header, vec![])
}
pub fn new_with_nodes(
pub fn new_with_nodes<S: Into<String>>(
consumed_all_input: bool,
header: Option<&'text str>,
nodes: Vec<AccordionTabNodes<'text>>,
header: Option<S>,
nodes: Vec<AccordionTabNodes>,
) -> Self {
Self {
nodes,
consumed_all_input,
header,
header: header.map(|s| s.into()),
}
}
}

impl Node<'_> for AccordionTab<'_> {
impl Node for AccordionTab {
fn serialize(&self) -> String {
format!(
"//\n{header}{nodes}\n\\\\{end}",
Expand All @@ -164,12 +164,12 @@ impl Node<'_> for AccordionTab<'_> {
}
}

impl<'text> Branch<'text, AccordionTabNodes<'text>> for AccordionTab<'text> {
fn push<CanBeNode: Into<AccordionTabNodes<'text>>>(&mut self, node: CanBeNode) {
impl Branch<AccordionTabNodes> for AccordionTab {
fn push<CanBeNode: Into<AccordionTabNodes>>(&mut self, node: CanBeNode) {
self.nodes.push(node.into());
}

fn get_maybe_nodes() -> Vec<MaybeNode<'text, AccordionTabNodes<'text>>> {
fn get_maybe_nodes() -> Vec<MaybeNode<AccordionTabNodes>> {
vec![
Heading::maybe_node(),
Image::maybe_node(),
Expand All @@ -183,7 +183,7 @@ impl<'text> Branch<'text, AccordionTabNodes<'text>> for AccordionTab<'text> {
]
}

fn get_fallback_node() -> Option<DefinitelyNode<'text, AccordionTabNodes<'text>>> {
fn get_fallback_node() -> Option<DefinitelyNode<AccordionTabNodes>> {
Some(Paragraph::fallback_node())
}

Expand All @@ -193,8 +193,8 @@ impl<'text> Branch<'text, AccordionTabNodes<'text>> for AccordionTab<'text> {
}
}

impl<'text> Deserializer<'text> for AccordionTab<'text> {
fn deserialize_with_context(input: &'text str, _: Option<Context>) -> Option<Self> {
impl Deserializer for AccordionTab {
fn deserialize_with_context(input: &str, _: Option<Context>) -> Option<Self> {
let mut matcher = Matcher::new(input);
if let Some(tab) = matcher.get_match("//\n", "\n\\\\", false) {
let mut inner_matcher = Matcher::new(tab.body);
Expand Down Expand Up @@ -243,7 +243,7 @@ mod cfg {
fn test_accordion_tab_deserialize_with_no_header() {
assert_eq!(
AccordionTab::deserialize("//\nI am regular text\n\\\\\n\n"),
Some(AccordionTab::new_with_nodes(
Some(AccordionTab::new_with_nodes::<&str>(
false,
None,
vec![
Expand All @@ -258,7 +258,7 @@ mod cfg {
fn test_accordion_tab_deserialize_with_no_header_and_no_newline() {
assert_eq!(
AccordionTab::deserialize("//\n![alt](url)\n\n\\\\"),
Some(AccordionTab::new_with_nodes(
Some(AccordionTab::new_with_nodes::<&str>(
true,
None,
vec![Image::new(true, "alt", "url").into()]
Expand Down Expand Up @@ -353,13 +353,13 @@ t**b**
],
)
.into(),
Image::new(false, "a", "u").into(),
Image::new(false, 'a', 'u').into(),
ImageGallery::new_with_nodes(
false,
vec![
Image::new(true, "a", "u").into(),
Image::new(true, "a2", "u2").into(),
],
false,
)
.into(),
Divider::new(false).into(),
Expand Down Expand Up @@ -389,7 +389,7 @@ t**b**
.into()],
)
.into(),
Embed::new(false, "youtube", "123").into(),
Embed::new("youtube", "123", false).into(),
CloudinaryImageGallery::new("username", "tag", true).into(),
],
);
Expand Down
21 changes: 12 additions & 9 deletions src/nodes/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ use crate::{

/// Representation of an anchor
#[derive(Debug, PartialEq)]
pub struct Anchor<'text> {
pub text: &'text str,
pub url: &'text str,
pub struct Anchor {
pub text: String,
pub url: String,
}

impl<'text> Anchor<'text> {
pub fn new(text: &'text str, url: &'text str) -> Self {
Anchor { text, url }
impl Anchor {
pub fn new<S: Into<String>>(text: S, url: S) -> Self {
Anchor {
text: text.into(),
url: url.into(),
}
}
}

impl<'text> Node<'text> for Anchor<'text> {
impl Node for Anchor {
fn serialize(&self) -> String {
format!("[{}]({})", self.text, self.url)
}
Expand All @@ -26,8 +29,8 @@ impl<'text> Node<'text> for Anchor<'text> {
}
}

impl<'text> Deserializer<'text> for Anchor<'text> {
fn deserialize_with_context(input: &'text str, _: Option<Context>) -> Option<Self> {
impl Deserializer for Anchor {
fn deserialize_with_context(input: &str, _: Option<Context>) -> Option<Self> {
let mut matcher = Matcher::new(input);
if let Some(text) = matcher.get_match("[", "]", false) {
if let Some(url) = matcher.get_match("(", ")", false) {
Expand Down
Loading

0 comments on commit 8d3db67

Please sign in to comment.