use std::str::FromStr;
use crate::{
rule::{extract_rules, ConvAction},
variant::{Variant, VariantMap},
};
#[derive(Debug, Clone)]
pub struct PageRules {
title: Option<VariantMap<String>>,
conv_actions: Vec<ConvAction>,
}
impl PageRules {
pub fn get_title(&self, target: Variant) -> Option<&str> {
self.title
.as_ref()
.and_then(|map| map.get_text_with_fallback(target))
}
pub fn as_conv_actions(&self) -> &[ConvAction] {
&self.conv_actions
}
}
impl FromStr for PageRules {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut title = None;
let mut conv_actions = vec![];
for rule in extract_rules(s).filter_map(|r| r.ok()) {
if rule.set_title {
if let Some(map) = rule.conv.as_ref().and_then(|conv| conv.get_bid()).cloned() {
if !map.is_empty() {
title = Some(map); }
}
}
if let Some(ca) = rule.into_conv_action() {
conv_actions.push(ca);
}
}
Ok(PageRules {
title,
conv_actions,
})
}
}