Skip to content

Commit

Permalink
rename 'a -> 'md for markdown objects
Browse files Browse the repository at this point in the history
  • Loading branch information
yshavit authored Aug 9, 2024
1 parent 2ff2d7d commit 7ff2a84
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 152 deletions.
34 changes: 17 additions & 17 deletions src/fmt_md.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ impl Default for ReferencePlacement {
}
}

pub fn write_md<'a, I, W>(options: &'a MdOptions, out: &mut Output<W>, nodes: I)
pub fn write_md<'md, I, W>(options: &'md MdOptions, out: &mut Output<W>, nodes: I)
where
I: Iterator<Item = MdElemRef<'a>>,
I: Iterator<Item = MdElemRef<'md>>,
W: SimpleWrite,
{
let mut writer_state = MdWriterState {
Expand All @@ -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<I, W>(&mut self, out: &mut Output<W>, nodes: I, add_break: bool) -> usize
where
I: Iterator<Item = MdElemRef<'a>>,
I: Iterator<Item = MdElemRef<'md>>,
W: SimpleWrite,
{
let mut count = 0;
Expand All @@ -108,7 +108,7 @@ impl<'s, 'a> MdWriterState<'s, 'a> {
count
}

pub fn write_one_md<W>(&mut self, out: &mut Output<W>, node_ref: MdElemRef<'a>)
pub fn write_one_md<W>(&mut self, out: &mut Output<W>, node_ref: MdElemRef<'md>)
where
W: SimpleWrite,
{
Expand Down Expand Up @@ -169,19 +169,19 @@ impl<'s, 'a> MdWriterState<'s, 'a> {
}
}

fn write_paragraph<W: SimpleWrite>(&mut self, out: &mut Output<W>, paragraph: &'a Paragraph) {
fn write_paragraph<W: SimpleWrite>(&mut self, out: &mut Output<W>, paragraph: &'md Paragraph) {
out.with_block(Block::Plain, |out| {
self.inlines_writer.write_line(out, &paragraph.body);
});
}

fn write_block_quote<W: SimpleWrite>(&mut self, out: &mut Output<W>, block: &'a BlockQuote) {
fn write_block_quote<W: SimpleWrite>(&mut self, out: &mut Output<W>, block: &'md BlockQuote) {
out.with_block(Block::Quote, |out| {
self.write_md(out, Self::doc_iter(&block.body), false);
});
}

fn write_list<W: SimpleWrite>(&mut self, out: &mut Output<W>, list: &'a List) {
fn write_list<W: SimpleWrite>(&mut self, out: &mut Output<W>, 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. [ ] "
Expand All @@ -194,7 +194,7 @@ impl<'s, 'a> MdWriterState<'s, 'a> {
});
}

fn write_table<W: SimpleWrite>(&mut self, out: &mut Output<W>, table: TableSlice<'a>) {
fn write_table<W: SimpleWrite>(&mut self, out: &mut Output<W>, table: TableSlice<'md>) {
let alignments = table.alignments();
let rows = table.rows();

Expand Down Expand Up @@ -350,7 +350,7 @@ impl<'s, 'a> MdWriterState<'s, 'a> {
});
}

fn write_list_item<W: SimpleWrite>(&mut self, out: &mut Output<W>, index: &Option<u32>, item: &'a ListItem) {
fn write_list_item<W: SimpleWrite>(&mut self, out: &mut Output<W>, index: &Option<u32>, item: &'md ListItem) {
let mut counting_writer = CountingWriter::wrap(out);
match index {
None => std::fmt::Write::write_str(&mut counting_writer, "- ").unwrap(),
Expand Down Expand Up @@ -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<MdElem>) -> impl Iterator<Item = MdElemRef<'a>> {
fn doc_iter(body: &'md Vec<MdElem>) -> impl Iterator<Item = MdElemRef<'md>> {
[MdElemRef::Doc(body)].into_iter()
}
}
Expand Down Expand Up @@ -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<u32>, checked: Option<bool>, item: Vec<MdElem>, expected: &str) {
fn create_li_singleton(idx: Option<u32>, checked: Option<bool>, item: Vec<MdElem>, expected: &str) {
let li = ListItem { checked, item };
check_render_refs(vec![MdElemRef::ListItem(ListItemRef(idx, &li))], expected)
}
Expand Down Expand Up @@ -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<MdElemRef<'a>>, expect: &str) {
fn check_render_refs_with(options: &MdOptions, nodes: Vec<MdElemRef>, expect: &str) {
nodes.iter().for_each(|n| VARIANTS_CHECKER.see(n));

let mut out = Output::new(String::default());
Expand Down
46 changes: 23 additions & 23 deletions src/fmt_md_inlines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ pub struct MdInlinesWriterOptions {
pub link_format: LinkTransform,
}

pub struct MdInlinesWriter<'a> {
seen_links: HashSet<LinkLabel<'a>>,
seen_footnotes: HashSet<&'a String>,
pending_references: PendingReferences<'a>,
pub struct MdInlinesWriter<'md> {
seen_links: HashSet<LinkLabel<'md>>,
seen_footnotes: HashSet<&'md String>,
pending_references: PendingReferences<'md>,
link_transformer: LinkTransformer,
}

struct PendingReferences<'a> {
pub links: HashMap<LinkLabel<'a>, UrlAndTitle<'a>>,
pub footnotes: HashMap<&'a String, &'a Vec<MdElem>>,
struct PendingReferences<'md> {
pub links: HashMap<LinkLabel<'md>, UrlAndTitle<'md>>,
pub footnotes: HashMap<&'md String, &'md Vec<MdElem>>,
}

impl<'a> PendingReferences<'a> {
impl<'md> PendingReferences<'md> {
fn with_capacity(capacity: usize) -> Self {
Self {
links: HashMap::with_capacity(capacity),
Expand All @@ -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<String>,
pub title: &'md Option<String>,
}

#[derive(Debug, Copy, Clone)]
Expand All @@ -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)),
Expand All @@ -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 {
Expand All @@ -94,25 +94,25 @@ 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<MdElem>)> {
pub fn drain_pending_footnotes(&mut self) -> Vec<(&'md String, &'md Vec<MdElem>)> {
self.pending_references.footnotes.drain().collect()
}

pub fn write_line<I, W>(&mut self, out: &mut Output<W>, elems: I)
where
I: IntoIterator<Item = &'a Inline>,
I: IntoIterator<Item = &'md Inline>,
W: SimpleWrite,
{
for elem in elems {
self.write_inline_element(out, elem);
}
}

pub fn write_inline_element<W>(&mut self, out: &mut Output<W>, elem: &'a Inline)
pub fn write_inline_element<W>(&mut self, out: &mut Output<W>, elem: &'md Inline)
where
W: SimpleWrite,
{
Expand Down Expand Up @@ -165,7 +165,7 @@ impl<'a> MdInlinesWriter<'a> {
pub fn write_linklike<W, L>(&mut self, out: &mut Output<W>, 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) {
Expand Down
36 changes: 18 additions & 18 deletions src/link_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ pub struct LinkTransformer {
}

#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum LinkLabel<'a> {
Text(Cow<'a, str>),
Inline(&'a Vec<Inline>),
pub enum LinkLabel<'md> {
Text(Cow<'md, str>),
Inline(&'md Vec<Inline>),
}

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 {
Expand Down Expand Up @@ -64,8 +64,8 @@ enum LinkTransformState {
Reference(ReferenceAssigner),
}

pub struct LinkTransformation<'a> {
link_text: Option<Cow<'a, str>>,
pub struct LinkTransformation<'md> {
link_text: Option<Cow<'md, str>>,
}

/// A temporary holder to perform link transformations.
Expand All @@ -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<L>(transform: LinkTransform, inline_writer: &mut MdInlinesWriter<'a>, item: L) -> Self
impl<'md> LinkTransformation<'md> {
pub fn new<L>(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,
Expand All @@ -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),
Expand Down Expand Up @@ -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)),
Expand All @@ -166,7 +166,7 @@ impl ReferenceAssigner {
}
}

fn assign_if_numeric<'a>(&mut self, prev: &str) -> Option<Cow<'a, LinkReference>> {
fn assign_if_numeric<'md>(&mut self, prev: &str) -> Option<Cow<'md, LinkReference>> {
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()))),
Expand All @@ -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))
Expand All @@ -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<Inline>) -> String {
fn inlines_to_string<'md>(inline_writer: &mut MdInlinesWriter<'md>, inlines: &'md Vec<Inline>) -> String {
let mut string_writer = Output::new(String::with_capacity(32)); // guess at capacity
inline_writer.write_line(&mut string_writer, inlines);
string_writer
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 5 additions & 6 deletions src/select/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ pub type ParseResult<T> = Result<T, ParseErrorReason>;

pub const SELECTOR_SEPARATOR: char = '|';

pub trait Selector<'a, I: Into<MdElemRef<'a>>> {
// TODO I should really rename all these 'a to 'md
fn try_select(&self, item: I) -> Option<MdElemRef<'a>>;
pub trait Selector<'md, I: Into<MdElemRef<'md>>> {
fn try_select(&self, item: I) -> Option<MdElemRef<'md>>;
}

#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -69,7 +68,7 @@ macro_rules! selectors {
}

impl MdqRefSelector {
fn try_select_node<'a>(&self, node: MdElemRef<'a>) -> Option<MdElemRef<'a>> {
fn try_select_node<'md>(&self, node: MdElemRef<'md>) -> Option<MdElemRef<'md>> {
match (self, node) {
$(
(Self::$name(selector), MdElemRef::$name(elem)) => selector.try_select(elem),
Expand Down Expand Up @@ -190,15 +189,15 @@ impl MdqRefSelector {
Ok(selectors)
}

pub fn find_nodes<'a>(&self, nodes: Vec<MdElemRef<'a>>) -> Vec<MdElemRef<'a>> {
pub fn find_nodes<'md>(&self, nodes: Vec<MdElemRef<'md>>) -> Vec<MdElemRef<'md>> {
let mut result = Vec::with_capacity(8); // arbitrary guess
for node in nodes {
self.build_output(&mut result, node);
}
result
}

fn build_output<'a>(&self, out: &mut Vec<MdElemRef<'a>>, node: MdElemRef<'a>) {
fn build_output<'md>(&self, out: &mut Vec<MdElemRef<'md>>, 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
Expand Down
6 changes: 3 additions & 3 deletions src/select/match_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ pub trait MatchSelector<I> {
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<MdElemRef<'a>>,
I: Copy + Into<MdElemRef<'md>>,
M: MatchSelector<I>,
{
fn try_select(&self, item: I) -> Option<MdElemRef<'a>> {
fn try_select(&self, item: I) -> Option<MdElemRef<'md>> {
if self.matches(item) {
Some(item.into())
} else {
Expand Down
Loading

0 comments on commit 7ff2a84

Please sign in to comment.