@@ -156,6 +156,16 @@ pub struct ValueTypeError {
156
156
found : Value ,
157
157
}
158
158
159
+ /// An error type for when a key is missing from the [`Env`].
160
+ ///
161
+ /// [`Env`]: struct.Env.html
162
+ #[ derive( Debug , Clone ) ]
163
+ #[ non_exhaustive]
164
+ pub struct MissingKeyError {
165
+ /// The raw key.
166
+ key : Arc < str > ,
167
+ }
168
+
159
169
impl Env {
160
170
/// State for whether or not to paint colorful rectangles for layout
161
171
/// debugging.
@@ -205,24 +215,30 @@ impl Env {
205
215
///
206
216
/// Panics if the key is not found, or if it is present with the wrong type.
207
217
pub fn get < ' a , V : ValueType < ' a > > ( & ' a self , key : impl Borrow < Key < V > > ) -> V {
208
- let key = key. borrow ( ) ;
209
- if let Some ( value) = self . 0 . map . get ( key. key ) {
210
- value. to_inner_unchecked ( )
211
- } else {
212
- panic ! ( "key for {} not found" , key. key)
218
+ match self . try_get ( key) {
219
+ Ok ( value) => value,
220
+ Err ( err) => panic ! ( "{}" , err) ,
213
221
}
214
222
}
215
223
216
- /// Gets a value from the environment.
224
+ /// Trys to get a value from the environment.
225
+ ///
226
+ /// If the value is not found, the raw key is returned as the error.
217
227
///
218
228
/// # Panics
219
229
///
220
230
/// Panics if the value for the key is found, but has the wrong type.
221
- pub fn try_get < ' a , V : ValueType < ' a > > ( & ' a self , key : impl Borrow < Key < V > > ) -> Option < V > {
231
+ pub fn try_get < ' a , V : ValueType < ' a > > (
232
+ & ' a self ,
233
+ key : impl Borrow < Key < V > > ,
234
+ ) -> Result < V , MissingKeyError > {
222
235
self . 0
223
236
. map
224
237
. get ( key. borrow ( ) . key )
225
238
. map ( |value| value. to_inner_unchecked ( ) )
239
+ . ok_or ( MissingKeyError {
240
+ key : key. borrow ( ) . key . into ( ) ,
241
+ } )
226
242
}
227
243
228
244
/// Gets a value from the environment, in its encapsulated [`Value`] form,
@@ -233,25 +249,28 @@ impl Env {
233
249
///
234
250
/// # Panics
235
251
///
236
- /// Panics if the key is not found
252
+ /// Panics if the key is not found.
253
+ ///
237
254
/// [`Value`]: enum.Value.html
238
255
pub fn get_untyped ( & self , key : impl Borrow < Key < ( ) > > ) -> & Value {
239
- let key = key. borrow ( ) ;
240
- if let Some ( value) = self . 0 . map . get ( key. key ) {
241
- value
242
- } else {
243
- panic ! ( "key for {} not found" , key. key)
256
+ match self . try_get_untyped ( key) {
257
+ Ok ( val) => val,
258
+ Err ( err) => panic ! ( "{}" , err) ,
244
259
}
245
260
}
246
261
247
262
/// Gets a value from the environment, in its encapsulated [`Value`] form,
248
- /// returning None if a value isn't found.
263
+ /// returning `None` if a value isn't found.
264
+ ///
265
+ /// # Note
266
+ /// This is not intended for general use, but only for inspecting an `Env`
267
+ /// e.g. for debugging, theme editing, and theme loading.
249
268
///
250
- /// *WARNING:* This is not intended for general use, but only for inspecting an `Env` e.g.
251
- /// for debugging, theme editing, and theme loading.
252
269
/// [`Value`]: enum.Value.html
253
- pub fn try_get_untyped ( & self , key : impl Borrow < Key < ( ) > > ) -> Option < & Value > {
254
- self . 0 . map . get ( key. borrow ( ) . key )
270
+ pub fn try_get_untyped ( & self , key : impl Borrow < Key < ( ) > > ) -> Result < & Value , MissingKeyError > {
271
+ self . 0 . map . get ( key. borrow ( ) . key ) . ok_or ( MissingKeyError {
272
+ key : key. borrow ( ) . key . into ( ) ,
273
+ } )
255
274
}
256
275
257
276
/// Gets the entire contents of the `Env`, in key-value pairs.
@@ -484,7 +503,21 @@ impl std::fmt::Display for ValueTypeError {
484
503
}
485
504
}
486
505
506
+ impl MissingKeyError {
507
+ /// The raw key that was missing.
508
+ pub fn raw_key ( & self ) -> & str {
509
+ & self . key
510
+ }
511
+ }
512
+
513
+ impl std:: fmt:: Display for MissingKeyError {
514
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
515
+ write ! ( f, "Missing key: '{}'" , self . key)
516
+ }
517
+ }
518
+
487
519
impl std:: error:: Error for ValueTypeError { }
520
+ impl std:: error:: Error for MissingKeyError { }
488
521
489
522
/// Use this macro for types which are cheap to clone (ie all `Copy` types).
490
523
macro_rules! impl_value_type_owned {
0 commit comments