impl include=regex that only includes reg match

This commit is contained in:
lda committed 2025-05-22 22:35:03 +07:00
1 parent 7806fba9e6
commit 5360d3bc26
1 file changed
+49 -21
+49 -21
View File
@@ -79,7 +79,7 @@ async fn filtered_ics(
};
// Compile regex
let regex_rules = match query.get("regex") {
let regex_exclude_rules = match query.get("regex") {
Some(rule) => match Regex::new(rule) {
Ok(reg_mc) => Some(reg_mc),
Err(e) => {
@@ -89,9 +89,27 @@ async fn filtered_ics(
},
None => None,
};
if let Some(real) = &regex_rules {
if let Some(real) = &regex_exclude_rules {
println!("there is a regex rule and it is {}", real.as_str())
};
// Compile include regex
let regex_include_rules = match query.get("include") {
Some(rule) => match Regex::new(rule) {
Ok(reg_mc) => Some(reg_mc),
Err(e) => {
eprintln!("request include regex is ass: {e}");
return HttpResponse::BadRequest().body(format!("Invalid include regex rule: {e}"));
}
},
None => None,
};
if let Some(real) = &regex_include_rules {
println!("there is an include regex rule and it is {}", real.as_str());
};
// Do the CPU-heavy stuff on another thread
let block_task = tokio::task::spawn_blocking(move || -> Result<String, String> {
let calendar = body.parse::<Calendar>()?;
@@ -99,8 +117,13 @@ async fn filtered_ics(
let start_date = now - TimeDelta::days(365);
let end_date = now + TimeDelta::days(365);
let filtered_events =
filter_events_by_date_and_regex(calendar, regex_rules, start_date, end_date);
let filtered_events = filter_events_by_date_and_regex(
calendar,
regex_exclude_rules,
regex_include_rules, // Pass include rule here
start_date,
end_date,
);
let filtered_calendar = Calendar {
components: filtered_events,
@@ -128,12 +151,11 @@ async fn filtered_ics(
fn filter_events_by_date_and_regex(
calendar: Calendar,
pattern: Option<Regex>,
exclude_pattern: Option<Regex>,
include_pattern: Option<Regex>,
start_date: NaiveDate,
end_date: NaiveDate,
) -> Vec<CalendarComponent> {
// let summary_filter = Regex::new(pattern).unwrap();
calendar
.components
.into_iter()
@@ -149,21 +171,27 @@ fn filter_events_by_date_and_regex(
let summary = event.get_summary()?;
let kill = if let Some(reg) = &pattern {
reg.is_match(summary)
.inspect_err(|e| eprintln!("{e}"))
.unwrap_or(false)
// where is the
} else {
summary.contains("CLASS 2") || (2..=8).any(|c| summary.contains(&format!("G{c}")))
};
if kill {
// not perfect...
None
} else {
Some(CalendarComponent::Event(event))
// Apply exclude rule
if let Some(reg) = &exclude_pattern {
if reg.is_match(summary).unwrap_or_else(|e| {
eprintln!("Error in exclude regex match: {e}");
false
}) {
return None;
}
}
// Apply include rule
if let Some(reg) = &include_pattern {
if !reg.is_match(summary).unwrap_or_else(|e| {
eprintln!("Error in include regex match: {e}");
false
}) {
return None;
}
}
Some(CalendarComponent::Event(event))
})
.collect()
}