use std::{
convert::TryFrom,
error::Error,
fmt::{self, Display, Formatter},
};
use serde::{Deserialize, Serialize};
use super::{
Action, ConditionalPushRule, ConditionalPushRuleInit, PatternedPushRule, PatternedPushRuleInit,
PushCondition, PushRule, PushRuleInit,
};
#[derive(Clone, Debug, Serialize, Deserialize)]
#[non_exhaustive]
pub struct AnyPushRule {
pub actions: Vec<Action>,
pub default: bool,
pub enabled: bool,
pub rule_id: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub conditions: Option<Vec<PushCondition>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub pattern: Option<String>,
}
impl From<PushRule> for AnyPushRule {
fn from(push_rule: PushRule) -> Self {
let PushRule { actions, default, enabled, rule_id } = push_rule;
Self { actions, default, enabled, rule_id, conditions: None, pattern: None }
}
}
impl From<PatternedPushRule> for AnyPushRule {
fn from(push_rule: PatternedPushRule) -> Self {
let PatternedPushRule { actions, default, enabled, rule_id, pattern } = push_rule;
Self { actions, default, enabled, rule_id, conditions: None, pattern: Some(pattern) }
}
}
impl From<ConditionalPushRule> for AnyPushRule {
fn from(push_rule: ConditionalPushRule) -> Self {
let ConditionalPushRule { actions, default, enabled, rule_id, conditions } = push_rule;
Self { actions, default, enabled, rule_id, conditions: Some(conditions), pattern: None }
}
}
impl From<PushRuleInit> for AnyPushRule {
fn from(init: PushRuleInit) -> Self {
let PushRuleInit { actions, default, enabled, rule_id } = init;
Self { actions, default, enabled, rule_id, pattern: None, conditions: None }
}
}
impl From<ConditionalPushRuleInit> for AnyPushRule {
fn from(init: ConditionalPushRuleInit) -> Self {
let ConditionalPushRuleInit { actions, default, enabled, rule_id, conditions } = init;
Self { actions, default, enabled, rule_id, pattern: None, conditions: Some(conditions) }
}
}
impl From<PatternedPushRuleInit> for AnyPushRule {
fn from(init: PatternedPushRuleInit) -> Self {
let PatternedPushRuleInit { actions, default, enabled, rule_id, pattern } = init;
Self { actions, default, enabled, rule_id, pattern: Some(pattern), conditions: None }
}
}
impl From<AnyPushRule> for PushRule {
fn from(push_rule: AnyPushRule) -> Self {
let AnyPushRule { actions, default, enabled, rule_id, .. } = push_rule;
PushRule { actions, default, enabled, rule_id }
}
}
#[derive(Debug)]
#[non_exhaustive]
pub struct MissingPatternError;
impl Display for MissingPatternError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "Push rule does not have a pattern.")
}
}
impl Error for MissingPatternError {}
impl TryFrom<AnyPushRule> for PatternedPushRule {
type Error = MissingPatternError;
fn try_from(push_rule: AnyPushRule) -> Result<Self, Self::Error> {
if let AnyPushRule { actions, default, enabled, rule_id, pattern: Some(pattern), .. } =
push_rule
{
Ok(PatternedPushRule { actions, default, enabled, rule_id, pattern })
} else {
Err(MissingPatternError)
}
}
}
#[derive(Debug)]
#[non_exhaustive]
pub struct MissingConditionsError;
impl Display for MissingConditionsError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "Push rule has no conditions.")
}
}
impl Error for MissingConditionsError {}
impl TryFrom<AnyPushRule> for ConditionalPushRule {
type Error = MissingConditionsError;
fn try_from(push_rule: AnyPushRule) -> Result<Self, Self::Error> {
if let AnyPushRule {
actions,
default,
enabled,
rule_id,
conditions: Some(conditions),
..
} = push_rule
{
Ok(ConditionalPushRule { actions, default, enabled, rule_id, conditions })
} else {
Err(MissingConditionsError)
}
}
}