diff --git a/examples/eval.rs b/examples/eval.rs index ad7365ce..fe8fd836 100644 --- a/examples/eval.rs +++ b/examples/eval.rs @@ -9,6 +9,8 @@ use roto::types::typevalue::TypeValue; use roto::vm; use roto::blocks::Scope::{self, FilterMap}; +use routecore::bgp::communities::HumanReadableCommunity as Community; + fn test_data( name: Scope, source_code: &'static str, @@ -30,7 +32,7 @@ fn test_data( let asn = Asn::from_u32(211321).into(); let comms = TypeValue::List(List::new(vec![ElementTypeValue::Primitive( - routecore::bgp::communities::Community::from([127, 12, 13, 12]).into()) + Community::from([127, 12, 13, 12]).into()) ])); let my_comms_type = TypeDef::List(Box::new(TypeDef::List(Box::new(TypeDef::Community)))); diff --git a/examples/route.rs b/examples/route.rs index 49cb0911..4bdbb636 100644 --- a/examples/route.rs +++ b/examples/route.rs @@ -44,7 +44,7 @@ fn test_data( .0 .announcements() .unwrap() - .filter_map(|p| if let Ok(Nlri::Unicast(BasicNlri { prefix, .. })) = p { Some(Prefix::from(prefix)) } else { None }) + .filter_map(|p| if let Ok(Nlri::Unicast(BasicNlri { prefix, .. })) = p { Some(prefix) } else { None }) .collect(); let msg_id = (RotondaId(0), 0); diff --git a/examples/types.rs b/examples/types.rs index 061111ff..25d0bbbd 100644 --- a/examples/types.rs +++ b/examples/types.rs @@ -5,6 +5,8 @@ use roto::types::collections::{ElementTypeValue, List, Record}; use roto::types::typedef::TypeDef; use roto::types::typevalue::TypeValue; +use routecore::bgp::communities::HumanReadableCommunity as Community; + fn main() { // let count = RotoType::create_primitive_var( // RotoType::Asn, @@ -45,7 +47,7 @@ fn main() { let comms = TypeValue::List(List::new(vec![ElementTypeValue::Primitive( - routecore::bgp::communities::Community::from([127, 12, 13, 12]).into()) + Community::from([127, 12, 13, 12]).into()) ])); let my_comms_type = diff --git a/src/ast.rs b/src/ast.rs index 1b4e6e0c..66652525 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -30,6 +30,9 @@ use nom::{AsChar, Finish}; use serde::{Serialize, Serializer}; use smallvec::SmallVec; +use routecore::bgp::communities::HumanReadableCommunity as Community; +use routecore::bgp::communities::{ExtendedCommunity, LargeCommunity, StandardCommunity}; + use crate::compiler::error::CompileError; use crate::types::builtin::{Asn, Boolean}; use crate::types::typevalue::TypeValue; @@ -2147,23 +2150,23 @@ impl From<&'_ StandardCommunityLiteral> for ShortString { } } -impl TryFrom<&'_ StandardCommunityLiteral> for routecore::bgp::communities::Community { +impl TryFrom<&'_ StandardCommunityLiteral> for Community { type Error = CompileError; // The aforementioned heavy lifting is here. fn try_from( literal: &StandardCommunityLiteral, ) -> Result { - let comm = ::from_str( + let comm = ::from_str( &literal.0 ).map_err( |e| CompileError::from(format!( "Cannot convert literal '{}' into Extended Community: {e}", literal.0, )))?; - Ok(routecore::bgp::communities::Community::Standard( - comm, - )) + Ok( + comm.into(), + ) } } @@ -2252,23 +2255,23 @@ impl From<&'_ ExtendedCommunityLiteral> for ShortString { } } -impl<'a> TryFrom<&'a ExtendedCommunityLiteral> for routecore::bgp::communities::Community { +impl<'a> TryFrom<&'a ExtendedCommunityLiteral> for Community { type Error = CompileError; // The aforementioned heavy lifting is here. fn try_from( literal: &ExtendedCommunityLiteral, ) -> Result { - let comm = ::from_str( + let comm = ::from_str( &literal.0 ).map_err( |e| CompileError::from(format!( "Cannot convert literal '{}' into Extended Community: {e}", literal.0, )))?; - Ok(routecore::bgp::communities::Community::Extended( - comm, - )) + Ok( + comm.into() + ) } } @@ -2378,23 +2381,21 @@ impl From<&'_ LargeCommunityLiteral> for ShortString { } } -impl TryFrom<&'_ LargeCommunityLiteral> for routecore::bgp::communities::Community { +impl TryFrom<&'_ LargeCommunityLiteral> for Community { type Error = CompileError; // The aforementioned heavy lifting is here. fn try_from( literal: &LargeCommunityLiteral, ) -> Result { - let comm = ::from_str( + let comm = ::from_str( &literal.0 ).map_err( |e| CompileError::from(format!( "Cannot convert literal '{}' into Large Community: {e}", literal.0, )))?; - Ok(routecore::bgp::communities::Community::Large( - comm, - )) + Ok(comm.into()) } } diff --git a/src/attr_change_set.rs b/src/attr_change_set.rs index 580a73c9..490cda8f 100644 --- a/src/attr_change_set.rs +++ b/src/attr_change_set.rs @@ -5,7 +5,7 @@ use routecore::bgp::aspath::HopPath; use routecore::bgp::message::nlri::PathId; use routecore::bgp::types::AfiSafi; use routecore::addr::Prefix; -use routecore::bgp::communities::Community; +use routecore::bgp::communities::HumanReadableCommunity as Community; use serde::Serialize; use std::marker::PhantomData; use std::ops::Index; diff --git a/src/types/builtin/builtin_type_value.rs b/src/types/builtin/builtin_type_value.rs index fe8491ed..f7780d53 100644 --- a/src/types/builtin/builtin_type_value.rs +++ b/src/types/builtin/builtin_type_value.rs @@ -7,7 +7,7 @@ use std::fmt::Display; use routecore::bgp::message::nlri::PathId; use routecore::bgp::types::{AfiSafi, NextHop}; use routecore::addr::Prefix; -use routecore::bgp::communities::Community; +use routecore::bgp::communities::HumanReadableCommunity as Community; use serde::Serialize; use crate::compiler::compile::CompileError; diff --git a/src/types/builtin/primitives.rs b/src/types/builtin/primitives.rs index 0f809d4f..9608ee55 100644 --- a/src/types/builtin/primitives.rs +++ b/src/types/builtin/primitives.rs @@ -19,6 +19,8 @@ use super::super::typedef::TypeDef; use super::super::typevalue::TypeValue; use super::builtin_type_value::BuiltinTypeValue; +use routecore::bgp::communities::HumanReadableCommunity as Community; + //------------ U16 Type ----------------------------------------------------- // #[derive(Debug, Eq, Copy, Clone, Serialize)] @@ -2244,7 +2246,7 @@ impl RotoType for routecore::bgp::message::nlri::PathId { // } // } -impl RotoType for routecore::bgp::communities::Community { +impl RotoType for Community { fn get_props_for_method( _ty: TypeDef, method_name: &crate::ast::Identifier, @@ -2385,28 +2387,23 @@ impl RotoType for routecore::bgp::communities::Community { } } -impl ScalarValue for routecore::bgp::communities::Community {} +impl ScalarValue for Community {} -impl From for TypeValue { - fn from(val: routecore::bgp::communities::Community) -> Self { +impl From for TypeValue { + fn from(val: Community) -> Self { TypeValue::Builtin(BuiltinTypeValue::Community(val)) } } -// impl From for TypeValue { -// fn from(val: routecore::bgp::communities::Community) -> Self { -// TypeValue::Builtin(BuiltinTypeValue::Community(val)) -// } -// } -impl From for BuiltinTypeValue { - fn from(value: routecore::bgp::communities::Community) -> Self { +impl From for BuiltinTypeValue { + fn from(value: Community) -> Self { BuiltinTypeValue::Community(value) } } -impl From> for TypeValue { - fn from(value: Vec) -> Self { +impl From> for TypeValue { + fn from(value: Vec) -> Self { let list: Vec = value .iter() .map(|c| ElementTypeValue::Primitive(TypeValue::from(*c))) @@ -3009,7 +3006,6 @@ impl VectorValue for routecore::bgp::aspath::HopPath { &mut self, vector: Vec, ) -> Result<(), LongSegmentError> { - for asn in vector { self.append(OwnedHop::Asn(asn.0)); } diff --git a/src/types/builtin/route.rs b/src/types/builtin/route.rs index 669e8b40..28d954b3 100644 --- a/src/types/builtin/route.rs +++ b/src/types/builtin/route.rs @@ -12,8 +12,13 @@ // └─────────────┘ status use log::{debug, error}; -use routecore::bgp::{message::{SessionConfig, nlri::PathId}, types::{AfiSafi, NextHop}, aspath::HopPath}; use routecore::addr::Prefix; +use routecore::bgp::{ + aspath::HopPath, + communities::HumanReadableCommunity as Community, + message::{nlri::PathId, SessionConfig}, + types::{AfiSafi, NextHop}, +}; use serde::{Deserialize, Serialize}; use smallvec::SmallVec; use std::{net::IpAddr, sync::Arc}; @@ -35,10 +40,7 @@ use crate::{ vm::{StackValue, VmError}, }; -use super::{ - Asn, BuiltinTypeValue, IpAddress, OriginType, - RouteStatus, -}; +use super::{Asn, BuiltinTypeValue, IpAddress, OriginType, RouteStatus}; use crate::attr_change_set::{ AttrChangeSet, ScalarOption, ScalarValue, VectorOption, VectorValue, }; @@ -161,7 +163,7 @@ impl RawRouteWithDeltas { peer_asn, router_id.clone(), afi_safi, - path_id + path_id, )?, ))?; @@ -280,7 +282,7 @@ impl RawRouteWithDeltas { self.peer_asn, self.router_id.clone(), self.afi_safi, - self.path_id + self.path_id, )?) } } @@ -311,7 +313,7 @@ impl RawRouteWithDeltas { self.peer_asn, self.router_id, self.afi_safi, - self.path_id + self.path_id, ); } @@ -337,7 +339,7 @@ impl RawRouteWithDeltas { self.peer_asn, self.router_id.clone(), self.afi_safi, - self.path_id + self.path_id, )?, ))?; } @@ -451,7 +453,9 @@ impl RawRouteWithDeltas { &self, field_token: usize, ) -> Result, VmError> { - let current_set = if let Some(atrd) = self.attribute_deltas.get_latest_change_set() { + let current_set = if let Some(atrd) = + self.attribute_deltas.get_latest_change_set() + { atrd } else { return Err(VmError::InvalidRecord); @@ -481,7 +485,6 @@ impl RawRouteWithDeltas { &self, field_token: usize, ) -> Result { - match field_token.try_into()? { RouteToken::AsPath => self .raw_message @@ -548,15 +551,21 @@ impl RawRouteWithDeltas { .raw_message .raw_message .0 - .all_communities() + .all_communities::() .ok() .flatten() .map(TypeValue::from) .ok_or(VmError::InvalidFieldAccess), RouteToken::Prefix => Ok(self.prefix.into()), RouteToken::Status => Ok(self.status_deltas.current()), - RouteToken::PeerIp => self.peer_ip.map(TypeValue::from).ok_or(VmError::InvalidFieldAccess), - RouteToken::PeerAsn => self.peer_asn.map(TypeValue::from).ok_or(VmError::InvalidFieldAccess), + RouteToken::PeerIp => self + .peer_ip + .map(TypeValue::from) + .ok_or(VmError::InvalidFieldAccess), + RouteToken::PeerAsn => self + .peer_asn + .map(TypeValue::from) + .ok_or(VmError::InvalidFieldAccess), // _ => None, // originator_id: ChangedOption { // value: None, @@ -1192,7 +1201,7 @@ impl TryFrom for RouteToken { 9 => Ok(RouteToken::Status), 10 => Ok(RouteToken::PeerIp), 11 => Ok(RouteToken::PeerAsn), - _ => { + _ => { debug!("Unknown RouteToken value: {}", value); Err(VmError::InvalidMethodCall) } @@ -1386,7 +1395,9 @@ impl UpdateMessage { self.0.aggregator().map_err(VmError::from)?, ), communities: VectorOption::from( - self.0.all_communities().map_err(VmError::from)?, + self.0 + .all_communities::() + .map_err(VmError::from)?, ), peer_ip: peer_ip.into(), peer_asn: peer_asn.into(), diff --git a/src/types/builtin/tests.rs b/src/types/builtin/tests.rs index 00f38804..2e315257 100644 --- a/src/types/builtin/tests.rs +++ b/src/types/builtin/tests.rs @@ -17,7 +17,7 @@ mod route { _Consume, } use routecore::bgp::aspath::HopPath; - use routecore::bgp::communities::{Community, StandardCommunity, Tag, ExtendedCommunity, LargeCommunity}; + use routecore::bgp::communities::{HumanReadableCommunity as Community, StandardCommunity, Tag, ExtendedCommunity, LargeCommunity}; use routecore::bgp::{ message::{ nlri::{BasicNlri, Nlri}, @@ -932,12 +932,11 @@ src_ty.clone().test_type_conversion(arg_ty)"] #[test] fn test_standard_community_2() { - let test_value = Community::Standard( + let test_value: Community = StandardCommunity::new( routecore::asn::Asn16::from(12500), Tag::new(7890), - ), - ); + ).into(); let res = StringLiteral::new("AS12500:7890".to_string()); mk_converted_type_value(test_value, res).unwrap(); @@ -986,12 +985,11 @@ src_ty.clone().test_type_conversion(arg_ty)"] LargeCommunity::from_str( "234:123:456" ).unwrap().into(); - let res = Community::Standard( + let res: Community = StandardCommunity::new( routecore::asn::Asn16::from(7500), Tag::new(3000), - ) - ); + ).into(); test_consume_method_on_type_value(test_value, "set", res) } diff --git a/src/types/enum_types.rs b/src/types/enum_types.rs index 16bde64a..df7bdef4 100644 --- a/src/types/enum_types.rs +++ b/src/types/enum_types.rs @@ -2,7 +2,7 @@ use std::fmt::{Debug, Display}; use std::str::FromStr; use log::trace; -use routecore::bgp::communities::{Wellknown, Community, StandardCommunity}; +use routecore::bgp::communities::{Wellknown, HumanReadableCommunity as Community, StandardCommunity}; use routecore::bgp::types::{Afi, Safi}; use routecore::bmp::message::MessageType; use serde::Serialize; @@ -74,7 +74,7 @@ where TypeDef::ConstEnumVariant(_) => Ok(self.into()), TypeDef::U32 => Ok(u32::from(self.value).into()), TypeDef::Community => { - Ok(Community::Standard(StandardCommunity::from(::from(self.value))).into()) + Ok(Community(StandardCommunity::from(::from(self.value)).into()).into()) }, _ => Err(format!( "Cannot convert type EnumVariant to type {:?}", diff --git a/src/types/typedef.rs b/src/types/typedef.rs index f28d2a9a..0c6312f0 100644 --- a/src/types/typedef.rs +++ b/src/types/typedef.rs @@ -9,6 +9,7 @@ use routecore::bgp::aspath::HopPath; use serde::Serialize; use routecore::bgp::{types::AfiSafi, message::nlri::PathId}; use routecore::addr::Prefix; +use routecore::bgp::communities::HumanReadableCommunity as Community; use crate::compiler::compile::CompileError; use crate::traits::Token; @@ -602,7 +603,7 @@ impl TypeDef { Hop::get_props_for_method(self.clone(), method_name) } TypeDef::Community => { - routecore::bgp::communities::Community::get_props_for_method(self.clone(), method_name) + Community::get_props_for_method(self.clone(), method_name) } TypeDef::OriginType => { OriginType::get_props_for_method(self.clone(), method_name) diff --git a/src/types/typevalue.rs b/src/types/typevalue.rs index 42b10ad7..b8f0f199 100644 --- a/src/types/typevalue.rs +++ b/src/types/typevalue.rs @@ -9,6 +9,7 @@ use primitives::{ use routecore::bgp::aspath::HopPath; use routecore::bgp::message::nlri::PathId; use routecore::bgp::types::{AfiSafi, NextHop}; +use routecore::bgp::communities::HumanReadableCommunity as Community; use serde::Serialize; //============ TypeValue ==================================================== @@ -337,7 +338,7 @@ impl RotoType for TypeValue { Boolean::get_props_for_method(ty, method_name) } TypeDef::Community => { - routecore::bgp::communities::Community::get_props_for_method(ty, method_name) + Community::get_props_for_method(ty, method_name) } TypeDef::ConstEnumVariant(_) => Err(CompileError::new( "Unsupported TypeDef::ConstEnumVariant in TypeValue::\ diff --git a/tests/data_sources.rs b/tests/data_sources.rs index 8e05b892..1a644259 100644 --- a/tests/data_sources.rs +++ b/tests/data_sources.rs @@ -9,6 +9,8 @@ use roto::types::typedef::TypeDef; use roto::types::typevalue::TypeValue; use roto::vm; +use routecore::bgp::communities::HumanReadableCommunity as Community; + mod common; fn test_data( @@ -39,10 +41,8 @@ fn test_data( let comms = TypeValue::List(List::new(vec![ElementTypeValue::Primitive( - routecore::bgp::communities::Community::from([ - 127, 12, 13, 12, - ]).into()), - ])); + Community::from([127, 12, 13, 12]).into(), + )])); // let my_comms_type = // TypeDef::List(Box::new(TypeDef::List(Box::new(TypeDef::Community)))); diff --git a/tests/end_to_end.rs b/tests/end_to_end.rs index 9d7aec86..3ce05109 100644 --- a/tests/end_to_end.rs +++ b/tests/end_to_end.rs @@ -1,7 +1,7 @@ use log::trace; + use roto::ast::AcceptReject; use roto::compiler::Compiler; - use roto::blocks::Scope::{self, Filter, FilterMap}; use roto::types::builtin::Asn; use roto::types::collections::{ElementTypeValue, List, Record}; @@ -10,6 +10,7 @@ use roto::types::typedef::TypeDef; use roto::types::typevalue::TypeValue; use roto::vm::{self, VmResult}; use rotonda_store::prelude::MergeUpdate; +use routecore::bgp::communities::HumanReadableCommunity as Community; mod common; @@ -74,10 +75,9 @@ fn test_data( trace!("ASN {:?}", asn); let comms_list = List::new(vec![ElementTypeValue::Primitive( - routecore::bgp::communities::Community::from([ + Community::from([ 127, 12, 13, 12, - ]) - .into(), + ]).into(), )]); trace!("comms list {}", comms_list); @@ -95,7 +95,7 @@ fn test_data( let comms = TypeValue::List(List::new(vec![ElementTypeValue::Primitive( - routecore::bgp::communities::Community::from([ + Community::from([ 127, 12, 13, 12, ]).into()), ])); @@ -187,7 +187,7 @@ fn test_filter_map_1() { let comms = TypeValue::List(List::new(vec![ElementTypeValue::Primitive( - routecore::bgp::communities::Community::from([ + Community::from([ 127, 12, 13, 12, ]).into()), ])); diff --git a/tests/my_message.rs b/tests/my_message.rs index 29cd97da..6cc9a43f 100644 --- a/tests/my_message.rs +++ b/tests/my_message.rs @@ -8,6 +8,8 @@ use roto::types::typedef::TypeDef; use roto::types::typevalue::TypeValue; use roto::vm::{self, VmResult}; +use routecore::bgp::communities::HumanReadableCommunity as Community; + mod common; fn test_data( @@ -40,7 +42,7 @@ fn test_data( let comms = TypeValue::List(List::new(vec![ElementTypeValue::Primitive( - routecore::bgp::communities::Community::from([ + Community::from([ 127, 12, 13, 12, ]) .into()) diff --git a/tests/string_conversions.rs b/tests/string_conversions.rs index eec1d931..81e08d3e 100644 --- a/tests/string_conversions.rs +++ b/tests/string_conversions.rs @@ -9,6 +9,8 @@ use roto::types::typedef::TypeDef; use roto::types::typevalue::TypeValue; use roto::vm::{self, VmResult}; +use routecore::bgp::communities::HumanReadableCommunity as Community; + mod common; fn src_code(format_line: &str) -> String { @@ -98,11 +100,11 @@ fn test_data( let comms = TypeValue::List(List::new(vec![ ElementTypeValue::Primitive( - routecore::bgp::communities::Community::from([ + Community::from([ 127, 12, 13, 12, ]).into()), ElementTypeValue::Primitive( - routecore::bgp::communities::Community::from([ + Community::from([ 127, 12, 13, 20, ]).into()) ])