@@ -6,29 +6,42 @@ use std::{
66} ;
77
88use hickory_server:: proto:: {
9+ ProtoError ,
910 op:: Message ,
1011 rr:: {
1112 Name , Record , RecordSet , RecordType , RrKey ,
1213 domain:: { IntoLabel , Label } ,
1314 } ,
1415 serialize:: binary:: BinDecodable ,
1516} ;
16- use n0_error:: { AnyError , Result , StdResultExt } ;
17+ use n0_error:: { AnyError , StdResultExt , e , stack_error } ;
1718use pkarr:: SignedPacket ;
1819
1920#[ derive(
2021 derive_more:: From , derive_more:: Into , Eq , PartialEq , Ord , PartialOrd , Hash , Clone , Copy ,
2122) ]
2223pub struct PublicKeyBytes ( [ u8 ; 32 ] ) ;
2324
25+ #[ stack_error( derive, add_meta, from_sources) ]
26+ pub enum InvalidPublicKeyBytes {
27+ #[ error( transparent) ]
28+ Encoding {
29+ #[ error( std_err) ]
30+ source : z32:: Z32Error ,
31+ } ,
32+ #[ error( "invalid length, must be 32 bytes" ) ]
33+ InvalidLength ,
34+ }
35+
2436impl PublicKeyBytes {
2537 pub fn new ( bytes : [ u8 ; 32 ] ) -> Self {
2638 Self ( bytes)
2739 }
2840
29- pub fn from_z32 ( s : & str ) -> Result < Self > {
30- let bytes = z32:: decode ( s. as_bytes ( ) ) . anyerr ( ) ?;
31- let bytes = TryInto :: < [ u8 ; 32 ] > :: try_into ( & bytes[ ..] ) . std_context ( "invalid length" ) ?;
41+ pub fn from_z32 ( s : & str ) -> Result < Self , InvalidPublicKeyBytes > {
42+ let bytes = z32:: decode ( s. as_bytes ( ) ) ?;
43+ let bytes = TryInto :: < [ u8 ; 32 ] > :: try_into ( & bytes[ ..] )
44+ . map_err ( |_| e ! ( InvalidPublicKeyBytes :: InvalidLength ) ) ?;
3245 Ok ( Self ( bytes) )
3346 }
3447
@@ -75,7 +88,8 @@ impl TryFrom<PublicKeyBytes> for pkarr::PublicKey {
7588}
7689
7790impl FromStr for PublicKeyBytes {
78- type Err = AnyError ;
91+ type Err = InvalidPublicKeyBytes ;
92+
7993 fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
8094 Self :: from_z32 ( s)
8195 }
@@ -87,17 +101,19 @@ impl AsRef<[u8; 32]> for PublicKeyBytes {
87101 }
88102}
89103
90- pub fn signed_packet_to_hickory_message ( signed_packet : & SignedPacket ) -> Result < Message > {
104+ pub fn signed_packet_to_hickory_message (
105+ signed_packet : & SignedPacket ,
106+ ) -> Result < Message , ProtoError > {
91107 let encoded = signed_packet. encoded_packet ( ) ;
92- let message = Message :: from_bytes ( & encoded) . anyerr ( ) ?;
108+ let message = Message :: from_bytes ( & encoded) ?;
93109 Ok ( message)
94110}
95111
96112pub fn signed_packet_to_hickory_records_without_origin (
97113 signed_packet : & SignedPacket ,
98114 filter : impl Fn ( & Record ) -> bool ,
99- ) -> Result < ( Label , BTreeMap < RrKey , Arc < RecordSet > > ) > {
100- let common_zone = Label :: from_utf8 ( & signed_packet. public_key ( ) . to_z32 ( ) ) . anyerr ( ) ?;
115+ ) -> Result < ( Label , BTreeMap < RrKey , Arc < RecordSet > > ) , ProtoError > {
116+ let common_zone = Label :: from_utf8 ( & signed_packet. public_key ( ) . to_z32 ( ) ) ?;
101117 let mut message = signed_packet_to_hickory_message ( signed_packet) ?;
102118 let answers = message. take_answers ( ) ;
103119 let mut output: BTreeMap < RrKey , Arc < RecordSet > > = BTreeMap :: new ( ) ;
@@ -111,7 +127,7 @@ pub fn signed_packet_to_hickory_records_without_origin(
111127 if name. num_labels ( ) < 1 {
112128 continue ;
113129 }
114- let zone = name. iter ( ) . next_back ( ) . unwrap ( ) . into_label ( ) . anyerr ( ) ?;
130+ let zone = name. iter ( ) . next_back ( ) . unwrap ( ) . into_label ( ) ?;
115131 if zone != common_zone {
116132 continue ;
117133 }
@@ -120,7 +136,7 @@ pub fn signed_packet_to_hickory_records_without_origin(
120136 }
121137
122138 let name_without_zone =
123- Name :: from_labels ( name. iter ( ) . take ( name. num_labels ( ) as usize - 1 ) ) . anyerr ( ) ?;
139+ Name :: from_labels ( name. iter ( ) . take ( name. num_labels ( ) as usize - 1 ) ) ?;
124140 record. set_name ( name_without_zone) ;
125141
126142 let rrkey = RrKey :: new ( record. name ( ) . into ( ) , record. record_type ( ) ) ;
@@ -144,8 +160,8 @@ pub fn record_set_append_origin(
144160 input : & RecordSet ,
145161 origin : & Name ,
146162 serial : u32 ,
147- ) -> Result < RecordSet > {
148- let new_name = input. name ( ) . clone ( ) . append_name ( origin) . anyerr ( ) ?;
163+ ) -> Result < RecordSet , ProtoError > {
164+ let new_name = input. name ( ) . clone ( ) . append_name ( origin) ?;
149165 let mut output = RecordSet :: new ( new_name. clone ( ) , input. record_type ( ) , serial) ;
150166 // TODO: less clones
151167 for record in input. records_without_rrsigs ( ) {
0 commit comments