@@ -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,
@@ -236,22 +252,23 @@ impl Env {
236
252
/// Panics if the key is not found
237
253
/// [`Value`]: enum.Value.html
238
254
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)
255
+ match self . try_get_untyped ( key) {
256
+ Ok ( val) => val,
257
+ Err ( err) => panic ! ( "{}" , err) ,
244
258
}
245
259
}
246
260
247
261
/// Gets a value from the environment, in its encapsulated [`Value`] form,
248
- /// returning None if a value isn't found.
262
+ /// returning `None`` if a value isn't found.
263
+ ///
264
+ /// *WARNING:* This is not intended for general use, but only for
265
+ /// inspecting an `Env` e.g. for debugging, theme editing, and theme loading.
249
266
///
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
267
/// [`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 )
268
+ pub fn try_get_untyped ( & self , key : impl Borrow < Key < ( ) > > ) -> Result < & Value , MissingKeyError > {
269
+ self . 0 . map . get ( key. borrow ( ) . key ) . ok_or ( MissingKeyError {
270
+ key : key. borrow ( ) . key . into ( ) ,
271
+ } )
255
272
}
256
273
257
274
/// Gets the entire contents of the `Env`, in key-value pairs.
@@ -484,7 +501,21 @@ impl std::fmt::Display for ValueTypeError {
484
501
}
485
502
}
486
503
504
+ impl MissingKeyError {
505
+ /// The raw key that was missing.
506
+ pub fn raw_key ( & self ) -> & str {
507
+ & self . key
508
+ }
509
+ }
510
+
511
+ impl std:: fmt:: Display for MissingKeyError {
512
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
513
+ write ! ( f, "Missing key: '{}'" , self . key)
514
+ }
515
+ }
516
+
487
517
impl std:: error:: Error for ValueTypeError { }
518
+ impl std:: error:: Error for MissingKeyError { }
488
519
489
520
/// Use this macro for types which are cheap to clone (ie all `Copy` types).
490
521
macro_rules! impl_value_type_owned {
0 commit comments