changxi 0.3.0

TUI EPUB Reader
#[derive(Clone, Debug, Default, PartialEq)]
pub struct TextStyle {
    pub bold: bool,
    pub italic: bool,
    pub underline: bool,
    pub strikethrough: bool,
}

#[derive(Clone, Debug)]
pub struct StyledText {
    pub text: String,
    pub style: TextStyle,
}

#[derive(Clone, Debug)]
pub enum ContentElement {
    Text(Vec<StyledText>),
    Image(String),
    BlankLine,
}

#[derive(Clone, Debug)]
pub struct Chapter {
    pub _id: String,
    pub title: String,
    pub elements: Vec<ContentElement>,
}

impl Chapter {
    pub fn new(id: String, title: String, elements: Vec<ContentElement>) -> Self {
        Chapter {
            _id: id,
            title,
            elements,
        }
    }

    pub fn content_as_text(&self) -> String {
        self.elements
            .iter()
            .map(|e| match e {
                ContentElement::Text(spans) => {
                    spans.iter().map(|s| s.text.as_str()).collect::<String>()
                }
                ContentElement::Image(_) => String::new(),
                ContentElement::BlankLine => "\n".to_string(),
            })
            .collect::<Vec<_>>()
            .join("\n\n")
    }
}

pub struct SearchResult {
    pub chapter_index: usize,
    pub chapter_title: String,
    pub context_before: String,
    pub match_text: String,
    pub context_after: String,
    pub position: usize,
}

impl SearchResult {
    pub fn new(
        chapter_index: usize,
        chapter_title: String,
        context_before: String,
        match_text: String,
        context_after: String,
        position: usize,
    ) -> Self {
        SearchResult {
            chapter_index,
            chapter_title,
            context_before,
            match_text,
            context_after,
            position,
        }
    }
}