Skip to content
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

Merged
merged 3 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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)
Copy link
Member

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.

}
}

/// 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)> {
Copy link
Member

Choose a reason for hiding this comment

The 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 iter or iter_raw or something. 🤷

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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);
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