MCPcopy Create free account
hub / github.com/PowerShell/DSC / invoke

Method invoke

lib/dsc-lib/src/functions/cidr_subnet.rs:35–104  ·  view source on GitHub ↗
(&self, args: &[Value], _context: &Context)

Source from the content-addressed store, hash-verified

33 }
34
35 fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> {
36 debug!("{}", t!("functions.cidrSubnet.invoked"));
37
38 let cidr_string = args[0].as_str().unwrap();
39 let new_cidr = args[1].as_i64().unwrap() as u8;
40 let subnet_index = args[2].as_i64().unwrap();
41
42 if subnet_index < 0 {
43 return Err(DscError::FunctionArg(
44 "cidrSubnet".to_string(),
45 t!("functions.cidrSubnet.negativeSubnetIndex").to_string(),
46 ));
47 }
48
49 let network = cidr_string.parse::<IpNetwork>().map_err(|_| {
50 DscError::FunctionArg(
51 "cidrSubnet".to_string(),
52 t!("functions.cidrSubnet.invalidCidr", cidr = cidr_string).to_string(),
53 )
54 })?;
55
56 let result = match network {
57 IpNetwork::V4(net) => {
58 if new_cidr > 32 {
59 return Err(DscError::FunctionArg(
60 "cidrSubnet".to_string(),
61 t!("functions.cidrSubnet.invalidPrefixV4", prefix = new_cidr).to_string(),
62 ));
63 }
64
65 if new_cidr < net.prefix() {
66 return Err(DscError::FunctionArg(
67 "cidrSubnet".to_string(),
68 t!(
69 "functions.cidrSubnet.newCidrTooSmall",
70 newCidr = new_cidr,
71 currentCidr = net.prefix()
72 )
73 .to_string(),
74 ));
75 }
76
77 calculate_ipv4_subnet(net, new_cidr, subnet_index as usize)?
78 }
79 IpNetwork::V6(net) => {
80 if new_cidr > 128 {
81 return Err(DscError::FunctionArg(
82 "cidrSubnet".to_string(),
83 t!("functions.cidrSubnet.invalidPrefixV6", prefix = new_cidr).to_string(),
84 ));
85 }
86
87 if new_cidr < net.prefix() {
88 return Err(DscError::FunctionArg(
89 "cidrSubnet".to_string(),
90 t!(
91 "functions.cidrSubnet.newCidrTooSmall",
92 newCidr = new_cidr,

Callers

nothing calls this directly

Calls 4

FunctionArgEnum · 0.85
calculate_ipv4_subnetFunction · 0.85
calculate_ipv6_subnetFunction · 0.85
as_strMethod · 0.80

Tested by

no test coverage detected