11use crate :: common:: { cidr_parts, parse_prefix, IpNetworkError } ;
2- use std:: { cmp, fmt, net:: Ipv6Addr , str:: FromStr , convert :: TryFrom } ;
2+ use std:: { cmp, convert :: TryFrom , fmt, net:: Ipv6Addr , str:: FromStr } ;
33
44const IPV6_BITS : u8 = 128 ;
55const IPV6_SEGMENT_BITS : u8 = 16 ;
66
77/// Represents a network range where the IP addresses are of v6
88#[ derive( Debug , Clone , Copy , Hash , PartialEq , Eq , PartialOrd , Ord ) ]
9- #[ cfg_attr( feature = "schemars" , derive( schemars:: JsonSchema ) ) ]
109pub struct Ipv6Network {
1110 addr : Ipv6Addr ,
1211 prefix : u8 ,
@@ -33,6 +32,46 @@ impl serde::Serialize for Ipv6Network {
3332 }
3433}
3534
35+ #[ cfg( feature = "schemars" ) ]
36+ impl schemars:: JsonSchema for Ipv6Network {
37+ fn schema_name ( ) -> String {
38+ "Ipv6Network" . to_string ( )
39+ }
40+
41+ fn json_schema ( _: & mut schemars:: gen:: SchemaGenerator ) -> schemars:: schema:: Schema {
42+ schemars:: schema:: SchemaObject {
43+ instance_type : Some ( schemars:: schema:: InstanceType :: String . into ( ) ) ,
44+ string : Some ( Box :: new ( schemars:: schema:: StringValidation {
45+ pattern : Some (
46+ concat ! (
47+ r#"^("# ,
48+ r#"([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}"# ,
49+ r#"|([0-9a-fA-F]{1,4}:){1,7}:"# ,
50+ r#"|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}"# ,
51+ r#"|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}"# ,
52+ r#"|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}"# ,
53+ r#"|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}"# ,
54+ r#"|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}"# ,
55+ r#"|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})"# ,
56+ r#"|:((:[0-9a-fA-F]{1,4}){1,7}|:)"# ,
57+ r#"|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}"# ,
58+ r#"|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])"# ,
59+ r#"|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])"# ,
60+ r#"")[/](12[0-8]|1[0-1][0-9]|[0-9]?[0-9])$"# ,
61+ ) . to_string ( ) ,
62+ ) ,
63+ ..Default :: default ( )
64+ } ) ) ,
65+ extensions : [ ( "x-rust-type" . to_string ( ) , "ipnetwork::Ipv6Network" . into ( ) ) ]
66+ . iter ( )
67+ . cloned ( )
68+ . collect ( ) ,
69+ ..Default :: default ( )
70+ }
71+ . into ( )
72+ }
73+ }
74+
3675impl Ipv6Network {
3776 /// Constructs a new `Ipv6Network` from any `Ipv6Addr` and a prefix denoting the network size.
3877 ///
0 commit comments