From 7ff2a84becc4b08f762dcad7e34c7d656b1df2fa Mon Sep 17 00:00:00 2001 From: Yuval Shavit Date: Thu, 8 Aug 2024 20:23:00 -0400 Subject: [PATCH] rename 'a -> 'md for markdown objects --- src/fmt_md.rs | 34 +++++++------- src/fmt_md_inlines.rs | 46 +++++++++--------- src/link_transform.rs | 36 +++++++------- src/select/api.rs | 11 ++--- src/select/match_selector.rs | 6 +-- src/select/sel_table.rs | 4 +- src/tree.rs | 8 ++-- src/tree_ref.rs | 91 ++++++++++++++++++------------------ src/tree_ref_serde.rs | 66 +++++++++++++------------- 9 files changed, 150 insertions(+), 152 deletions(-) diff --git a/src/fmt_md.rs b/src/fmt_md.rs index ac54fcd..4272260 100644 --- a/src/fmt_md.rs +++ b/src/fmt_md.rs @@ -63,9 +63,9 @@ impl Default for ReferencePlacement { } } -pub fn write_md<'a, I, W>(options: &'a MdOptions, out: &mut Output, nodes: I) +pub fn write_md<'md, I, W>(options: &'md MdOptions, out: &mut Output, nodes: I) where - I: Iterator>, + I: Iterator>, W: SimpleWrite, { let mut writer_state = MdWriterState { @@ -84,16 +84,16 @@ where writer_state.write_definitions(out, DefinitionsToWrite::Both, nodes_count > 1); } -struct MdWriterState<'s, 'a> { - opts: &'a MdOptions, +struct MdWriterState<'s, 'md> { + opts: &'md MdOptions, prev_was_thematic_break: bool, - inlines_writer: &'s mut MdInlinesWriter<'a>, + inlines_writer: &'s mut MdInlinesWriter<'md>, } -impl<'s, 'a> MdWriterState<'s, 'a> { +impl<'s, 'md> MdWriterState<'s, 'md> { fn write_md(&mut self, out: &mut Output, nodes: I, add_break: bool) -> usize where - I: Iterator>, + I: Iterator>, W: SimpleWrite, { let mut count = 0; @@ -108,7 +108,7 @@ impl<'s, 'a> MdWriterState<'s, 'a> { count } - pub fn write_one_md(&mut self, out: &mut Output, node_ref: MdElemRef<'a>) + pub fn write_one_md(&mut self, out: &mut Output, node_ref: MdElemRef<'md>) where W: SimpleWrite, { @@ -169,19 +169,19 @@ impl<'s, 'a> MdWriterState<'s, 'a> { } } - fn write_paragraph(&mut self, out: &mut Output, paragraph: &'a Paragraph) { + fn write_paragraph(&mut self, out: &mut Output, paragraph: &'md Paragraph) { out.with_block(Block::Plain, |out| { self.inlines_writer.write_line(out, ¶graph.body); }); } - fn write_block_quote(&mut self, out: &mut Output, block: &'a BlockQuote) { + fn write_block_quote(&mut self, out: &mut Output, block: &'md BlockQuote) { out.with_block(Block::Quote, |out| { self.write_md(out, Self::doc_iter(&block.body), false); }); } - fn write_list(&mut self, out: &mut Output, list: &'a List) { + fn write_list(&mut self, out: &mut Output, list: &'md List) { out.with_block(Block::Plain, |out| { let mut index = list.starting_index; // let mut prefix = String::with_capacity(8); // enough for "12. [ ] " @@ -194,7 +194,7 @@ impl<'s, 'a> MdWriterState<'s, 'a> { }); } - fn write_table(&mut self, out: &mut Output, table: TableSlice<'a>) { + fn write_table(&mut self, out: &mut Output, table: TableSlice<'md>) { let alignments = table.alignments(); let rows = table.rows(); @@ -350,7 +350,7 @@ impl<'s, 'a> MdWriterState<'s, 'a> { }); } - fn write_list_item(&mut self, out: &mut Output, index: &Option, item: &'a ListItem) { + fn write_list_item(&mut self, out: &mut Output, index: &Option, item: &'md ListItem) { let mut counting_writer = CountingWriter::wrap(out); match index { None => std::fmt::Write::write_str(&mut counting_writer, "- ").unwrap(), @@ -434,13 +434,13 @@ impl<'s, 'a> MdWriterState<'s, 'a> { }); } - fn line_to_string(&mut self, line: &'a Line) -> String { + fn line_to_string(&mut self, line: &'md Line) -> String { let mut out = Output::new(String::with_capacity(line.len() * 10)); // rough guess self.inlines_writer.write_line(&mut out, line); out.take_underlying().unwrap() } - fn doc_iter(body: &'a Vec) -> impl Iterator> { + fn doc_iter(body: &'md Vec) -> impl Iterator> { [MdElemRef::Doc(body)].into_iter() } } @@ -830,7 +830,7 @@ pub mod tests { create_li_singleton(Some(3), Some(true), md_elems!("plain text"), "3. [x] plain text"); } - fn create_li_singleton<'a>(idx: Option, checked: Option, item: Vec, expected: &str) { + fn create_li_singleton(idx: Option, checked: Option, item: Vec, expected: &str) { let li = ListItem { checked, item }; check_render_refs(vec![MdElemRef::ListItem(ListItemRef(idx, &li))], expected) } @@ -2120,7 +2120,7 @@ pub mod tests { check_render_refs_with(&default_opts(), nodes, expect) } - fn check_render_refs_with<'a>(options: &MdOptions, nodes: Vec>, expect: &str) { + fn check_render_refs_with(options: &MdOptions, nodes: Vec, expect: &str) { nodes.iter().for_each(|n| VARIANTS_CHECKER.see(n)); let mut out = Output::new(String::default()); diff --git a/src/fmt_md_inlines.rs b/src/fmt_md_inlines.rs index 8b52326..ce6eeca 100644 --- a/src/fmt_md_inlines.rs +++ b/src/fmt_md_inlines.rs @@ -13,19 +13,19 @@ pub struct MdInlinesWriterOptions { pub link_format: LinkTransform, } -pub struct MdInlinesWriter<'a> { - seen_links: HashSet>, - seen_footnotes: HashSet<&'a String>, - pending_references: PendingReferences<'a>, +pub struct MdInlinesWriter<'md> { + seen_links: HashSet>, + seen_footnotes: HashSet<&'md String>, + pending_references: PendingReferences<'md>, link_transformer: LinkTransformer, } -struct PendingReferences<'a> { - pub links: HashMap, UrlAndTitle<'a>>, - pub footnotes: HashMap<&'a String, &'a Vec>, +struct PendingReferences<'md> { + pub links: HashMap, UrlAndTitle<'md>>, + pub footnotes: HashMap<&'md String, &'md Vec>, } -impl<'a> PendingReferences<'a> { +impl<'md> PendingReferences<'md> { fn with_capacity(capacity: usize) -> Self { Self { links: HashMap::with_capacity(capacity), @@ -35,10 +35,10 @@ impl<'a> PendingReferences<'a> { } #[derive(Serialize, Debug, PartialEq, Eq, Copy, Clone, Hash)] -pub struct UrlAndTitle<'a> { - pub url: &'a String, +pub struct UrlAndTitle<'md> { + pub url: &'md String, #[serde(skip_serializing_if = "Option::is_none")] - pub title: &'a Option, + pub title: &'md Option, } #[derive(Debug, Copy, Clone)] @@ -47,18 +47,18 @@ pub enum LinkLikeType { Image, } -pub trait LinkLike<'a> { - fn link_info(&self) -> (LinkLikeType, LinkLabel<'a>, &'a LinkDefinition); +pub trait LinkLike<'md> { + fn link_info(&self) -> (LinkLikeType, LinkLabel<'md>, &'md LinkDefinition); } -impl<'a> LinkLike<'a> for &'a Link { - fn link_info(&self) -> (LinkLikeType, LinkLabel<'a>, &'a LinkDefinition) { +impl<'md> LinkLike<'md> for &'md Link { + fn link_info(&self) -> (LinkLikeType, LinkLabel<'md>, &'md LinkDefinition) { (LinkLikeType::Link, LinkLabel::Inline(&self.text), &self.link_definition) } } -impl<'a> LinkLike<'a> for &'a Image { - fn link_info(&self) -> (LinkLikeType, LinkLabel<'a>, &'a LinkDefinition) { +impl<'md> LinkLike<'md> for &'md Image { + fn link_info(&self) -> (LinkLikeType, LinkLabel<'md>, &'md LinkDefinition) { ( LinkLikeType::Image, LinkLabel::Text(Cow::Borrowed(&self.alt)), @@ -67,7 +67,7 @@ impl<'a> LinkLike<'a> for &'a Image { } } -impl<'a> MdInlinesWriter<'a> { +impl<'md> MdInlinesWriter<'md> { pub fn new(options: MdInlinesWriterOptions) -> Self { let pending_refs_capacity = 8; // arbitrary guess Self { @@ -94,17 +94,17 @@ impl<'a> MdInlinesWriter<'a> { self.pending_references.footnotes.len() } - pub fn drain_pending_links(&mut self) -> Vec<(LinkLabel<'a>, UrlAndTitle<'a>)> { + pub fn drain_pending_links(&mut self) -> Vec<(LinkLabel<'md>, UrlAndTitle<'md>)> { self.pending_references.links.drain().collect() } - pub fn drain_pending_footnotes(&mut self) -> Vec<(&'a String, &'a Vec)> { + pub fn drain_pending_footnotes(&mut self) -> Vec<(&'md String, &'md Vec)> { self.pending_references.footnotes.drain().collect() } pub fn write_line(&mut self, out: &mut Output, elems: I) where - I: IntoIterator, + I: IntoIterator, W: SimpleWrite, { for elem in elems { @@ -112,7 +112,7 @@ impl<'a> MdInlinesWriter<'a> { } } - pub fn write_inline_element(&mut self, out: &mut Output, elem: &'a Inline) + pub fn write_inline_element(&mut self, out: &mut Output, elem: &'md Inline) where W: SimpleWrite, { @@ -165,7 +165,7 @@ impl<'a> MdInlinesWriter<'a> { pub fn write_linklike(&mut self, out: &mut Output, link_like: L) where W: SimpleWrite, - L: LinkLike<'a> + Copy, + L: LinkLike<'md> + Copy, { let (link_type, label, link) = link_like.link_info(); if matches!(link_type, LinkLikeType::Image) { diff --git a/src/link_transform.rs b/src/link_transform.rs index ae0d696..d2aff34 100644 --- a/src/link_transform.rs +++ b/src/link_transform.rs @@ -27,12 +27,12 @@ pub struct LinkTransformer { } #[derive(Debug, PartialEq, Eq, Hash, Clone)] -pub enum LinkLabel<'a> { - Text(Cow<'a, str>), - Inline(&'a Vec), +pub enum LinkLabel<'md> { + Text(Cow<'md, str>), + Inline(&'md Vec), } -impl<'a> LinkLabel<'a> { +impl<'md> LinkLabel<'md> { pub fn get_sort_string(&self) -> String { // There may be a way to Cow this so that we don't have to copy the ::Text string, but I can't find it. match self { @@ -64,8 +64,8 @@ enum LinkTransformState { Reference(ReferenceAssigner), } -pub struct LinkTransformation<'a> { - link_text: Option>, +pub struct LinkTransformation<'md> { + link_text: Option>, } /// A temporary holder to perform link transformations. @@ -87,10 +87,10 @@ pub struct LinkTransformation<'a> { /// ``` /// /// This lets us use the `transform_variant()`'s Copy-ability to release the borrow. -impl<'a> LinkTransformation<'a> { - pub fn new(transform: LinkTransform, inline_writer: &mut MdInlinesWriter<'a>, item: L) -> Self +impl<'md> LinkTransformation<'md> { + pub fn new(transform: LinkTransform, inline_writer: &mut MdInlinesWriter<'md>, item: L) -> Self where - L: LinkLike<'a> + Copy, + L: LinkLike<'md> + Copy, { let link_text = match transform { LinkTransform::Keep | LinkTransform::Inline => None, @@ -111,10 +111,10 @@ impl<'a> LinkTransformation<'a> { Self { link_text } } - // We could in principle return a Cow<'a, LinkReference>, and save some clones in the assigner. + // We could in principle return a Cow<'md, LinkReference>, and save some clones in the assigner. // To do that, fmt_md_inlines.rs would need to adjust to hold Cows instead of LinkLabels directly. For now, not // a high priority. - pub fn apply(self, transformer: &mut LinkTransformer, link: &'a LinkReference) -> LinkReference { + pub fn apply(self, transformer: &mut LinkTransformer, link: &'md LinkReference) -> LinkReference { match &mut transformer.delegate { LinkTransformState::Keep => Cow::Borrowed(link), LinkTransformState::Inline => Cow::Owned(LinkReference::Inline), @@ -152,7 +152,7 @@ impl ReferenceAssigner { } } - fn assign<'a>(&mut self, state: LinkTransformation<'a>, link: &'a LinkReference) -> Cow<'a, LinkReference> { + fn assign<'md>(&mut self, state: LinkTransformation<'md>, link: &'md LinkReference) -> Cow<'md, LinkReference> { match &link { LinkReference::Inline => self.assign_new(), LinkReference::Full(prev) => self.assign_if_numeric(prev).unwrap_or_else(|| Cow::Borrowed(link)), @@ -166,7 +166,7 @@ impl ReferenceAssigner { } } - fn assign_if_numeric<'a>(&mut self, prev: &str) -> Option> { + fn assign_if_numeric<'md>(&mut self, prev: &str) -> Option> { if prev.chars().all(|ch| ch.is_numeric()) { match self.reorderings.entry(String::from(prev)) { Entry::Occupied(map_to) => Some(Cow::Owned(LinkReference::Full(map_to.get().to_string()))), @@ -180,7 +180,7 @@ impl ReferenceAssigner { } } - fn assign_new<'a>(&mut self) -> Cow<'a, LinkReference> { + fn assign_new<'md>(&mut self) -> Cow<'md, LinkReference> { let idx_str = self.next_index.to_string(); self.next_index += 1; Cow::Owned(LinkReference::Full(idx_str)) @@ -189,7 +189,7 @@ impl ReferenceAssigner { /// Turns the inlines into a String. Unlike [crate::fmt_str::inlines_to_plain_string], this respects formatting spans /// like emphasis, strong, etc. -fn inlines_to_string<'a>(inline_writer: &mut MdInlinesWriter<'a>, inlines: &'a Vec) -> String { +fn inlines_to_string<'md>(inline_writer: &mut MdInlinesWriter<'md>, inlines: &'md Vec) -> String { let mut string_writer = Output::new(String::with_capacity(32)); // guess at capacity inline_writer.write_line(&mut string_writer, inlines); string_writer @@ -464,10 +464,10 @@ mod tests { ); } - fn transform<'a>( + fn transform<'md>( mut transformer: &mut LinkTransformer, - mut iw: &mut MdInlinesWriter<'a>, - link: &'a Link, + mut iw: &mut MdInlinesWriter<'md>, + link: &'md Link, ) -> LinkReference { let actual = LinkTransformation::new(transformer.transform_variant(), &mut iw, link) .apply(&mut transformer, &link.link_definition.reference); diff --git a/src/select/api.rs b/src/select/api.rs index 05b0dfd..b37ae20 100644 --- a/src/select/api.rs +++ b/src/select/api.rs @@ -18,9 +18,8 @@ pub type ParseResult = Result; pub const SELECTOR_SEPARATOR: char = '|'; -pub trait Selector<'a, I: Into>> { - // TODO I should really rename all these 'a to 'md - fn try_select(&self, item: I) -> Option>; +pub trait Selector<'md, I: Into>> { + fn try_select(&self, item: I) -> Option>; } #[derive(Debug, Clone, PartialEq)] @@ -69,7 +68,7 @@ macro_rules! selectors { } impl MdqRefSelector { - fn try_select_node<'a>(&self, node: MdElemRef<'a>) -> Option> { + fn try_select_node<'md>(&self, node: MdElemRef<'md>) -> Option> { match (self, node) { $( (Self::$name(selector), MdElemRef::$name(elem)) => selector.try_select(elem), @@ -190,7 +189,7 @@ impl MdqRefSelector { Ok(selectors) } - pub fn find_nodes<'a>(&self, nodes: Vec>) -> Vec> { + pub fn find_nodes<'md>(&self, nodes: Vec>) -> Vec> { let mut result = Vec::with_capacity(8); // arbitrary guess for node in nodes { self.build_output(&mut result, node); @@ -198,7 +197,7 @@ impl MdqRefSelector { result } - fn build_output<'a>(&self, out: &mut Vec>, node: MdElemRef<'a>) { + fn build_output<'md>(&self, out: &mut Vec>, node: MdElemRef<'md>) { // try_select_node is defined in macro_helpers::selectors! match self.try_select_node(node.clone()) { // TODO can we remove this? I don't think so, but let's follow up diff --git a/src/select/match_selector.rs b/src/select/match_selector.rs index ad66610..d2b8607 100644 --- a/src/select/match_selector.rs +++ b/src/select/match_selector.rs @@ -7,12 +7,12 @@ pub trait MatchSelector { fn matches(&self, item: I) -> bool; } -impl<'a, I, M> Selector<'a, I> for M +impl<'md, I, M> Selector<'md, I> for M where - I: Copy + Into>, + I: Copy + Into>, M: MatchSelector, { - fn try_select(&self, item: I) -> Option> { + fn try_select(&self, item: I) -> Option> { if self.matches(item) { Some(item.into()) } else { diff --git a/src/select/sel_table.rs b/src/select/sel_table.rs index ab4ada6..6a0c626 100644 --- a/src/select/sel_table.rs +++ b/src/select/sel_table.rs @@ -33,8 +33,8 @@ impl TableSliceSelector { } } -impl<'a> Selector<'a, TableSlice<'a>> for TableSliceSelector { - fn try_select(&self, slice: TableSlice<'a>) -> Option> { +impl<'md> Selector<'md, TableSlice<'md>> for TableSliceSelector { + fn try_select(&self, slice: TableSlice<'md>) -> Option> { let mut slice = slice.clone(); // TODO is there any way to avoid this? There may not be. slice.normalize(); diff --git a/src/tree.rs b/src/tree.rs index 9aa1a97..0366d5a 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -604,16 +604,16 @@ impl MdElem { } } -struct NodeToMdqIter<'a, I> +struct NodeToMdqIter<'md, I> where I: Iterator, { children: I, pending: IntoIter, - lookups: &'a Lookups, + lookups: &'md Lookups, } -impl<'a, I> Iterator for NodeToMdqIter<'a, I> +impl<'md, I> Iterator for NodeToMdqIter<'md, I> where I: Iterator, { @@ -1985,7 +1985,7 @@ mod tests { // See [ #[test] fn link_has_conflicting_definition() { - fn get<'a>(validate_no_conflicting_links: bool) -> Result { + fn get(validate_no_conflicting_links: bool) -> Result { lookups_for( &ParseOptions::gfm(), ReadOptions { diff --git a/src/tree_ref.rs b/src/tree_ref.rs index e413900..e3a1425 100644 --- a/src/tree_ref.rs +++ b/src/tree_ref.rs @@ -7,44 +7,44 @@ use markdown::mdast; /// An MdqNodeRef is a slice into an MdqNode tree, where each element can be outputted, and certain elements can be /// selected. #[derive(Debug, Clone, PartialEq)] -pub enum MdElemRef<'a> { +pub enum MdElemRef<'md> { // Multiple elements that form a single area - Doc(&'a Vec), + Doc(&'md Vec), // main elements - BlockQuote(&'a BlockQuote), - CodeBlock(&'a CodeBlock), - Inline(&'a Inline), - List(&'a List), - Paragraph(&'a Paragraph), - Section(&'a Section), - Table(&'a Table), - Html(HtmlRef<'a>), + BlockQuote(&'md BlockQuote), + CodeBlock(&'md CodeBlock), + Inline(&'md Inline), + List(&'md List), + Paragraph(&'md Paragraph), + Section(&'md Section), + Table(&'md Table), + Html(HtmlRef<'md>), ThematicBreak, // sub-elements - ListItem(ListItemRef<'a>), - Link(&'a Link), - Image(&'a Image), - TableSlice(TableSlice<'a>), + ListItem(ListItemRef<'md>), + Link(&'md Link), + Image(&'md Image), + TableSlice(TableSlice<'md>), } #[derive(Debug, Clone, Copy, PartialEq)] -pub struct ListItemRef<'a>(pub Option, pub &'a ListItem); +pub struct ListItemRef<'md>(pub Option, pub &'md ListItem); #[derive(Debug, Clone, Copy, PartialEq)] -pub struct HtmlRef<'a>(pub &'a String); +pub struct HtmlRef<'md>(pub &'md String); #[derive(Debug, Clone, PartialEq)] -pub struct TableSlice<'a> { +pub struct TableSlice<'md> { alignments: Vec, - rows: Vec>, + rows: Vec>, } -pub type TableRowSlice<'a> = Vec>; +pub type TableRowSlice<'md> = Vec>; -impl<'a> From<&'a Table> for TableSlice<'a> { - fn from(table: &'a Table) -> Self { +impl<'md> From<&'md Table> for TableSlice<'md> { + fn from(table: &'md Table) -> Self { let alignments = table.alignments.clone(); let mut rows = Vec::with_capacity(table.rows.len()); for table_row in &table.rows { @@ -55,12 +55,12 @@ impl<'a> From<&'a Table> for TableSlice<'a> { } } -impl<'a> TableSlice<'a> { +impl<'md> TableSlice<'md> { pub fn alignments(&self) -> &Vec { &self.alignments } - pub fn rows(&self) -> impl Iterator> { + pub fn rows(&self) -> impl Iterator> { self.rows.iter() } @@ -150,8 +150,8 @@ impl<'a> TableSlice<'a> { } } -impl<'a> From<&'a MdElem> for MdElemRef<'a> { - fn from(value: &'a MdElem) -> Self { +impl<'md> From<&'md MdElem> for MdElemRef<'md> { + fn from(value: &'md MdElem) -> Self { match value { MdElem::ThematicBreak => Self::ThematicBreak, MdElem::Paragraph(p) => Self::Paragraph(p), @@ -166,63 +166,62 @@ impl<'a> From<&'a MdElem> for MdElemRef<'a> { } } -// TODO do I need all these explicit 'a lifetimes? I think I can do '_ -impl<'a> From<&'a BlockQuote> for MdElemRef<'a> { - fn from(value: &'a BlockQuote) -> Self { +impl<'md> From<&'md BlockQuote> for MdElemRef<'md> { + fn from(value: &'md BlockQuote) -> Self { MdElemRef::BlockQuote(value) } } -impl<'a> From<&'a CodeBlock> for MdElemRef<'a> { - fn from(value: &'a CodeBlock) -> Self { +impl<'md> From<&'md CodeBlock> for MdElemRef<'md> { + fn from(value: &'md CodeBlock) -> Self { MdElemRef::CodeBlock(value) } } -impl<'a> From> for MdElemRef<'a> { - fn from(value: ListItemRef<'a>) -> Self { +impl<'md> From> for MdElemRef<'md> { + fn from(value: ListItemRef<'md>) -> Self { MdElemRef::ListItem(value) } } -impl<'a> From> for MdElemRef<'a> { - fn from(value: HtmlRef<'a>) -> Self { +impl<'md> From> for MdElemRef<'md> { + fn from(value: HtmlRef<'md>) -> Self { Self::Html(HtmlRef(value.0)) } } -impl<'a> From<&'a Image> for MdElemRef<'a> { - fn from(value: &'a Image) -> Self { +impl<'md> From<&'md Image> for MdElemRef<'md> { + fn from(value: &'md Image) -> Self { MdElemRef::Image(value) } } -impl<'a> From<&'a Link> for MdElemRef<'a> { - fn from(value: &'a Link) -> Self { +impl<'md> From<&'md Link> for MdElemRef<'md> { + fn from(value: &'md Link) -> Self { MdElemRef::Link(value) } } -impl<'a> From<&'a Paragraph> for MdElemRef<'a> { - fn from(value: &'a Paragraph) -> Self { +impl<'md> From<&'md Paragraph> for MdElemRef<'md> { + fn from(value: &'md Paragraph) -> Self { MdElemRef::Paragraph(value) } } -impl<'a> From<&'a Section> for MdElemRef<'a> { - fn from(value: &'a Section) -> Self { +impl<'md> From<&'md Section> for MdElemRef<'md> { + fn from(value: &'md Section) -> Self { MdElemRef::Section(value) } } -impl<'a> From<&'a Table> for MdElemRef<'a> { - fn from(value: &'a Table) -> Self { +impl<'md> From<&'md Table> for MdElemRef<'md> { + fn from(value: &'md Table) -> Self { MdElemRef::Table(value) } } -impl<'a> From> for MdElemRef<'a> { - fn from(value: TableSlice<'a>) -> Self { +impl<'md> From> for MdElemRef<'md> { + fn from(value: TableSlice<'md>) -> Self { MdElemRef::TableSlice(value) } } diff --git a/src/tree_ref_serde.rs b/src/tree_ref_serde.rs index 05b41c6..ae1474b 100644 --- a/src/tree_ref_serde.rs +++ b/src/tree_ref_serde.rs @@ -9,48 +9,48 @@ use std::borrow::{Borrow, Cow}; use std::collections::HashMap; #[derive(Serialize)] -pub struct SerdeDoc<'a> { - items: Vec>, +pub struct SerdeDoc<'md> { + items: Vec>, #[serde(skip_serializing_if = "HashMap::is_empty")] - links: HashMap, UrlAndTitle<'a>>, + links: HashMap, UrlAndTitle<'md>>, #[serde(skip_serializing_if = "HashMap::is_empty")] - footnotes: HashMap<&'a String, Vec>>, + footnotes: HashMap<&'md String, Vec>>, } #[derive(Serialize)] #[serde(rename_all = "snake_case")] -pub enum SerdeElem<'a> { - Document(Vec>), - BlockQuote(Vec>), +pub enum SerdeElem<'md> { + Document(Vec>), + BlockQuote(Vec>), CodeBlock { - code: &'a String, + code: &'md String, #[serde(rename = "type")] code_type: CodeBlockType, #[serde(skip_serializing_if = "Option::is_none")] - language: Option<&'a String>, + language: Option<&'md String>, #[serde(skip_serializing_if = "Option::is_none")] - metadata: Option<&'a String>, + metadata: Option<&'md String>, }, Paragraph(String), Link { display: String, #[serde(flatten)] - link: LinkSerde<'a>, + link: LinkSerde<'md>, }, Image { - alt: &'a String, + alt: &'md String, #[serde(flatten)] - link: LinkSerde<'a>, + link: LinkSerde<'md>, }, - List(Vec>), - ListItem(LiSerde<'a>), + List(Vec>), + ListItem(LiSerde<'md>), Section { depth: u8, title: String, - body: Vec>, + body: Vec>, }, #[serde(serialize_with = "serialize_thematic_break")] ThematicBreak, @@ -59,7 +59,7 @@ pub enum SerdeElem<'a> { rows: Vec>, }, Html { - value: &'a String, + value: &'md String, }, } @@ -68,21 +68,21 @@ fn serialize_thematic_break(ser: S) -> Result { } #[derive(Serialize)] -pub struct LinkSerde<'a> { - url: &'a String, +pub struct LinkSerde<'md> { + url: &'md String, #[serde(skip_serializing_if = "Option::is_none")] - title: &'a Option, + title: &'md Option, #[serde(skip_serializing_if = "Option::is_none")] - reference: Option>, + reference: Option>, #[serde(skip_serializing_if = "Option::is_none")] reference_style: Option, } -impl<'a> From<&'a LinkDefinition> for LinkSerde<'a> { - fn from(value: &'a LinkDefinition) -> Self { +impl<'md> From<&'md LinkDefinition> for LinkSerde<'md> { + fn from(value: &'md LinkDefinition) -> Self { let LinkDefinition { url, title, reference } = value; let (reference, reference_style) = match reference { @@ -121,14 +121,14 @@ impl From<&AlignKind> for AlignSerde { } #[derive(Serialize)] -pub struct LiSerde<'a> { - item: Vec>, +pub struct LiSerde<'md> { + item: Vec>, #[serde(skip_serializing_if = "Option::is_none")] index: Option, #[serde(skip_serializing_if = "Option::is_none")] - checked: &'a Option, + checked: &'md Option, } #[derive(Serialize)] @@ -147,8 +147,8 @@ pub enum CodeBlockType { Yaml, } -impl<'a> SerdeDoc<'a> { - pub fn new(elems: &[MdElemRef<'a>], opts: MdInlinesWriterOptions) -> Self { +impl<'md> SerdeDoc<'md> { + pub fn new(elems: &[MdElemRef<'md>], opts: MdInlinesWriterOptions) -> Self { let mut inlines_writer = MdInlinesWriter::new(opts); const DEFAULT_CAPACITY: usize = 16; // we could compute these, but it's not really worth it let mut result = Self { @@ -177,8 +177,8 @@ impl<'a> SerdeDoc<'a> { } } -impl<'a> SerdeElem<'a> { - fn build_multi(elems: &'a [M], inlines_writer: &mut MdInlinesWriter<'a>) -> Vec +impl<'md> SerdeElem<'md> { + fn build_multi(elems: &'md [M], inlines_writer: &mut MdInlinesWriter<'md>) -> Vec where M: Borrow, { @@ -189,7 +189,7 @@ impl<'a> SerdeElem<'a> { result } - fn build(elem: MdElemRef<'a>, inlines_writer: &mut MdInlinesWriter<'a>) -> Self { + fn build(elem: MdElemRef<'md>, inlines_writer: &mut MdInlinesWriter<'md>) -> Self { match elem { MdElemRef::Doc(doc) => Self::Document(Self::build_multi(doc, inlines_writer)), MdElemRef::BlockQuote(bq) => Self::BlockQuote(Self::build_multi(&bq.body, inlines_writer)), @@ -291,9 +291,9 @@ impl<'a> SerdeElem<'a> { } } -fn inlines_to_string<'a, I>(inlines: I, writer: &mut MdInlinesWriter<'a>) -> String +fn inlines_to_string<'md, I>(inlines: I, writer: &mut MdInlinesWriter<'md>) -> String where - I: IntoIterator, + I: IntoIterator, { let mut output = Output::new(String::with_capacity(16)); // guess writer.write_line(&mut output, inlines);