use crate::table::ToBytes;
#[derive(Debug, Clone)]
pub enum MatchValue {
ExactValue {
bytes: Vec<u8>,
},
RangeValue {
lower_bytes: Vec<u8>,
higher_bytes: Vec<u8>,
},
LPM {
bytes: Vec<u8>,
prefix_length: i32,
},
Ternary {
value: Vec<u8>,
mask: Vec<u8>,
},
}
impl MatchValue {
pub fn exact<T: ToBytes>(value: T) -> MatchValue {
MatchValue::ExactValue {
bytes: value.to_bytes(),
}
}
pub fn get_exact_value(&self) -> &Vec<u8> {
match self {
MatchValue::ExactValue { bytes } => bytes,
_ => panic!("No exact match value."),
}
}
pub fn range<T: ToBytes>(lower: T, higher: T) -> MatchValue {
MatchValue::RangeValue {
lower_bytes: lower.to_bytes(),
higher_bytes: higher.to_bytes(),
}
}
pub fn get_range_value(&self) -> (&Vec<u8>, &Vec<u8>) {
match self {
MatchValue::RangeValue {
lower_bytes,
higher_bytes,
} => (lower_bytes, higher_bytes),
_ => panic!("No range match value."),
}
}
pub fn lpm<T: ToBytes>(value: T, prefix_length: i32) -> MatchValue {
MatchValue::LPM {
bytes: value.to_bytes(),
prefix_length,
}
}
pub fn ternary<T: ToBytes>(value: T, mask: T) -> MatchValue {
MatchValue::Ternary {
value: value.to_bytes(),
mask: mask.to_bytes(),
}
}
}