diff --git a/examples/asn_in_out.rs b/examples/asn_in_out.rs index 34bbd4a8..246b3f76 100644 --- a/examples/asn_in_out.rs +++ b/examples/asn_in_out.rs @@ -1,11 +1,12 @@ use roto::compiler::Compiler; use roto::blocks::Scope::{self, FilterMap}; -use roto::types::builtin::Asn; use roto::types::collections::Record; use roto::types::typedef::TypeDef; use roto::vm; +use routecore::asn::Asn; + fn test_data( name: Scope, source_code: &'static str, diff --git a/examples/eval.rs b/examples/eval.rs index fe8fd836..7ff15c8c 100644 --- a/examples/eval.rs +++ b/examples/eval.rs @@ -1,8 +1,6 @@ use roto::compiler::compile::Compiler; -use roto::types::builtin::{ - self, Asn, BuiltinTypeValue, -}; +use roto::types::builtin::BuiltinTypeValue; use roto::types::collections::{ElementTypeValue, List, Record}; use roto::types::typedef::TypeDef; use roto::types::typevalue::TypeValue; @@ -10,6 +8,7 @@ use roto::vm; use roto::blocks::Scope::{self, FilterMap}; use routecore::bgp::communities::HumanReadableCommunity as Community; +use routecore::asn::Asn; fn test_data( name: Scope, @@ -70,11 +69,11 @@ fn test_data( ("next-hop", next_hop), ( "med", - builtin::BuiltinTypeValue::U32(80).into(), + BuiltinTypeValue::U32(80).into(), ), ( "local-pref", - builtin::BuiltinTypeValue::U32(20).into(), + BuiltinTypeValue::U32(20).into(), ), ("communities", comms), ], diff --git a/examples/types.rs b/examples/types.rs index 25d0bbbd..714f5024 100644 --- a/examples/types.rs +++ b/examples/types.rs @@ -1,11 +1,10 @@ -use roto::types::builtin::{ - Asn, BuiltinTypeValue, -}; +use roto::types::builtin::BuiltinTypeValue; use roto::types::collections::{ElementTypeValue, List, Record}; use roto::types::typedef::TypeDef; use roto::types::typevalue::TypeValue; use routecore::bgp::communities::HumanReadableCommunity as Community; +use routecore::asn::Asn; fn main() { // let count = RotoType::create_primitive_var( diff --git a/src/ast.rs b/src/ast.rs index 66652525..f4133ecd 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -32,9 +32,10 @@ use smallvec::SmallVec; use routecore::bgp::communities::HumanReadableCommunity as Community; use routecore::bgp::communities::{ExtendedCommunity, LargeCommunity, StandardCommunity}; +use routecore::asn::Asn; use crate::compiler::error::CompileError; -use crate::types::builtin::{Asn, Boolean}; +use crate::types::builtin::Boolean; use crate::types::typevalue::TypeValue; use crate::{first_into_compile_err, parse_string}; diff --git a/src/attr_change_set.rs b/src/attr_change_set.rs index 490cda8f..94a268ec 100644 --- a/src/attr_change_set.rs +++ b/src/attr_change_set.rs @@ -6,13 +6,14 @@ use routecore::bgp::message::nlri::PathId; use routecore::bgp::types::AfiSafi; use routecore::addr::Prefix; use routecore::bgp::communities::HumanReadableCommunity as Community; +use routecore::asn::Asn; use serde::Serialize; use std::marker::PhantomData; use std::ops::Index; use crate::ast::StringLiteral; use crate::types::builtin::{ - Asn, AtomicAggregate, BuiltinTypeValue, IpAddress, + AtomicAggregate, BuiltinTypeValue, IpAddress, LocalPref, MultiExitDisc, OriginType, RouteStatus, }; use crate::types::collections::ElementTypeValue; @@ -318,7 +319,7 @@ impl + std::fmt::Debug> VectorOption { .0 .try_into_asn() .map_err(|_| LongSegmentError)?; - vec![Asn(asn)] + vec![asn] } TypeValue::Builtin(BuiltinTypeValue::U32(int)) => { vec![Asn::from_u32(int)] diff --git a/src/types/builtin/builtin_type_value.rs b/src/types/builtin/builtin_type_value.rs index f7780d53..bdc29e0d 100644 --- a/src/types/builtin/builtin_type_value.rs +++ b/src/types/builtin/builtin_type_value.rs @@ -4,6 +4,7 @@ use std::fmt::Display; +use routecore::asn::Asn; use routecore::bgp::message::nlri::PathId; use routecore::bgp::types::{AfiSafi, NextHop}; use routecore::addr::Prefix; @@ -23,7 +24,7 @@ use super::super::typedef::TypeDef; use super::super::typevalue::TypeValue; use super::{ - Aggregator, Asn, AtomicAggregate, BgpUpdateMessage, Boolean, + Aggregator, AtomicAggregate, BgpUpdateMessage, Boolean, HexLiteral, Hop, IntegerLiteral, IpAddress, LocalPref, MultiExitDisc, OriginType, PrefixLength, RawRouteWithDeltas, RouteStatus, StringLiteral, diff --git a/src/types/builtin/primitives.rs b/src/types/builtin/primitives.rs index 9608ee55..31fa1c16 100644 --- a/src/types/builtin/primitives.rs +++ b/src/types/builtin/primitives.rs @@ -1,8 +1,9 @@ use std::fmt::{Display, Formatter}; use log::{debug, error, trace}; -use routecore::asn::LongSegmentError; +use routecore::asn::{LongSegmentError, Asn}; use routecore::bgp::aspath::OwnedHop; +use routecore::bgp::message::nlri::PathId; use serde::Serialize; use crate::ast::{IpAddressLiteral, PrefixLiteral}; @@ -110,8 +111,8 @@ impl RotoType for u16 { Ok(TypeValue::Builtin(BuiltinTypeValue::U32(self as u32))) } TypeDef::Asn => Ok(TypeValue::Builtin(BuiltinTypeValue::Asn( - Asn(routecore::asn::Asn::from(self as u32)), - ))), + routecore::asn::Asn::from(self as u32)), + )), TypeDef::PrefixLength => match self { 0..=128 => Ok(TypeValue::Builtin( BuiltinTypeValue::PrefixLength(PrefixLength(self as u8)), @@ -280,7 +281,7 @@ impl RotoType for u32 { Ok(TypeValue::Builtin(BuiltinTypeValue::U32(self))) } TypeDef::Asn => Ok(TypeValue::Builtin(BuiltinTypeValue::Asn( - Asn(routecore::asn::Asn::from(self)), + routecore::asn::Asn::from(self), ))), TypeDef::PrefixLength => match self { 0..=128 => Ok(TypeValue::Builtin( @@ -946,9 +947,9 @@ impl RotoType for IntegerLiteral { }, TypeDef::Asn => match self.0 { 0..=4294967295 => { - Ok(TypeValue::Builtin(BuiltinTypeValue::Asn(Asn( + Ok(TypeValue::Builtin(BuiltinTypeValue::Asn( routecore::asn::Asn::from(self.0 as u32), - )))) + ))) } i if i < 0 => Err(CompileError::from( "Cannot convert type \ @@ -1566,6 +1567,9 @@ impl RotoType for PrefixLength { TypeDef::U32 => { Ok(TypeValue::Builtin(BuiltinTypeValue::U32(self.0.into()))) } + TypeDef::IntegerLiteral => { + Ok(TypeValue::Builtin(BuiltinTypeValue::IntegerLiteral(IntegerLiteral(self.0 as i64)))) + } _ => Err(format!( "Cannot convert type PrefixLength to type {:?}", type_def @@ -1594,11 +1598,20 @@ impl RotoType for PrefixLength { } fn exec_type_method<'a>( - _method_token: usize, - _args: &[StackValue], + method_token: usize, + args: &[StackValue], _res_type: TypeDef, ) -> Result { - Err(VmError::InvalidMethodCall) + match method_token.try_into()? { + PrefixLengthToken::From => { + if let Some(StackValue::Ref(TypeValue::Builtin(BuiltinTypeValue::U32(val)))) = args.get(0) { + Ok(PrefixLength((*val).try_into().map_err(|_| VmError::InvalidConversion)?).into()) + } else { + Err(VmError::InvalidCommandArg) + } + } + _ => Err(VmError::InvalidMethodCall), + } } } @@ -1716,28 +1729,29 @@ impl RotoType for routecore::bgp::types::AfiSafi { fn exec_value_method<'a>( &'a self, - method_token: usize, - args: &'a [crate::vm::StackValue], - res_type: crate::types::typedef::TypeDef, + _method_token: usize, + _args: &'a [crate::vm::StackValue], + _res_type: crate::types::typedef::TypeDef, ) -> Result { - todo!() + Err(VmError::InvalidMethodCall) } fn exec_consume_value_method( self, - method_token: usize, - args: Vec, - res_type: crate::types::typedef::TypeDef, + _method_token: usize, + _args: Vec, + _res_type: crate::types::typedef::TypeDef, ) -> Result { - todo!() + Err(VmError::InvalidMethodCall) } fn exec_type_method( - method_token: usize, - args: &[crate::vm::StackValue], - res_type: crate::types::typedef::TypeDef, + _method_token: usize, + _args: &[crate::vm::StackValue], + _res_type: crate::types::typedef::TypeDef, ) -> Result { - todo!() + Err(VmError::InvalidMethodCall) + } } @@ -1753,23 +1767,49 @@ impl ScalarValue for routecore::bgp::message::nlri::PathId {} impl RotoType for routecore::bgp::message::nlri::PathId { fn get_props_for_method( - ty: TypeDef, + _ty: TypeDef, method_name: &crate::ast::Identifier, ) -> Result where Self: std::marker::Sized, { - todo!() + match method_name.ident.as_str() { + "from" => Ok(MethodProps::new( + TypeDef::PathId, + PathIdToken::From.into(), + vec![TypeDef::U32], + )), + _ => Err(format!( + "Unknown method: '{}' for type PrefixLength", + method_name.ident + ) + .into()), + } + } fn into_type( self, - type_value: &TypeDef, - ) -> Result + type_value: &crate::types::typedef::TypeDef, + ) -> Result where Self: std::marker::Sized, { - todo!() + match type_value { + TypeDef::PathId => Ok(TypeValue::Builtin(BuiltinTypeValue::PathId(self))), + TypeDef::IntegerLiteral => { + Ok(TypeValue::Builtin(BuiltinTypeValue::IntegerLiteral(IntegerLiteral(::from(self) as i64)))) + } + TypeDef::StringLiteral => Ok(TypeValue::Builtin( + BuiltinTypeValue::StringLiteral(self.into()), + )), + TypeDef::U32 => Ok(TypeValue::Builtin(BuiltinTypeValue::U32(self.into()))), + _ => Err(format!( + "Cannot convert type PrefixLength to type {:?}", + type_value + ) + .into()), + } } fn exec_value_method<'a>( @@ -1778,27 +1818,66 @@ impl RotoType for routecore::bgp::message::nlri::PathId { args: &'a [StackValue], res_type: TypeDef, ) -> Result { - todo!() + Err(VmError::InvalidMethodCall) + } + + fn exec_consume_value_method( self, method_token: usize, args: Vec, res_type: TypeDef, ) -> Result { - todo!() + Err(VmError::InvalidMethodCall) + } - fn exec_type_method( + fn exec_type_method<'a>( method_token: usize, args: &[StackValue], - res_type: TypeDef, + _res_type: TypeDef, ) -> Result { - todo!() + match method_token.try_into()? { + PathIdToken::From => { + if let Some(StackValue::Owned(TypeValue::Builtin(BuiltinTypeValue::U32(val)))) = args.get(0) { + Ok(PathId::from_u32(*val).into()) + } else { + Err(VmError::StackUnderflow) + } + } + _ => Err(VmError::InvalidMethodCall), + } + } +} + +#[derive(Debug)] +pub(crate) enum PathIdToken { + From, +} + +impl TryFrom for PathIdToken { + type Error = VmError; + + fn try_from(val: usize) -> Result { + match val { + 0 => Ok(PathIdToken::From), + _ => { + debug!("Unknown token value: {}", val); + Err(VmError::InvalidMethodCall) + } + } + } +} + +impl From for usize { + fn from(val: PathIdToken) -> Self { + val as usize } } + // ----------- Community ----------------------------------------------------- /// A BGP community. @@ -2645,24 +2724,24 @@ impl From for usize { // ----------- Asn type ----------------------------------------------------- -#[derive(Debug, Eq, Copy, Clone, Hash, PartialEq, Serialize)] -pub struct Asn(pub(crate) routecore::asn::Asn); +// #[derive(Debug, Eq, Copy, Clone, Hash, PartialEq, Serialize)] +// pub struct Asn(pub(crate) routecore::asn::Asn); -impl Asn { - pub fn new(asn: routecore::asn::Asn) -> Self { - Asn(asn) - } +// impl Asn { +// pub fn new(asn: routecore::asn::Asn) -> Self { +// Asn(asn) +// } - pub fn from_u32(asn: u32) -> Self { - Asn(routecore::asn::Asn::from(asn)) - } +// pub fn from_u32(asn: u32) -> Self { +// Asn(routecore::asn::Asn::from(asn)) +// } - pub fn get_asn(&self) -> routecore::asn::Asn { - self.0 - } -} +// pub fn get_asn(&self) -> routecore::asn::Asn { +// self.0 +// } +// } -impl RotoType for Asn { +impl RotoType for routecore::asn::Asn { fn get_props_for_method( _ty: TypeDef, method_name: &crate::ast::Identifier, @@ -2696,11 +2775,11 @@ impl RotoType for Asn { TypeDef::StringLiteral => Ok(TypeValue::Builtin( BuiltinTypeValue::StringLiteral(self.into()), )), - TypeDef::U8 => match self.0.into_u32() { + TypeDef::U8 => match self.into_u32() { 0..=255 => { // this should work, shouldn't it? Ok(TypeValue::Builtin(BuiltinTypeValue::U8( - self.0.into_u32() as u8, + self.into_u32() as u8, ))) } val => Err(format!( @@ -2711,7 +2790,7 @@ impl RotoType for Asn { .into()), }, TypeDef::U32 => Ok(TypeValue::Builtin(BuiltinTypeValue::U32( - self.0.into_u32(), + self.into_u32(), ))), _ => { Err(format!("Cannot convert type Asn to type {:?}", type_def) @@ -2731,7 +2810,7 @@ impl RotoType for Asn { if let TypeValue::Builtin(BuiltinTypeValue::Asn(asn)) = args.get(0).ok_or(VmError::InvalidMethodCall)?.as_ref() { - Ok(TypeValue::from(Asn::new(asn.0))) + Ok(TypeValue::from(*asn)) } else { Err(VmError::AnonymousArgumentNotFound) } @@ -2763,29 +2842,29 @@ impl From for TypeValue { } } -impl From for BuiltinTypeValue { - fn from(value: routecore::asn::Asn) -> Self { - BuiltinTypeValue::Asn(Asn(value)) - } -} +// impl From for BuiltinTypeValue { +// fn from(value: routecore::asn::Asn) -> Self { +// BuiltinTypeValue::Asn(value) +// } +// } -impl From for Asn { - fn from(value: u32) -> Self { - Asn(value.into()) - } -} +// impl From for Asn { +// fn from(value: u32) -> Self { +// value.into() +// } +// } -impl From for Asn { - fn from(value: u16) -> Self { - Asn((value as u32).into()) - } -} +// impl From for Asn { +// fn from(value: u16) -> Self { +// (value as u32).into() +// } +// } -impl Display for Asn { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.0) - } -} +// impl Display for Asn { +// fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { +// write!(f, "{}", self.0) +// } +// } #[derive(Debug)] pub enum AsnToken { @@ -2938,16 +3017,16 @@ impl RotoType for routecore::bgp::aspath::HopPath { match method.try_into()? { AsPathToken::Origin => match self.origin().cloned() { Some(origin_asn) => Ok(TypeValue::Builtin( - BuiltinTypeValue::Asn(Asn(origin_asn + BuiltinTypeValue::Asn(origin_asn .try_into_asn() .map_err(|_| VmError::InvalidValueType)?)), - )), + ), None => Err(VmError::InvalidPayload), }, AsPathToken::Contains => { - if let TypeValue::Builtin(BuiltinTypeValue::Asn(Asn( + if let TypeValue::Builtin(BuiltinTypeValue::Asn( search_asn, - ))) = first_into_vm_err!(args, InvalidMethodCall)?.as_ref() + )) = first_into_vm_err!(args, InvalidMethodCall)?.as_ref() { let contains = self.contains(&RoutecoreHop::from(*search_asn)); @@ -2985,7 +3064,7 @@ impl RotoType for routecore::bgp::aspath::HopPath { impl VectorValue for routecore::bgp::aspath::HopPath { type ReadItem = routecore::bgp::aspath::OwnedHop; - type WriteItem = Asn; + type WriteItem = routecore::asn::Asn; fn prepend_vec( &mut self, @@ -2993,7 +3072,7 @@ impl VectorValue for routecore::bgp::aspath::HopPath { ) -> Result<(), LongSegmentError> { let mut as_path = vector .iter() - .map(|a| routecore::bgp::aspath::OwnedHop::Asn(a.0)) + .map(|a| routecore::bgp::aspath::OwnedHop::Asn(*a)) .collect::>(); as_path.extend_from_slice(std::mem::take(self).iter().as_slice()); @@ -3007,7 +3086,7 @@ impl VectorValue for routecore::bgp::aspath::HopPath { vector: Vec, ) -> Result<(), LongSegmentError> { for asn in vector { - self.append(OwnedHop::Asn(asn.0)); + self.append(OwnedHop::Asn(asn)); } Ok(()) @@ -3027,7 +3106,7 @@ impl VectorValue for routecore::bgp::aspath::HopPath { left_path.extend_from_slice( vector .into_iter() - .map(|a| a.0.into()) + .map(|a| a.into()) .collect::>() .as_slice(), ); @@ -3162,11 +3241,11 @@ impl Display for Hop { } } -impl From for Hop { - fn from(value: Asn) -> Self { - Hop(value.0.into()) - } -} +// impl From for Hop { +// fn from(value: Asn) -> Self { +// Hop(value.0.into()) +// } +// } //------------ OriginType type ---------------------------------------------- diff --git a/src/types/builtin/route.rs b/src/types/builtin/route.rs index 28d954b3..90798f5f 100644 --- a/src/types/builtin/route.rs +++ b/src/types/builtin/route.rs @@ -13,6 +13,7 @@ use log::{debug, error}; use routecore::addr::Prefix; +use routecore::asn::Asn; use routecore::bgp::{ aspath::HopPath, communities::HumanReadableCommunity as Community, @@ -40,7 +41,7 @@ use crate::{ vm::{StackValue, VmError}, }; -use super::{Asn, BuiltinTypeValue, IpAddress, OriginType, RouteStatus}; +use super::{BuiltinTypeValue, IpAddress, OriginType, RouteStatus}; use crate::attr_change_set::{ AttrChangeSet, ScalarOption, ScalarValue, VectorOption, VectorValue, }; @@ -228,7 +229,7 @@ impl RawRouteWithDeltas { afi_safi: self.afi_safi, path_id: self.path_id, peer_ip: self.peer_ip, - peer_asn: Some(Asn::new(peer_asn)), + peer_asn: Some(peer_asn), router_id: self.router_id, attribute_deltas: self.attribute_deltas, status_deltas: self.status_deltas, @@ -254,7 +255,7 @@ impl RawRouteWithDeltas { } pub fn peer_asn(&self) -> Option { - self.peer_asn.map(|asn| asn.0) + self.peer_asn } pub fn router_id(&self) -> Option> { @@ -551,7 +552,7 @@ impl RawRouteWithDeltas { .raw_message .raw_message .0 - .all_communities::() + .all_human_readable_communities() .ok() .flatten() .map(TypeValue::from) @@ -1396,7 +1397,7 @@ impl UpdateMessage { ), communities: VectorOption::from( self.0 - .all_communities::() + .all_human_readable_communities() .map_err(VmError::from)?, ), peer_ip: peer_ip.into(), diff --git a/src/types/builtin/tests.rs b/src/types/builtin/tests.rs index 2e315257..7f2a03c4 100644 --- a/src/types/builtin/tests.rs +++ b/src/types/builtin/tests.rs @@ -1,7 +1,7 @@ #[cfg(test)] mod route { use super::super::{ - Asn, Boolean, IntegerLiteral, PrefixLength, StringLiteral, + Boolean, IntegerLiteral, PrefixLength, StringLiteral, }; use crate::ast::IpAddressLiteral; use crate::types::builtin::{BuiltinTypeValue, IpAddress}; @@ -17,6 +17,7 @@ mod route { _Consume, } use routecore::bgp::aspath::HopPath; + use routecore::asn::{Asn, Asn16}; use routecore::bgp::communities::{HumanReadableCommunity as Community, StandardCommunity, Tag, ExtendedCommunity, LargeCommunity}; use routecore::bgp::{ message::{ @@ -126,7 +127,7 @@ mod route { } let res = delta.attributes.as_path.prepend( - crate::types::builtin::primitives::Asn::from(211321_u32), + Asn::from(211321_u32), ); assert!(res.is_ok()); @@ -152,7 +153,7 @@ mod route { attr_set.as_path.as_routecore_hops_vec().get(0), Asn::try_from(211321_u32) .ok() - .map(move |a| routecore::bgp::aspath::Hop::from(a.0)) + .map(routecore::bgp::aspath::Hop::from) .as_ref() .as_ref() ); @@ -160,7 +161,7 @@ mod route { attr_set.as_path.as_routecore_hops_vec().get(1), Asn::try_from(200_u32) .ok() - .map(move |a| routecore::bgp::aspath::Hop::from(a.0)) + .map(routecore::bgp::aspath::Hop::from) .as_ref() .as_ref() ); @@ -266,14 +267,14 @@ mod route { assert_eq!( *attr_set.as_path.as_routecore_hops_vec()[0], routecore::bgp::aspath::Hop::from( - Asn::try_from(211321_u32).unwrap().0 + Asn::try_from(211321_u32).unwrap() ) ); assert_eq!( attr_set.as_path.as_routecore_hops_vec().get(1), - Asn::try_from(200_u16) + Asn::try_from(200_u32) .ok() - .map(move |a| routecore::bgp::aspath::Hop::from(a.0)) + .map(routecore::bgp::aspath::Hop::from) .as_ref() .as_ref() ); @@ -292,7 +293,7 @@ mod route { attr_set.as_path.as_routecore_hops_vec().get(0), Asn::try_from(211322_u32) .ok() - .map(move |a| routecore::bgp::aspath::Hop::from(a.0)) + .map(routecore::bgp::aspath::Hop::from) .as_ref() .as_ref() ); @@ -300,15 +301,15 @@ mod route { attr_set.as_path.as_routecore_hops_vec().get(1), Asn::try_from(211321_u32) .ok() - .map(move |a| routecore::bgp::aspath::Hop::from(a.0)) + .map(routecore::bgp::aspath::Hop::from) .as_ref() .as_ref() ); assert_eq!( attr_set.as_path.as_routecore_hops_vec().get(2), - Asn::try_from(200_u32) + routecore::asn::Asn::try_from(200_u32) .ok() - .map(move |a| routecore::bgp::aspath::Hop::from(a.0)) + .map(routecore::bgp::aspath::Hop::from) .as_ref() .as_ref() ); @@ -532,7 +533,7 @@ mod route { init(); let test_value = 0_u8; - let arg: HopPath = vec![routecore::asn::Asn::from(24)].into(); + let arg: HopPath = vec![Asn::from(24)].into(); test_consume_method_on_type_value(test_value, "set", arg).unwrap(); } @@ -576,7 +577,7 @@ mod route { #[should_panic = "Cannot convert type U8 to type AsPath"] fn test_u8_conversion_as_path() { let test_value = 24_u8; - let res: HopPath = vec![routecore::asn::Asn::from(24)].into(); + let res: HopPath = vec![Asn::from(24)].into(); mk_converted_type_value(test_value, res).unwrap(); } @@ -637,7 +638,7 @@ than 128 into type PrefixLength"] #[test] fn test_conversion_asn_literal_u16() -> Result<(), CompileError> { let test_value = IntegerLiteral::new(32768); - let res = Asn::from(32768_u16); + let res = Asn::from(32768_u32); mk_converted_type_value(test_value, res) } @@ -689,7 +690,7 @@ greater than 128 into type PrefixLength"] #[test] fn test_conversion_asn_u32() -> Result<(), CompileError> { let test_value = 32768_u32; - let res = Asn::new(32768.into()); + let res = Asn::from(32768); mk_converted_type_value(test_value, res) } @@ -888,7 +889,7 @@ src_ty.clone().test_type_conversion(arg_ty)"] #[test] fn test_integer_literal_1() -> Result<(), CompileError> { let test_value = IntegerLiteral::new(100); - let res = Asn::new(100.into()); + let res = Asn::from(100); mk_converted_type_value(test_value, res) } @@ -920,6 +921,27 @@ src_ty.clone().test_type_conversion(arg_ty)"] test_value.into_type(&TypeDef::U8).unwrap(); } + //-------- Test: PrefixLengthLiteral ------------------------------------- + + #[test] + fn test_prefix_length_literal_1() { + let test_value: PrefixLength = PrefixLength(23); + let res = IntegerLiteral::new(23); + + mk_converted_type_value(test_value, res).unwrap(); + } + + #[test] + #[ignore] + fn test_prefix_length_literal_2() { + let test_value: PrefixLength = PrefixLength(23); + let res = IntegerLiteral::new(23); + + test_method_on_type_value_with_multiple_args(test_value, MethodType::Type, "from", &[23_u32], res).unwrap(); + } + + + //-------- Test: Communities --------------------------------------------- #[test] @@ -934,7 +956,7 @@ src_ty.clone().test_type_conversion(arg_ty)"] fn test_standard_community_2() { let test_value: Community = StandardCommunity::new( - routecore::asn::Asn16::from(12500), + Asn16::from(12500), Tag::new(7890), ).into(); let res = StringLiteral::new("AS12500:7890".to_string()); @@ -948,12 +970,12 @@ src_ty.clone().test_type_conversion(arg_ty)"] let test_value: Community = StandardCommunity::new( - routecore::asn::Asn16::from(12500), + Asn16::from(12500), Tag::new(7890), ).into(); let res: Community = StandardCommunity::new( - routecore::asn::Asn16::from(7500), + Asn16::from(7500), Tag::new(3000), ).into(); @@ -970,7 +992,7 @@ src_ty.clone().test_type_conversion(arg_ty)"] ).unwrap().into(); let res: Community = StandardCommunity::new( - routecore::asn::Asn16::from(7500), + Asn16::from(7500), Tag::new(3000), ).into(); @@ -987,7 +1009,7 @@ src_ty.clone().test_type_conversion(arg_ty)"] ).unwrap().into(); let res: Community = StandardCommunity::new( - routecore::asn::Asn16::from(7500), + Asn16::from(7500), Tag::new(3000), ).into(); @@ -1004,7 +1026,7 @@ src_ty.clone().test_type_conversion(arg_ty)"] ).unwrap().into(); let res: Community = StandardCommunity::new( - routecore::asn::Asn16::from(7500), + Asn16::from(7500), Tag::new(3000), ).into(); diff --git a/src/types/typedef.rs b/src/types/typedef.rs index 0c6312f0..1437c447 100644 --- a/src/types/typedef.rs +++ b/src/types/typedef.rs @@ -5,6 +5,7 @@ use std::hash::{Hash, Hasher}; // These are all the types the user can create. This enum is used to create // `user defined` types. use log::{trace, debug}; +use routecore::asn::Asn; use routecore::bgp::aspath::HopPath; use serde::Serialize; use routecore::bgp::{types::AfiSafi, message::nlri::PathId}; @@ -23,7 +24,7 @@ use crate::{ }; use super::builtin::{ - Asn, AtomicAggregate, Aggregator, Boolean, HexLiteral, Hop, + AtomicAggregate, Aggregator, Boolean, HexLiteral, Hop, IntegerLiteral, IpAddress, LocalPref, MultiExitDisc, OriginType, PrefixLength, RawRouteWithDeltas, RouteStatus, StringLiteral, Unknown, @@ -237,7 +238,7 @@ impl TypeDef { IntegerLiteral(StringLiteral,U8,U32,StringLiteral,PrefixLength,LocalPref,Asn;ConstEnumVariant), StringLiteral(Asn;), HexLiteral(StringLiteral,U8,U32,Community;), - PrefixLength(StringLiteral,U8,U32;), + PrefixLength(StringLiteral,IntegerLiteral,U8,U32;), AfiSafi(StringLiteral;), PathId(StringLiteral,IntegerLiteral,U32;), Asn(StringLiteral,U32;), diff --git a/src/types/typevalue.rs b/src/types/typevalue.rs index b8f0f199..af0d668a 100644 --- a/src/types/typevalue.rs +++ b/src/types/typevalue.rs @@ -6,6 +6,7 @@ use primitives::{ RouteStatus, }; +use routecore::asn::Asn; use routecore::bgp::aspath::HopPath; use routecore::bgp::message::nlri::PathId; use routecore::bgp::types::{AfiSafi, NextHop}; @@ -26,7 +27,7 @@ use crate::{ use super::{ builtin::{ - primitives, Asn, BgpUpdateMessage, Boolean, BuiltinTypeValue, + primitives, BgpUpdateMessage, Boolean, BuiltinTypeValue, HexLiteral, IntegerLiteral, IpAddress, PrefixLength, RawRouteWithDeltas, StringLiteral, }, @@ -1078,8 +1079,8 @@ impl PartialOrd for TypeValue { TypeValue::Builtin(BuiltinTypeValue::IpAddress(IpAddress(v))), ) => Some(u.cmp(v)), ( - TypeValue::Builtin(BuiltinTypeValue::Asn(Asn(u))), - TypeValue::Builtin(BuiltinTypeValue::Asn(Asn(v))), + TypeValue::Builtin(BuiltinTypeValue::Asn(u)), + TypeValue::Builtin(BuiltinTypeValue::Asn(v)), ) => Some(u.cmp(v)), ( TypeValue::Builtin(BuiltinTypeValue::AsPath(_)), @@ -1248,11 +1249,11 @@ impl From for TypeValue { } } -impl From for TypeValue { - fn from(value: routecore::asn::Asn) -> Self { - TypeValue::Builtin(BuiltinTypeValue::Asn(Asn(value))) - } -} +// impl From for TypeValue { +// fn from(value: routecore::asn::Asn) -> Self { +// TypeValue::Builtin(BuiltinTypeValue::Asn(Asn(value))) +// } +// } impl From for TypeValue { fn from(value: routecore::bgp::aspath::HopPath) -> Self { @@ -1290,7 +1291,7 @@ impl From>> for BuiltinTypeValue { impl From> for TypeValue { fn from(as_path: Vec) -> Self { let as_path: Vec>> = - as_path.iter().map(|p| p.0.into()).collect(); + as_path.iter().map(|p| (*p).into()).collect(); let as_path = routecore::bgp::aspath::HopPath::from(as_path); TypeValue::Builtin(BuiltinTypeValue::AsPath(as_path)) } @@ -1370,7 +1371,7 @@ impl From for TypeValue { impl From for TypeValue { fn from(value: crate::ast::AsnLiteral) -> Self { - TypeValue::Builtin(BuiltinTypeValue::Asn(Asn(value.0.into()))) + TypeValue::Builtin(BuiltinTypeValue::Asn(value.0.into())) } } diff --git a/tests/accept_reject_simple.rs b/tests/accept_reject_simple.rs index 21ec0fab..2903eebb 100644 --- a/tests/accept_reject_simple.rs +++ b/tests/accept_reject_simple.rs @@ -3,11 +3,12 @@ use roto::ast::AcceptReject; use roto::compiler::Compiler; use roto::blocks::Scope::{self, FilterMap}; -use roto::types::builtin::Asn; use roto::types::collections::Record; use roto::types::typedef::TypeDef; use roto::vm::{self, VmResult}; +use routecore::asn::Asn; + mod common; fn test_data( diff --git a/tests/compare.rs b/tests/compare.rs index bdf4a5b5..cfd45156 100644 --- a/tests/compare.rs +++ b/tests/compare.rs @@ -2,13 +2,14 @@ use roto::ast::AcceptReject; use roto::compiler::Compiler; use roto::blocks::Scope::{self, FilterMap}; -use roto::types::builtin::Asn; use roto::types::collections::Record; use roto::types::typedef::TypeDef; use roto::types::typevalue::TypeValue; use roto::vm::{self, VmResult}; use rotonda_store::prelude::MergeUpdate; +use routecore::asn::Asn; + mod common; #[derive(Debug, Clone)] diff --git a/tests/data_sources.rs b/tests/data_sources.rs index 1a644259..173051ae 100644 --- a/tests/data_sources.rs +++ b/tests/data_sources.rs @@ -2,13 +2,13 @@ use log::trace; use roto::compiler::Compiler; use roto::blocks::Scope; -use roto::types::builtin::Asn; use roto::types::collections::{ElementTypeValue, List, Record}; use roto::types::datasources::DataSource; use roto::types::typedef::TypeDef; use roto::types::typevalue::TypeValue; use roto::vm; +use routecore::asn::Asn; use routecore::bgp::communities::HumanReadableCommunity as Community; mod common; diff --git a/tests/end_to_end.rs b/tests/end_to_end.rs index 3ce05109..ba6ad6c4 100644 --- a/tests/end_to_end.rs +++ b/tests/end_to_end.rs @@ -3,7 +3,6 @@ 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}; use roto::types::datasources::{DataSource, Rib}; use roto::types::typedef::TypeDef; @@ -11,6 +10,7 @@ use roto::types::typevalue::TypeValue; use roto::vm::{self, VmResult}; use rotonda_store::prelude::MergeUpdate; use routecore::bgp::communities::HumanReadableCommunity as Community; +use routecore::asn::Asn; mod common; diff --git a/tests/eq_conversions.rs b/tests/eq_conversions.rs index 2420143c..97e74aa7 100644 --- a/tests/eq_conversions.rs +++ b/tests/eq_conversions.rs @@ -1,14 +1,15 @@ use roto::ast::AcceptReject; use roto::compiler::Compiler; - use roto::blocks::Scope::{self, FilterMap}; -use roto::types::builtin::Asn; use roto::types::collections::Record; use roto::types::typedef::TypeDef; use roto::types::typevalue::TypeValue; use roto::vm::{self, VmResult}; + use rotonda_store::prelude::MergeUpdate; +use routecore::asn::Asn; + mod common; #[derive(Debug, Clone)] diff --git a/tests/lists.rs b/tests/lists.rs index e19f07dc..712024fe 100644 --- a/tests/lists.rs +++ b/tests/lists.rs @@ -3,13 +3,15 @@ use roto::ast::AcceptReject; use roto::compiler::Compiler; use roto::blocks::Scope::{self, FilterMap}; -use roto::types::builtin::Asn; use roto::types::collections::Record; use roto::types::typedef::TypeDef; use roto::types::typevalue::TypeValue; use roto::vm::{self, VmResult}; + use rotonda_store::prelude::MergeUpdate; +use routecore::asn::Asn; + mod common; #[derive(Debug, Clone)] diff --git a/tests/my_message.rs b/tests/my_message.rs index 6cc9a43f..12811be3 100644 --- a/tests/my_message.rs +++ b/tests/my_message.rs @@ -2,13 +2,13 @@ use log::trace; use roto::compiler::Compiler; use roto::blocks::Scope; -use roto::types::builtin::Asn; use roto::types::collections::{ElementTypeValue, List, Record}; use roto::types::typedef::TypeDef; use roto::types::typevalue::TypeValue; use roto::vm::{self, VmResult}; use routecore::bgp::communities::HumanReadableCommunity as Community; +use routecore::asn::Asn; mod common; diff --git a/tests/records.rs b/tests/records.rs index 836ad0c1..04b33ead 100644 --- a/tests/records.rs +++ b/tests/records.rs @@ -3,13 +3,15 @@ use roto::ast::AcceptReject; use roto::compiler::Compiler; use roto::blocks::Scope::{self, FilterMap}; -use roto::types::builtin::Asn; use roto::types::collections::Record; use roto::types::typedef::TypeDef; use roto::types::typevalue::TypeValue; use roto::vm::{self, VmResult}; + use rotonda_store::prelude::MergeUpdate; +use routecore::asn::Asn; + mod common; #[derive(Debug, Clone)] diff --git a/tests/string_conversions.rs b/tests/string_conversions.rs index 81e08d3e..51574705 100644 --- a/tests/string_conversions.rs +++ b/tests/string_conversions.rs @@ -2,7 +2,7 @@ use log::trace; use roto::compiler::Compiler; use roto::blocks::Scope; -use roto::types::builtin::{Asn, BuiltinTypeValue}; +use roto::types::builtin::BuiltinTypeValue; use roto::types::collections::{ElementTypeValue, List, Record}; use roto::types::enum_types::EnumVariant; use roto::types::typedef::TypeDef; @@ -10,6 +10,7 @@ use roto::types::typevalue::TypeValue; use roto::vm::{self, VmResult}; use routecore::bgp::communities::HumanReadableCommunity as Community; +use routecore::asn::Asn; mod common;