use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Match {
pub cell_index: usize,
pub cell_number: usize,
pub execution_count: Option<i32>,
pub match_type: MatchType,
pub line_index: usize,
pub line_number: usize,
pub line_content: String,
pub matched_text: String,
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub context_before: Vec<String>,
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub context_after: Vec<String>,
}
impl Match {
pub fn new(
cell_index: usize,
execution_count: Option<i32>,
match_type: MatchType,
line_index: usize,
line_content: String,
matched_text: String,
) -> Self {
Self {
cell_index,
cell_number: cell_index + 1,
execution_count,
match_type,
line_index,
line_number: line_index + 1,
line_content,
matched_text,
context_before: Vec::new(),
context_after: Vec::new(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum MatchType {
Input,
Output,
}
impl std::fmt::Display for MatchType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
MatchType::Input => write!(f, "input"),
MatchType::Output => write!(f, "output"),
}
}
}
#[derive(Debug, Clone)]
pub struct GrepOptions {
pub pattern: String,
pub case_insensitive: bool,
pub search_inputs: bool,
pub search_outputs: bool,
pub context_lines: Option<usize>,
pub context_before: Option<usize>,
pub context_after: Option<usize>,
pub word_regexp: bool,
pub fixed_strings: bool,
pub only_matching: bool,
pub invert_match: bool,
pub max_count: Option<usize>,
pub code_cells_only: bool,
pub markdown_cells_only: bool,
pub raw_cells_only: bool,
pub executed_only: bool,
pub not_executed_only: bool,
pub stream_output_only: bool,
pub error_output_only: bool,
pub result_output_only: bool,
pub glob_pattern: Option<String>,
pub exclude_pattern: Option<String>,
}
impl Default for GrepOptions {
fn default() -> Self {
Self {
pattern: String::new(),
case_insensitive: false,
search_inputs: true,
search_outputs: true,
context_lines: None,
context_before: None,
context_after: None,
word_regexp: false,
fixed_strings: false,
only_matching: false,
invert_match: false,
max_count: None,
code_cells_only: false,
markdown_cells_only: false,
raw_cells_only: false,
executed_only: false,
not_executed_only: false,
stream_output_only: false,
error_output_only: false,
result_output_only: false,
glob_pattern: None,
exclude_pattern: None,
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct GrepResult {
pub notebook: String,
pub matches: Vec<Match>,
}
impl GrepResult {
pub fn new(notebook: String) -> Self {
Self {
notebook,
matches: Vec::new(),
}
}
pub fn is_empty(&self) -> bool {
self.matches.is_empty()
}
pub fn match_count(&self) -> usize {
self.matches.len()
}
}