@@ -59,7 +59,7 @@ pub fn pop_env_bool(name: &str) -> bool {
5959 result
6060}
6161
62- /// Gets and unsets a uint environment variable `name`, or Ok(None) if it is not set.
62+ /// Gets and unsets a decimal uint environment variable `name`, or Ok(None) if it is not set.
6363///
6464/// # Errors
6565///
@@ -71,10 +71,24 @@ pub fn pop_env_uint(name: &str) -> Result<Option<u32>> {
7171 } ;
7272 let value: u32 = value_str
7373 . parse ( )
74- . context ( format ! ( "Parsing integer variable {name}=\" {value_str}\" " ) ) ?;
74+ . with_context ( || format ! ( "Parsing decimal integer variable {name}=\" {value_str}\" " ) ) ?;
7575 Ok ( Some ( value) )
7676}
7777
78+ /// Gets and unsets an octal uint environment variable `name`, or Ok(None) if it is not set.
79+ ///
80+ /// # Errors
81+ ///
82+ /// This function will return an error if the variable is set, but is not a valid integer string.
83+ pub fn pop_env_octal ( name : & str ) -> Result < Option < u32 > > {
84+ let value_str = match pop_env_str ( name) {
85+ None => return Ok ( None ) ,
86+ Some ( s) => s,
87+ } ;
88+ let value = u32:: from_str_radix ( & value_str, 8 )
89+ . with_context ( || format ! ( "Parsing octal integer variable {name}=\" {value_str}\" " ) ) ?;
90+ Ok ( Some ( value) )
91+ }
7892pub fn make_executable ( path : & str ) -> std:: io:: Result < ( ) > {
7993 let mut perms = fs:: metadata ( path) ?. permissions ( ) ;
8094 let mut mode = perms. mode ( ) ;
@@ -177,4 +191,32 @@ mod tests {
177191 assert ! ( not_set( VAR_NAME ) ) ;
178192 } ) ;
179193 }
194+
195+ #[ test]
196+ fn pop_env_octal_handles_unset ( ) {
197+ temp_env:: with_var_unset ( VAR_NAME , || {
198+ assert ! ( not_set( VAR_NAME ) ) ;
199+ assert_eq ! ( pop_env_octal( VAR_NAME ) . unwrap( ) , None ) ;
200+ } ) ;
201+ }
202+
203+ #[ test]
204+ fn pop_env_octal_handles_invalid ( ) {
205+ temp_env:: with_var ( VAR_NAME , Some ( "99" ) , || {
206+ assert ! ( pop_env_octal( VAR_NAME ) . is_err( ) ) ;
207+ assert ! ( not_set( VAR_NAME ) ) ;
208+ } ) ;
209+ }
210+
211+ #[ test]
212+ fn pop_env_octal_works ( ) {
213+ temp_env:: with_var ( VAR_NAME , Some ( "0777" ) , || {
214+ assert_eq ! ( pop_env_octal( VAR_NAME ) . unwrap( ) , Some ( 511 ) ) ;
215+ assert ! ( not_set( VAR_NAME ) ) ;
216+ } ) ;
217+ temp_env:: with_var ( VAR_NAME , Some ( "0022" ) , || {
218+ assert_eq ! ( pop_env_octal( VAR_NAME ) . unwrap( ) , Some ( 18 ) ) ;
219+ assert ! ( not_set( VAR_NAME ) ) ;
220+ } ) ;
221+ }
180222}
0 commit comments