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:: borrow:: Cow ; 2use std:: collections:: BTreeMap ; 3use std:: io; 4 5use avril_caps::{ Capabilities , env_name}; 6 7# [ derive ( Clone , Default )] 8pub struct Environment { 9values : BTreeMap < String , ( String , String )>, 10} 11 12fn name ( value : & str ) -> io:: Result < Cow < ' _ , str >> { 13env_name ( value). map_err ( |error| io:: Error :: new ( io:: ErrorKind :: InvalidInput , error)) 14} 15 16fn authorize ( caps : & Capabilities , name : & str ) -> io:: Result <()> { 17 caps. check_env ( name) 18. map_err ( |error| io:: Error :: new ( io:: ErrorKind :: PermissionDenied , error)) 19} 20 21impl Environment { 22pub fn new ( values : impl IntoIterator < Item =( String , String )>) -> io:: Result < Self > { 23let mut result =Self :: default (); 24for ( key, value) in values{ 25if value. contains ( '\0' ) { 26return Err ( io:: Error :: new ( 27 io:: ErrorKind :: InvalidInput , 28"Environment values cannot contain NUL" , 29)); 30} 31 result. values . insert ( name ( & key) ?. into_owned (), ( key, value)); 32} 33Ok ( result) 34} 35 36pub fn from_process () ->Self { 37let values = std:: env:: vars_os (). filter_map ( |( key, value) |{ 38let key = key. into_string (). ok () ?; 39let value = value. into_string (). ok () ?; 40Some (( name ( & key). ok () ?. into_owned (), ( key, value))) 41}); 42Self { values : values. collect () } 43} 44 45pub fn with_defaults ( mut self , defaults : Self ) ->Self { 46for ( key, value) in defaults. values { 47self . values . entry ( key). or_insert ( value); 48} 49self 50} 51 52pub fn get ( & self , caps : & Capabilities , key : & str ) -> io:: Result < Option < String >> { 53let key =name ( key) ?; 54authorize ( caps, & key) ?; 55Ok ( self 56. values 57. get ( key. as_ref ()) 58. map ( |( _, value) | value. clone ())) 59} 60 61pub fn set ( & mut self , caps : & Capabilities , key : & str , value : String ) -> io:: Result <()> { 62let normalized =name ( key) ?; 63if value. contains ( '\0' ) { 64return Err ( io:: Error :: new ( 65 io:: ErrorKind :: InvalidInput , 66"Environment values cannot contain NUL" , 67)); 68} 69authorize ( caps, & normalized) ?; 70self . values 71. insert ( normalized. into_owned (), ( key. to_owned (), value)); 72Ok (()) 73} 74 75pub fn delete ( & mut self , caps : & Capabilities , key : & str ) -> io:: Result <()> { 76let key =name ( key) ?; 77authorize ( caps, & key) ?; 78self . values . remove ( key. as_ref ()); 79Ok (()) 80} 81 82pub fn to_object ( & self , caps : & Capabilities ) ->Vec <( String , String )> { 83self . values 84. iter () 85. filter ( |( key, _) | caps. check_env ( key). is_ok ()) 86. map ( |( _, entry) | entry. clone ()) 87. collect () 88} 89}