char/avril
(wip) why'd you have to go and make things so complicaaaaated?
git clone https://git.t4t.associates/char/avril
f4a0f5b
main
1use std:: net:: SocketAddr ; 2 3use serde:: ser:: SerializeStruct ; 4use serde::{ Deserialize , Serialize , Serializer }; 5 6# [ derive ( Clone , Debug , Serialize , Deserialize )] 7# [ serde ( tag = "transport" , rename_all = "lowercase" )] 8pub enum Address { 9Tcp { hostname : String , port : u16 }, 10Unix { path : Option < String > }, 11} 12 13# [ derive ( Clone , Debug )] 14pub enum Endpoint { 15Tcp ( SocketAddr ), 16Unix { path : Option < String > }, 17} 18 19impl Serialize for Endpoint { 20fn serialize < S : Serializer >( & self , serializer : S ) ->Result < S :: Ok , S :: Error > { 21match self { 22Self :: Tcp ( address) =>{ 23let mut fields = serializer. serialize_struct ( "Address" , 3 ) ?; 24 fields. serialize_field ( "transport" , "tcp" ) ?; 25 fields. serialize_field ( "hostname" , & address. ip ()) ?; 26 fields. serialize_field ( "port" , & address. port ()) ?; 27 fields. end () 28} 29Self :: Unix { path} =>{ 30let mut fields = serializer. serialize_struct ( "Address" , 2 ) ?; 31 fields. serialize_field ( "transport" , "unix" ) ?; 32 fields. serialize_field ( "path" , path) ?; 33 fields. end () 34} 35} 36} 37} 38 39impl From < Endpoint > for Address { 40fn from ( endpoint : Endpoint ) ->Self { 41match endpoint{ 42Endpoint :: Tcp ( address) =>Self :: Tcp { 43hostname : address. ip (). to_string (), 44port : address. port (), 45}, 46Endpoint :: Unix { path} =>Self :: Unix { path}, 47} 48} 49}