Skip to content

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
  • Loading branch information
farooqkz committed May 9, 2024
1 parent eda93c2 commit 9076664
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 64 deletions.
5 changes: 5 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ pub enum Element<'a> {
Text(&'a str),
/// #hashtag
Tag(&'a str),
/// [label](#tag)
LabelledTag {
label: Box<Element<'a>>,
tag: &'a str
},
/// Represents a linebreak - \n
Linebreak,
Link {
Expand Down
18 changes: 17 additions & 1 deletion src/parser/parse_from_text/text_elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use nom::{
streaming::take_till1,
},
character::complete::char,
combinator::{peek, recognize, verify},
combinator::{peek, recognize, verify, consumed},
sequence::tuple,
AsChar, IResult, Offset, Slice,
};
Expand Down Expand Up @@ -253,6 +253,20 @@ fn bot_command_suggestion(input: &str) -> IResult<&str, Element, CustomError<&st
}
}

fn labelled_tag(input: &str) -> IResult<&str, Element, CustomError<&str>> {
let (input, label) = delimited(char('['), is_not("["), char(']'))(input)?;

Check failure on line 257 in src/parser/parse_from_text/text_elements.rs

View workflow job for this annotation

GitHub Actions / clippy

cannot find function `is_not` in this scope

error[E0425]: cannot find function `is_not` in this scope --> src/parser/parse_from_text/text_elements.rs:257:47 | 257 | let (input, label) = delimited(char('['), is_not("["), char(']'))(input)?; | ^^^^^^ not found in this scope | help: consider importing one of these items | 1 + use nom::bytes::complete::is_not; | 1 + use nom::bytes::streaming::is_not; |

Check failure on line 257 in src/parser/parse_from_text/text_elements.rs

View workflow job for this annotation

GitHub Actions / clippy

cannot find function `delimited` in this scope

error[E0425]: cannot find function `delimited` in this scope --> src/parser/parse_from_text/text_elements.rs:257:26 | 257 | let (input, label) = delimited(char('['), is_not("["), char(']'))(input)?; | ^^^^^^^^^ not found in this scope | help: consider importing this function | 1 + use nom::sequence::delimited; |

Check failure on line 257 in src/parser/parse_from_text/text_elements.rs

View workflow job for this annotation

GitHub Actions / Build and test (ubuntu-latest, 1.77.2)

cannot find function `delimited` in this scope

Check failure on line 257 in src/parser/parse_from_text/text_elements.rs

View workflow job for this annotation

GitHub Actions / Build and test (ubuntu-latest, 1.77.2)

cannot find function `is_not` in this scope
let (_, label) = parse_text_element(label, None)?;
let (input, tag) = delimited(char('('), is_not("("), char(')'))(input)?;

Check failure on line 259 in src/parser/parse_from_text/text_elements.rs

View workflow job for this annotation

GitHub Actions / clippy

cannot find function `is_not` in this scope

error[E0425]: cannot find function `is_not` in this scope --> src/parser/parse_from_text/text_elements.rs:259:45 | 259 | let (input, tag) = delimited(char('('), is_not("("), char(')'))(input)?; | ^^^^^^ not found in this scope | help: consider importing one of these items | 1 + use nom::bytes::complete::is_not; | 1 + use nom::bytes::streaming::is_not; |

Check failure on line 259 in src/parser/parse_from_text/text_elements.rs

View workflow job for this annotation

GitHub Actions / clippy

cannot find function `delimited` in this scope

error[E0425]: cannot find function `delimited` in this scope --> src/parser/parse_from_text/text_elements.rs:259:24 | 259 | let (input, tag) = delimited(char('('), is_not("("), char(')'))(input)?; | ^^^^^^^^^ not found in this scope | help: consider importing this function | 1 + use nom::sequence::delimited; |

Check failure on line 259 in src/parser/parse_from_text/text_elements.rs

View workflow job for this annotation

GitHub Actions / Build and test (ubuntu-latest, 1.77.2)

cannot find function `delimited` in this scope

Check failure on line 259 in src/parser/parse_from_text/text_elements.rs

View workflow job for this annotation

GitHub Actions / Build and test (ubuntu-latest, 1.77.2)

cannot find function `is_not` in this scope
let (_, tag) = consumed(hashtag)(tag)?;
Ok((
input,
Element::LabelledTag {
label: Box::new(label),
tag

Check failure on line 265 in src/parser/parse_from_text/text_elements.rs

View workflow job for this annotation

GitHub Actions / clippy

mismatched types

error[E0308]: mismatched types --> src/parser/parse_from_text/text_elements.rs:265:17 | 265 | tag | ^^^ expected `&str`, found `(&str, Element<'_>)` | = note: expected reference `&str` found tuple `(&str, parser::Element<'_>)`

Check failure on line 265 in src/parser/parse_from_text/text_elements.rs

View workflow job for this annotation

GitHub Actions / Build and test (ubuntu-latest, 1.77.2)

mismatched types
}
))
}

pub(crate) fn parse_text_element(
input: &str,
prev_char: Option<char>,
Expand All @@ -266,6 +280,8 @@ pub(crate) fn parse_text_element(

if let Ok((i, elm)) = hashtag(input) {
Ok((i, elm))
} else if let Ok((i, elm)) = labelled_tag(input) {
Ok((i, elm))
} else if let Ok((i, elm)) = {
if prev_char == Some(' ') || prev_char.is_none() {
bot_command_suggestion(input)
Expand Down
63 changes: 0 additions & 63 deletions tests/text_to_ast/mod.rs.orig

This file was deleted.

28 changes: 28 additions & 0 deletions tests/text_to_ast/text_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,34 @@ fn persian_hashtag_with_underline() {
);
}

#[test]
fn labelled_hashtag() {
let input = "[Hello](#hello) #world";

assert_eq!(
parse_only_text(input),
vec![
LabelledTag {
label: vec![Text("Hello")],
tag: "#hello",
},
Tag("#world"),
]
);

let input_bold = "[**Hello**](#hi) ";
assert_eq!(
parse_only_text(input_bold),
vec![
LaballedTag {
label: vec![Bold(vec![Text(Hello)])],
tag: "#hi",
},
Text(" ")
]
);
}

#[test]
fn email_address_standalone() {
let test_cases = vec![
Expand Down

0 comments on commit 9076664

Please sign in to comment.