Skip to content

Commit

Permalink
refactor(crate): Introduce Word scruct to handle more info about words
Browse files Browse the repository at this point in the history
  • Loading branch information
alerque committed Oct 27, 2024
1 parent 0c96a60 commit cae7668
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@ use std::{borrow::Cow, fmt, fmt::Display, str::FromStr};

use snafu::prelude::*;

#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Chunk {
pub segments: Vec<Segment>,
}

#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum Segment {
Separator(String),
Word(String),
Word(Word),
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct Chunk {
pub segments: Vec<Segment>,
pub struct Word {
word: String,
}

#[derive(Snafu)]
Expand All @@ -42,7 +48,9 @@ fn split_chunk(s: &str) -> Chunk {
if let Some(m) = capture.name("separator") {
segments.push(Segment::Separator(m.as_str().to_string()));
} else if let Some(m) = capture.name("word") {
segments.push(Segment::Word(m.as_str().to_string()));
segments.push(Segment::Word(Word {
word: m.as_str().to_owned(),
}));
}
}
Chunk { segments }
Expand Down Expand Up @@ -79,21 +87,28 @@ impl FromStr for Chunk {
}
}

impl Display for Chunk {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
for segment in &self.segments {
fmt.write_str(segment.to_string().as_ref())?;
}
Ok(())
}
}

impl Display for Segment {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let _ = match self {
Segment::Separator(string) => fmt.write_str(string),
Segment::Word(string) => fmt.write_str(string),
Segment::Word(word) => fmt.write_str(&word.to_string()),
};
Ok(())
}
}

impl Display for Chunk {
impl Display for Word {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
for segment in &self.segments {
fmt.write_str(segment.to_string().as_ref())?;
}
fmt.write_str(self.word.as_ref());
Ok(())
}
}

0 comments on commit cae7668

Please sign in to comment.