Skip to content

Commit

Permalink
Delimiter refactoring (#38)
Browse files Browse the repository at this point in the history
* Parents are responsible for delimiters

* Status update

* Remove unused method
  • Loading branch information
Lurk authored Nov 2, 2023
1 parent 764a886 commit accffe2
Show file tree
Hide file tree
Showing 18 changed files with 512 additions and 849 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

## Status

It is ready to poke around. There is no significant API changes expected.
It is not ready to poke around. There is significant API changes expected.

## Why?

Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,14 @@ mod tests {
#[test]
fn test_deserialize() {
let input = "# header";
let expected = Yamd::new_with_nodes(None, vec![Heading::new(true, "header", 1).into()]);
let expected = Yamd::new(None, vec![Heading::new("header", 1).into()]);
let actual = deserialize(input).unwrap();
assert_eq!(expected, actual);
}

#[test]
fn test_serialize() {
let input = Yamd::new_with_nodes(None, vec![Heading::new(true, "header", 1).into()]);
let input = Yamd::new(None, vec![Heading::new("header", 1).into()]);
let expected = "# header";
let actual = serialize(&input);
assert_eq!(expected, actual);
Expand All @@ -236,11 +236,11 @@ mod tests {
#[test]
fn deserialize_text_containing_utf8() {
let input = "## 🤔\n\n[link 😉](url)";
let expected = Yamd::new_with_nodes(
let expected = Yamd::new(
None,
vec![
Heading::new(false, "🤔", 2).into(),
Paragraph::new_with_nodes(true, vec![Anchor::new("link 😉", "url").into()]).into(),
Heading::new("🤔", 2).into(),
Paragraph::new(vec![Anchor::new("link 😉", "url").into()]).into(),
],
);
let actual = deserialize(input).unwrap();
Expand Down
87 changes: 37 additions & 50 deletions src/nodes/accordion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,43 +41,46 @@ impl From<AccordionTab> for AccordionNodes {

#[derive(Debug, PartialEq, Serialize, Clone)]
pub struct Accordion {
#[serde(skip_serializing)]
consumed_all_input: bool,
pub nodes: Vec<AccordionNodes>,
}

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

pub fn new_with_nodes(consumed_all_input: bool, nodes: Vec<AccordionNodes>) -> Self {
Accordion {
consumed_all_input,
nodes,
}
impl Default for Accordion {
fn default() -> Self {
Accordion::new(vec![])
}
}

impl Display for Accordion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"///\n{nodes}\n\\\\\\{end}",
"///\n{nodes}\n\\\\\\",
nodes = self
.nodes
.iter()
.map(|n| n.to_string())
.collect::<Vec<String>>()
.join(""),
end = if self.consumed_all_input { "" } else { "\n\n" }
.join("\n"),
)
}
}

impl Node for Accordion {
fn len(&self) -> usize {
self.nodes.iter().map(|n| n.len()).sum::<usize>() + self.get_outer_token_length()
let delimiter_len = if self.nodes.is_empty() {
0
} else {
self.nodes.len() - 1
};
self.nodes.iter().map(|n| n.len()).sum::<usize>()
+ self.get_outer_token_length()
+ delimiter_len
}
}

Expand All @@ -95,7 +98,7 @@ impl Branch<AccordionNodes> for Accordion {
}

fn get_outer_token_length(&self) -> usize {
8 + if self.consumed_all_input { 0 } else { 2 }
8
}
}

Expand All @@ -107,8 +110,7 @@ impl Deserializer for Accordion {
let mut matcher = Matcher::new(input);

if let Some(accordion) = matcher.get_match("///\n", "\n\\\\\\", false) {
let consumed_all_input = matcher.get_match("\n\n", "", false).is_none();
return Self::parse_branch(accordion.body, Self::new(consumed_all_input));
return Self::parse_branch(accordion.body, "\n", Self::default());
}
None
}
Expand All @@ -126,7 +128,7 @@ mod test {
#[test]
fn test_deserialize_empty() {
let input = "///\n\n\\\\\\";
assert_eq!(Accordion::deserialize(input), Some(Accordion::new(true)));
assert_eq!(Accordion::deserialize(input), Some(Accordion::default()));
}

#[test]
Expand All @@ -143,22 +145,13 @@ mod test {
\\\"#;
assert_eq!(
Accordion::deserialize(input),
Some(Accordion::new_with_nodes(
true,
vec![
AccordionTab::new(false, Some("header")).into(),
AccordionTab::new(true, Some("one more")).into()
]
))
Some(Accordion::new(vec![
AccordionTab::new(Some("header"), vec![]).into(),
AccordionTab::new(Some("one more"), vec![]).into()
]))
);
}

#[test]
fn consumed_all_input() {
let input = "///\n\n\\\\\\\n\n";
assert_eq!(Accordion::deserialize(input), Some(Accordion::new(false)));
}

#[test]
fn test_len() {
let input = r#"///
Expand All @@ -180,28 +173,22 @@ mod test {
Accordion::deserialize(
"///\n//\n/ header\n///\n//\n/ hi from nested\ncontent\n\\\\\n\\\\\\\n\\\\\n\\\\\\"
),
Some(Accordion::new_with_nodes(
true,
vec![AccordionTab::new_with_nodes(
true,
Some("header"),
vec![Accordion::new_with_nodes(
true,
vec![AccordionTab::new_with_nodes(
true,
Some("hi from nested"),
vec![Paragraph::new_with_nodes(
true,
vec![Text::new("content").into()]
)
.into()]
)
.into()]
)
.into()]
Some(Accordion::new(vec![AccordionTab::new(
Some("header"),
vec![Accordion::new(vec![AccordionTab::new(
Some("hi from nested"),
vec![Paragraph::new(vec![Text::new("content").into()]).into()]
)
.into()])
.into()]
))
)
.into()]))
);
}

#[test]
fn empty_accordion() {
let accordion = Accordion::default();
assert_eq!(accordion.len(), 8);
}
}
Loading

0 comments on commit accffe2

Please sign in to comment.