diff --git a/domo_lib/Cargo.lock b/domo_lib/Cargo.lock index 8bd1973..ac1b40c 100644 --- a/domo_lib/Cargo.lock +++ b/domo_lib/Cargo.lock @@ -19,7 +19,7 @@ dependencies = [ [[package]] name = "domo_lib" -version = "0.2.0" +version = "0.3.0" dependencies = [ "domo_proto", ] diff --git a/domo_lib/Cargo.toml b/domo_lib/Cargo.toml index a6274f4..294f43e 100644 --- a/domo_lib/Cargo.toml +++ b/domo_lib/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "domo_lib" -version = "0.2.0" +version = "0.3.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/domo_lib/src/filter/filters/function_filter.rs b/domo_lib/src/filter/filters/function_filter.rs deleted file mode 100644 index 1049e9a..0000000 --- a/domo_lib/src/filter/filters/function_filter.rs +++ /dev/null @@ -1,6 +0,0 @@ -use domo_proto::packet::Packet; - -use crate::filter::FilterState; - -pub type FunctionFilterFunction = fn(FilterState) -> FilterState; -pub struct FunctionFilter(pub FunctionFilterFunction); diff --git a/domo_lib/src/filter/filters/mod.rs b/domo_lib/src/filter/filters/mod.rs deleted file mode 100644 index c8eba15..0000000 --- a/domo_lib/src/filter/filters/mod.rs +++ /dev/null @@ -1,16 +0,0 @@ -use super::Filter; - -pub mod function_filter; -pub mod packet; - -/// Only allow Pass or Drops. -pub struct StrictFilter; -impl Filter for StrictFilter { - fn filter(&self, f: super::FilterState) -> super::FilterState { - if let super::FilterState::Skip(_) = f { - super::FilterState::Drop - } else { - f - } - } -} \ No newline at end of file diff --git a/domo_lib/src/filter/filters/packet.rs b/domo_lib/src/filter/filters/packet.rs deleted file mode 100644 index 882fd74..0000000 --- a/domo_lib/src/filter/filters/packet.rs +++ /dev/null @@ -1,33 +0,0 @@ -use domo_proto::packet::Packet; - -use crate::filter::{Filter, FilterState}; - -use super::function_filter::FunctionFilter; - -impl Filter for FunctionFilter { - fn filter(&self, f: FilterState) -> FilterState { - (self.0)(f) - } -} - -impl Filter for Packet { - fn filter(&self, f: FilterState) -> FilterState { - match f { - FilterState::Pass(p) => { - if &p != self { - FilterState::Drop - } else { - FilterState::Pass(p) - } - } - FilterState::Skip(p) => { - if &p != self { - FilterState::Drop - } else { - FilterState::Pass(p) - } - } - FilterState::Drop => FilterState::Drop, - } - } -} diff --git a/domo_lib/src/filter/mod.rs b/domo_lib/src/filter/mod.rs deleted file mode 100644 index e6de447..0000000 --- a/domo_lib/src/filter/mod.rs +++ /dev/null @@ -1,50 +0,0 @@ -pub mod filters; - -#[derive(Clone)] -pub enum FilterState { - /// T passed filter - Pass(T), - /// T was not subject to filter. - Skip(T), - /// T did not pass filter - Drop, -} - -impl FilterState { - pub fn to_type(self) -> Option { - match self { - FilterState::Pass(v) | FilterState::Skip(v) => Some(v), - FilterState::Drop => None - } - } - - pub fn is_pass(&self) -> bool { - if let FilterState::Pass(_) = self { - true - } else { - false - } - } - - pub fn is_skip(&self) -> bool { - if let FilterState::Skip(_) = self { - true - } else { - false - } - } - - pub fn is_drop(&self) -> bool { - if let FilterState::Drop = self { - true - } else { - false - } - } -} - -pub trait Filter { - fn filter(&self, f: FilterState) -> FilterState { - f - } -} diff --git a/domo_lib/src/lib.rs b/domo_lib/src/lib.rs index 4b89800..ed4f9be 100644 --- a/domo_lib/src/lib.rs +++ b/domo_lib/src/lib.rs @@ -1,6 +1,2 @@ /// Node abstraction -pub mod node; -/// Filters -pub mod filter; -/// Routing essentials -pub mod routing; \ No newline at end of file +pub mod node; \ No newline at end of file diff --git a/domo_lib/src/node/mod.rs b/domo_lib/src/node/mod.rs index c40ed61..cba5ce8 100644 --- a/domo_lib/src/node/mod.rs +++ b/domo_lib/src/node/mod.rs @@ -1,7 +1,5 @@ -use crate::filter::Filter; - use self::property::Property; -use domo_proto::{identifier::Identifier, packet::Packet}; +use domo_proto::identifier::Identifier; use std::collections::HashMap; pub mod property; @@ -12,11 +10,11 @@ pub struct Node { /// Who owns this node? pub parent: Option, /// Identifier of node - pub identifier: Identifier, + identifier: Identifier, /// Child nodes children: HashMap, /// All properties of the node. - pub properties: HashMap, + properties: HashMap, } impl Node { @@ -30,6 +28,25 @@ impl Node { } } + // Return reference to Node's identifier + pub fn identifier(&self) -> &Identifier { + &self.identifier + } + + /// (Over)writes property to self. + /// Returns a mutable reference to property. + pub fn insert_property<'a>(&'a mut self, property: Property) -> &'a mut Property { + self.properties + .insert(property.name.clone(), property.clone()); + self.properties.get_mut(&property.name).unwrap() + } + + /// Attempts to remove property from self. + /// Returns a mutable reference to the added node. + pub fn remove_property(&mut self, name: String) -> Option { + self.properties.remove(&name) + } + /// Attempts to add child node to self. /// Returns a mutable reference to the added node. pub fn add_child<'a>(&'a mut self, mut node: Node) -> Option<&'a mut Node> { @@ -50,49 +67,35 @@ impl Node { }) } - /// Returns if this node has the identifier. + /// Check if node has an identifier. + /// Only checks children. pub fn has(&self, identifier: &Identifier) -> bool { self.children.contains_key(identifier) } - /// Attempts to find given identifier and returns mutable reference to it. - pub fn find<'a>(&'a mut self, identifier: &Identifier) -> Option<&'a mut Node> { - for (_, child) in self.children.iter_mut() { + /// Attempts to find given identifier and returns reference to it. + pub fn find<'a>(&'a self, identifier: &Identifier) -> Option<&'a Node> { + if self.identifier == *identifier { + return Some(self); + } + for (_, child) in self.children.iter() { if let Some(found) = child.find(identifier) { return Some(found); } } None } -} -impl Filter for Node { - fn filter( - &self, - f: crate::filter::FilterState, - ) -> crate::filter::FilterState { - if let Some(i) = f.to_type() { - if self.has(&i) || i == self.identifier { - crate::filter::FilterState::Pass(i) - } else { - crate::filter::FilterState::Skip(i) - } - } else { - crate::filter::FilterState::Drop - } - } -} - -impl Filter for Node { - fn filter(&self, f: crate::filter::FilterState) -> crate::filter::FilterState { - if let Some(p) = f.to_type() { - if self.has(&p.dest) || self.identifier == p.dest { - crate::filter::FilterState::Pass(p) - } else { - crate::filter::FilterState::Skip(p) - } - } else { - crate::filter::FilterState::Drop + /// Attempts to find given identifier and returns mutable reference to it. + pub fn find_mut<'a>(&'a mut self, identifier: &Identifier) -> Option<&'a mut Node> { + if self.identifier == *identifier { + return Some(self); } + for (_, child) in self.children.iter_mut() { + if let Some(found) = child.find_mut(identifier) { + return Some(found); + } + } + None } } diff --git a/domo_lib/src/routing/endpoint.rs b/domo_lib/src/routing/endpoint.rs deleted file mode 100644 index 8b25a05..0000000 --- a/domo_lib/src/routing/endpoint.rs +++ /dev/null @@ -1,7 +0,0 @@ -use crate::filter::Filter; - -/// Endpoints are a non-returning trait. -/// Because endpoints *should* be the last thing in the chain, you *should* manage errors in there! -pub trait Endpoint: Filter { - fn handle(&mut self, item: T); -} diff --git a/domo_lib/src/routing/mod.rs b/domo_lib/src/routing/mod.rs deleted file mode 100644 index 1b020e3..0000000 --- a/domo_lib/src/routing/mod.rs +++ /dev/null @@ -1,62 +0,0 @@ -use domo_proto::packet::Packet; - -use crate::filter::{ - filters::function_filter::{FunctionFilter, FunctionFilterFunction}, - Filter, FilterState, -}; - -use self::endpoint::Endpoint; - -pub mod endpoint; - -/// The Router will first filter the packet through the filter chain. -/// Then it will filter past all endpoints and send the packet if the endpoint filter passed. -pub struct Router<'a> { - filters: Vec + 'a>>, - endpoints: Vec + 'a>>, -} - -impl<'a> Router<'a> { - pub fn new() -> Router<'a> { - Router { - filters: vec![], - endpoints: vec![], - } - } - - pub fn add_filter(&mut self, filter: impl Filter + 'a) { - self.filters.push(Box::new(filter)); - } - - pub fn add_function_filter(&mut self, function: FunctionFilterFunction) { - self.filters.push(Box::new(FunctionFilter(function))) - } - - pub fn add_endpoint(&mut self, endpoint: impl Endpoint + 'a) { - self.endpoints.push(Box::new(endpoint)) - } -} - -impl Filter for Router<'_> { - fn filter(&self, f: FilterState) -> FilterState { - let mut f = f; - for filter in &self.filters { - f = filter.filter(f); - } - f - } -} - -impl Endpoint for Router<'_> { - fn handle(&mut self, item: Packet) { - let item = self.filter(FilterState::Pass(item)); - for endpoint in &mut self.endpoints { - match endpoint.filter(item.clone()) { - FilterState::Pass(p) | FilterState::Skip(p) => { - let _ = &endpoint.handle(p); - } - FilterState::Drop => {} - } - } - } -}