Skip to content

Commit

Permalink
Add inspection utility methods to Env and Key
Browse files Browse the repository at this point in the history
These are intended for debugging and theme manipulation, not general use.
  • Loading branch information
Zarenor committed May 2, 2020
1 parent d1742e5 commit 55ac520
Show file tree
Hide file tree
Showing 2 changed files with 52 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])

### Changed

Expand Down Expand Up @@ -135,6 +136,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/899

## [0.5.0] - 2020-04-01
Expand Down Expand Up @@ -164,6 +166,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
49 changes: 49 additions & 0 deletions druid/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,39 @@ 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.
/// [`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 +307,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 55ac520

Please sign in to comment.