Skip to content

Commit

Permalink
Merge pull request #880 from Zarenor/master
Browse files Browse the repository at this point in the history
Add inspection utility methods to Env and Key
  • Loading branch information
Zarenor authored May 5, 2020
2 parents ec6ac87 + bfac1af commit 02c4ee0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ While some features like the clipboard, menus or file dialogs are not yet availa
- `Label::with_font` and `set_font`. ([#785] by [@thecodewarrior])
- `InternalEvent::RouteTimer` to route timer events. ([#831] by [@sjoshid])
- `MouseButtons` to `MouseEvent` to track which buttons are being held down during an event. ([#843] by [@xStrom])
- `Env` and `Key` gained methods for inspecting an `Env` at runtime ([#880] by [@Zarenor])
- `UpdateCtx::request_timer` and `UpdateCtx::request_anim_frame`. ([#898] by [@finnerale])

### Changed
Expand Down Expand Up @@ -140,6 +141,7 @@ While some features like the clipboard, menus or file dialogs are not yet availa
[#861]: https://github.com/xi-editor/druid/pull/861
[#869]: https://github.com/xi-editor/druid/pull/869
[#878]: https://github.com/xi-editor/druid/pull/878
[#880]: https://github.com/xi-editor/druid/pull/880
[#889]: https://github.com/xi-editor/druid/pull/889
[#894]: https://github.com/xi-editor/druid/pull/894
[#897]: https://github.com/xi-editor/druid/pull/897
Expand Down Expand Up @@ -172,6 +174,7 @@ Last release without a changelog :(
[@thecodewarrior]: https://github.com/thecodewarrior
[@sjoshid]: https://github.com/sjoshid
[@mastfissh]: https://github.com/mastfissh
[@Zarenor]: https://github.com/Zarenor

[Unreleased]: https://github.com/xi-editor/druid/compare/v0.5.0...master
[0.5.0]: https://github.com/xi-editor/druid/compare/v0.4.0...v0.5.0
Expand Down
53 changes: 53 additions & 0 deletions druid/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
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)> {
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);
Expand Down Expand Up @@ -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.
///
Expand Down

0 comments on commit 02c4ee0

Please sign in to comment.