@@ -7,7 +7,7 @@ use lazy_static::lazy_static;
77use regex:: Regex ;
88use std:: cmp:: min;
99use std:: collections:: HashMap ;
10- use validator:: Validate ;
10+ use validator:: { Validate , ValidationError } ;
1111use validator_derive:: Validate ;
1212
1313lazy_static ! {
@@ -37,6 +37,9 @@ pub struct NotificationHeaders {
3737 ) ]
3838 pub topic : Option < String > ,
3939
40+ #[ validate( custom( function = "validate_urgency" ) ) ]
41+ pub urgency : Option < String > ,
42+
4043 // These fields are validated separately, because the validation is complex
4144 // and based upon the content encoding
4245 pub encoding : Option < String > ,
@@ -45,10 +48,21 @@ pub struct NotificationHeaders {
4548 pub crypto_key : Option < String > ,
4649}
4750
51+ fn validate_urgency ( value : & str ) -> Result < ( ) , ValidationError > {
52+ if [ "very-low" , "low" , "normal" , "high" ] . contains ( & value) {
53+ Ok ( ( ) )
54+ } else {
55+ Err ( ValidationError :: new (
56+ "Value not equal to \" very-low\" , \" low\" , \" normal\" or \" high\" " ,
57+ ) )
58+ }
59+ }
60+
4861impl From < NotificationHeaders > for HashMap < String , String > {
4962 fn from ( headers : NotificationHeaders ) -> Self {
5063 let mut map = HashMap :: new ( ) ;
5164
65+ map. insert_opt ( "urgency" , headers. urgency ) ;
5266 map. insert_opt ( "encoding" , headers. encoding ) ;
5367 map. insert_opt ( "encryption" , headers. encryption ) ;
5468 map. insert_opt ( "encryption_key" , headers. encryption_key ) ;
@@ -73,11 +87,13 @@ impl NotificationHeaders {
7387 . map ( |ttl| min ( ttl, MAX_NOTIFICATION_TTL as i64 ) )
7488 . ok_or ( ApiErrorKind :: NoTTL ) ?;
7589 let topic = get_owned_header ( req, "topic" ) ;
90+ let urgency = get_owned_header ( req, "urgency" ) ;
7691
7792 let headers = if has_data {
7893 NotificationHeaders {
7994 ttl,
8095 topic,
96+ urgency,
8197 encoding : get_owned_header ( req, "content-encoding" ) ,
8298 encryption : get_owned_header ( req, "encryption" ) . map ( Self :: strip_header) ,
8399 encryption_key : get_owned_header ( req, "encryption-key" ) ,
@@ -88,6 +104,7 @@ impl NotificationHeaders {
88104 NotificationHeaders {
89105 ttl,
90106 topic,
107+ urgency,
91108 encoding : None ,
92109 encryption : None ,
93110 encryption_key : None ,
@@ -359,6 +376,7 @@ mod tests {
359376 NotificationHeaders {
360377 ttl: 10 ,
361378 topic: None ,
379+ urgency: None ,
362380 encoding: Some ( "aesgcm" . to_string( ) ) ,
363381 encryption: Some ( "salt=foo" . to_string( ) ) ,
364382 encryption_key: None ,
@@ -384,6 +402,7 @@ mod tests {
384402 NotificationHeaders {
385403 ttl: 10 ,
386404 topic: None ,
405+ urgency: None ,
387406 encoding: Some ( "aes128gcm" . to_string( ) ) ,
388407 encryption: Some ( "notsalt=foo" . to_string( ) ) ,
389408 encryption_key: None ,
@@ -410,6 +429,7 @@ mod tests {
410429 NotificationHeaders {
411430 ttl: 10 ,
412431 topic: None ,
432+ urgency: None ,
413433 encoding: Some ( "aesgcm" . to_string( ) ) ,
414434 encryption: Some ( "salt=foo" . to_string( ) ) ,
415435 encryption_key: None ,
0 commit comments