mod constants;
mod elements;
mod error;
mod parser;
mod regex;
mod search;
mod summary;
mod tree;
pub use elements::{Element, ListType};
pub use error::MeyerholdError;
pub use search::SearchResult;
pub use summary::{ContentItem, SnapshotSummary, DEFAULT_TEXT_CHAR_LIMIT};
use serde_json::Value;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Section {
Tabs,
Errors,
Tree,
Page,
}
#[derive(Debug, Clone)]
pub struct Meyerhold {
snapshot_text: String,
}
impl Meyerhold {
pub fn from_str(json_str: &str) -> Result<Self, MeyerholdError> {
let json = parser::parse_json(json_str)?;
Self::from_json(&json)
}
pub fn from_json(json: &Value) -> Result<Self, MeyerholdError> {
let snapshot_text = parser::extract_snapshot_text(json)?;
Ok(Self { snapshot_text })
}
pub fn from_text(text: impl Into<String>) -> Self {
Self {
snapshot_text: text.into(),
}
}
pub fn content(&self) -> &str {
&self.snapshot_text
}
pub fn summary(&self) -> SnapshotSummary {
summary::parse_summary(&self.snapshot_text)
}
pub fn elements(&self, list_type: ListType) -> Vec<Element> {
elements::extract_elements(&self.snapshot_text, list_type)
}
pub fn search(&self, pattern: &str, use_regex: bool) -> Result<Vec<SearchResult>, MeyerholdError> {
search::search(&self.snapshot_text, pattern, use_regex)
}
pub fn section(&self, section: Section) -> Option<String> {
let result = match section {
Section::Tabs => {
summary::extract_section(&self.snapshot_text, constants::SECTION_TABS, constants::SECTION_END)
}
Section::Errors => {
summary::extract_section(&self.snapshot_text, constants::SECTION_ERRORS, constants::SECTION_END)
}
Section::Tree => {
summary::extract_section(&self.snapshot_text, constants::SECTION_TREE_START, constants::SECTION_TREE_END)
}
Section::Page => summary::extract_page_state(&self.snapshot_text),
};
if result.is_empty() {
None
} else {
Some(result)
}
}
pub fn tree(&self, depth: usize, from_ref: Option<&str>) -> String {
tree::get_tree(&self.snapshot_text, depth, from_ref)
}
pub fn blank_tab_count(&self) -> usize {
summary::count_blank_tabs(&self.snapshot_text)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_from_json() {
let json = serde_json::json!({
"content": [{ "text": "test content" }]
});
let mh = Meyerhold::from_json(&json).unwrap();
assert_eq!(mh.content(), "test content");
}
#[test]
fn test_from_text() {
let mh = Meyerhold::from_text("direct text");
assert_eq!(mh.content(), "direct text");
}
}