-
Notifications
You must be signed in to change notification settings - Fork 567
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add inspection utility methods to Env and Key #880
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -208,6 +208,43 @@ impl Env { | |
.map(|value| value.to_inner_unchecked()) | ||
} | ||
|
||
/// Gets a value from the environment, in its encapsulated [`Value`] form, | ||
/// expecting the key to be present. | ||
/// | ||
/// *WARNING:* This is not intended for general use, but only for inspecting an `Env` e.g. | ||
/// for debugging, theme editing, and theme loading. | ||
/// | ||
/// # Panics | ||
/// | ||
/// Panics if the key is not found | ||
/// [`Value`]: enum.Value.html | ||
pub fn get_untyped(&self, key: impl Borrow<Key<()>>) -> &Value { | ||
let key = key.borrow(); | ||
if let Some(value) = self.0.map.get(key.key) { | ||
value | ||
} else { | ||
panic!("key for {} not found", key.key) | ||
} | ||
} | ||
|
||
/// Gets a value from the environment, in its encapsulated [`Value`] form, | ||
/// returning None if a value isn't found. | ||
/// | ||
/// *WARNING:* This is not intended for general use, but only for inspecting an `Env` e.g. | ||
/// for debugging, theme editing, and theme loading. | ||
/// [`Value`]: enum.Value.html | ||
pub fn try_get_untyped(&self, key: impl Borrow<Key<()>>) -> Option<&Value> { | ||
Zarenor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.0.map.get(key.borrow().key) | ||
} | ||
|
||
/// Gets the entire contents of the `Env`, in key-value pairs. | ||
/// | ||
/// *WARNING:* This is not intended for general use, but only for inspecting an `Env` e.g. | ||
/// for debugging, theme editing, and theme loading. | ||
pub fn get_all(&self) -> impl ExactSizeIterator<Item = (&String, &Value)> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a little late to the game but I might've just called this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah. Hm. That might have made some idiomatic sense. No reason not to change it. I'll try to find some time this weekend. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no pressure, just an observation! |
||
self.0.map.iter() | ||
} | ||
|
||
/// Adds a key/value, acting like a builder. | ||
pub fn adding<'a, V: ValueType<'a>>(mut self, key: Key<V>, value: impl Into<V::Owned>) -> Env { | ||
let env = Arc::make_mut(&mut self.0); | ||
|
@@ -274,6 +311,22 @@ impl<T> Key<T> { | |
} | ||
} | ||
|
||
impl Key<()> { | ||
/// Create an untyped `Key` with the given string value. | ||
/// | ||
/// *WARNING:* This is not for general usage - it's only useful | ||
/// for inspecting the contents of an [`Env`] - this is expected to be | ||
/// used for debugging, loading, and manipulating themes. | ||
/// | ||
/// [`Env`]: struct.Env.html | ||
pub const fn untyped(key: &'static str) -> Self { | ||
Key { | ||
key, | ||
value_type: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
impl Value { | ||
/// Get a reference to the inner object. | ||
/// | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's document panic conditions under the Panics header. Take a look at some of the other functions here like
try_get
.