mirror of
https://github.com/cubixle/radius-rs.git
synced 2026-04-30 20:18:46 +01:00
Make code-generator generatable the code for Date type
This commit is contained in:
@@ -45,6 +45,7 @@ enum RadiusAttributeValueType {
|
|||||||
TunnelPassword,
|
TunnelPassword,
|
||||||
Octets,
|
Octets,
|
||||||
IpAddr,
|
IpAddr,
|
||||||
|
Date,
|
||||||
Integer,
|
Integer,
|
||||||
VSA,
|
VSA,
|
||||||
}
|
}
|
||||||
@@ -56,6 +57,7 @@ impl FromStr for RadiusAttributeValueType {
|
|||||||
"string" => Ok(RadiusAttributeValueType::String),
|
"string" => Ok(RadiusAttributeValueType::String),
|
||||||
"octets" => Ok(RadiusAttributeValueType::Octets),
|
"octets" => Ok(RadiusAttributeValueType::Octets),
|
||||||
"ipaddr" => Ok(RadiusAttributeValueType::IpAddr),
|
"ipaddr" => Ok(RadiusAttributeValueType::IpAddr),
|
||||||
|
"date" => Ok(RadiusAttributeValueType::Date),
|
||||||
"integer" => Ok(RadiusAttributeValueType::Integer),
|
"integer" => Ok(RadiusAttributeValueType::Integer),
|
||||||
"vsa" => Ok(RadiusAttributeValueType::VSA),
|
"vsa" => Ok(RadiusAttributeValueType::VSA),
|
||||||
_ => Err(()),
|
_ => Err(()),
|
||||||
@@ -115,6 +117,8 @@ fn generate_header(w: &mut BufWriter<File>) {
|
|||||||
|
|
||||||
use std::net::Ipv4Addr;
|
use std::net::Ipv4Addr;
|
||||||
|
|
||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
|
||||||
use crate::avp::{AVP, AVPType, AVPError};
|
use crate::avp::{AVP, AVPType, AVPError};
|
||||||
use crate::packet::Packet;
|
use crate::packet::Packet;
|
||||||
use crate::tag::Tag;
|
use crate::tag::Tag;
|
||||||
@@ -187,23 +191,27 @@ fn generate_attribute_code(
|
|||||||
false => generate_string_attribute_code(w, &method_identifier, &type_identifier),
|
false => generate_string_attribute_code(w, &method_identifier, &type_identifier),
|
||||||
},
|
},
|
||||||
RadiusAttributeValueType::UserPassword => match attr.has_tag {
|
RadiusAttributeValueType::UserPassword => match attr.has_tag {
|
||||||
true => unimplemented!(),
|
true => unimplemented!("tagged-user-password"),
|
||||||
false => generate_user_password_attribute_code(w, &method_identifier, &type_identifier),
|
false => generate_user_password_attribute_code(w, &method_identifier, &type_identifier),
|
||||||
},
|
},
|
||||||
RadiusAttributeValueType::TunnelPassword => match attr.has_tag {
|
RadiusAttributeValueType::TunnelPassword => match attr.has_tag {
|
||||||
true => {
|
true => {
|
||||||
generate_tunnel_password_attribute_code(w, &method_identifier, &type_identifier)
|
generate_tunnel_password_attribute_code(w, &method_identifier, &type_identifier)
|
||||||
}
|
}
|
||||||
false => unimplemented!(),
|
false => unimplemented!("tunnel-password"),
|
||||||
},
|
},
|
||||||
RadiusAttributeValueType::Octets => match attr.has_tag {
|
RadiusAttributeValueType::Octets => match attr.has_tag {
|
||||||
true => unimplemented!(),
|
true => unimplemented!("tagged-octets"),
|
||||||
false => generate_octets_attribute_code(w, &method_identifier, &type_identifier),
|
false => generate_octets_attribute_code(w, &method_identifier, &type_identifier),
|
||||||
},
|
},
|
||||||
RadiusAttributeValueType::IpAddr => match attr.has_tag {
|
RadiusAttributeValueType::IpAddr => match attr.has_tag {
|
||||||
true => unimplemented!(),
|
true => unimplemented!("tagged-ip-addr"),
|
||||||
false => generate_ipaddr_attribute_code(w, &method_identifier, &type_identifier),
|
false => generate_ipaddr_attribute_code(w, &method_identifier, &type_identifier),
|
||||||
},
|
},
|
||||||
|
RadiusAttributeValueType::Date => match attr.has_tag {
|
||||||
|
true => unimplemented!("tagged-date"),
|
||||||
|
false => generate_date_attribute_code(w, &method_identifier, &type_identifier),
|
||||||
|
},
|
||||||
RadiusAttributeValueType::Integer => {
|
RadiusAttributeValueType::Integer => {
|
||||||
match value_defined_attributes_set.contains(&attr_name) {
|
match value_defined_attributes_set.contains(&attr_name) {
|
||||||
true => match attr.has_tag {
|
true => match attr.has_tag {
|
||||||
@@ -414,6 +422,32 @@ pub fn lookup_all_{method_identifier}(packet: &Packet) -> Result<Vec<Ipv4Addr>,
|
|||||||
w.write_all(code.as_bytes()).unwrap();
|
w.write_all(code.as_bytes()).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn generate_date_attribute_code(
|
||||||
|
w: &mut BufWriter<File>,
|
||||||
|
method_identifier: &str,
|
||||||
|
type_identifier: &str,
|
||||||
|
) {
|
||||||
|
let code = format!(
|
||||||
|
"pub fn add_{method_identifier}(packet: &mut Packet, value: &DateTime<Utc>) {{
|
||||||
|
packet.add(AVP::from_date({type_identifier}, value));
|
||||||
|
}}
|
||||||
|
pub fn lookup_{method_identifier}(packet: &Packet) -> Option<Result<DateTime<Utc>, AVPError>> {{
|
||||||
|
packet.lookup({type_identifier}).map(|v| v.encode_date())
|
||||||
|
}}
|
||||||
|
pub fn lookup_all_{method_identifier}(packet: &Packet) -> Result<Vec<DateTime<Utc>>, AVPError> {{
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
for avp in packet.lookup_all({type_identifier}) {{
|
||||||
|
vec.push(avp.encode_date()?)
|
||||||
|
}}
|
||||||
|
Ok(vec)
|
||||||
|
}}
|
||||||
|
",
|
||||||
|
method_identifier = method_identifier,
|
||||||
|
type_identifier = type_identifier,
|
||||||
|
);
|
||||||
|
w.write_all(code.as_bytes()).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
fn generate_integer_attribute_code(
|
fn generate_integer_attribute_code(
|
||||||
w: &mut BufWriter<File>,
|
w: &mut BufWriter<File>,
|
||||||
method_identifier: &str,
|
method_identifier: &str,
|
||||||
|
|||||||
Reference in New Issue
Block a user