Skip to content

Commit 9e61607

Browse files
committed
clean out warnings
1 parent 98fce68 commit 9e61607

File tree

14 files changed

+162
-105
lines changed

14 files changed

+162
-105
lines changed

anathema-runtime/src/builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ impl<G: GlobalEventHandler> Builder<G> {
6969
/// Registers a component as a template-only component.
7070
///
7171
/// This component has no state or reacts to any events
72-
pub fn template(&mut self, ident: impl Into<String>, template: impl ToSourceKind) {
73-
_ = self.prototype(ident, template, || (), || ());
72+
pub fn template(&mut self, ident: impl Into<String>, template: impl ToSourceKind) -> Result<()> {
73+
self.prototype(ident, template, || (), || ())
7474
}
7575

7676
/// Registers a [Component] with the runtime.

anathema-state/src/value/list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ mod test {
256256
list.push_back(1usize);
257257

258258
list.push_front(0usize);
259-
let mut list = list.to_ref();
259+
let list = list.to_ref();
260260

261261
let val = list.get(0).unwrap().to_ref();
262262
assert_eq!(*val, 0);

anathema-state/src/value/map.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,11 @@ mod test {
135135
eprintln!("- drop: {}", self.0);
136136
}
137137
}
138-
struct DMRef<'a>(&'a DM);
139-
impl Drop for DMRef<'_> {
140-
fn drop(&mut self) {
141-
eprintln!("- drop ref: {}", self.0.0);
142-
}
143-
}
144138

145139
#[test]
146140
fn remove() {
147141
let mut map = Map::empty();
148142
map.insert("a", DM(1));
149-
let a = map.get("a").unwrap();
150-
151143
assert!(map.remove("a").is_some());
152144
assert!(map.is_empty());
153145
}

anathema-state/src/value/maybe.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{PendingValue, Value};
2-
use crate::states::{AnyList, AnyMaybe};
3-
use crate::{AnyMap, Color, Hex, State, TypeId};
2+
use crate::states::AnyMaybe;
3+
use crate::{State, TypeId};
44

55
pub type Nullable<T> = Maybe<T>;
66

@@ -31,14 +31,22 @@ impl<T: State> Maybe<T> {
3131
}
3232
}
3333

34-
pub fn map<F, U>(&mut self, mut f: F) -> Option<U>
34+
pub fn map_mut<F, U>(&mut self, mut f: F) -> Option<U>
3535
where
3636
F: FnMut(&mut T) -> U,
3737
{
3838
let value = self.0.as_mut()?;
3939
Some(f(&mut *value.to_mut()))
4040
}
4141

42+
pub fn map_ref<F, U>(&self, f: F) -> Option<U>
43+
where
44+
F: Fn(&T) -> U,
45+
{
46+
let value = self.0.as_ref()?;
47+
Some(f(&*value.to_ref()))
48+
}
49+
4250
pub fn and_then_ref<F, U>(&self, f: F) -> Option<U>
4351
where
4452
F: Fn(&T) -> Option<U>,
@@ -86,21 +94,31 @@ impl<T: State + TypeId> From<Option<T>> for Value<Maybe<T>> {
8694
}
8795
}
8896

97+
impl<T: State + TypeId> Value<Maybe<T>> {
98+
pub fn map<F, U>(&mut self, f: F) -> Option<U>
99+
where
100+
F: FnMut(&mut T) -> U,
101+
{
102+
let mut value = self.to_mut();
103+
value.map_mut(f)
104+
}
105+
}
106+
89107
#[cfg(test)]
90108
mod test {
91109
use super::*;
92110

93111
#[test]
94-
fn blah() {
95-
let maybe = Maybe::some(1);
96-
let value = maybe.get_ref().unwrap();
97-
let state = value.to_ref();
98-
assert_eq!(1, value.as_int().unwrap());
112+
fn nullable_int() {
113+
let value = Maybe::some(1);
114+
let inner = value.get().unwrap();
115+
assert_eq!(1, inner.as_state().unwrap().as_int().unwrap());
99116
}
100117

101118
#[test]
102119
fn nested_nullables() {
103-
let value = Maybe::some(1);
104-
assert_eq!(1, value.as_int().unwrap());
120+
let value = Maybe::some(Maybe::some(1));
121+
let one = value.and_then_ref(|inner_map| inner_map.map_ref(|m| *m)).unwrap();
122+
assert_eq!(one, 1);
105123
}
106124
}

anathema-state/src/value/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ impl<T: State> From<T> for Value<T> {
5454
}
5555

5656
impl Value<Box<dyn State>> {
57+
// This is a mildly sketchy function and you should probably not use it
5758
#[doc(hidden)]
5859
pub fn to_mut_cast<U: State>(&mut self) -> Unique<'_, U> {
5960
let value = get_unique(self.key.owned());
@@ -245,7 +246,7 @@ impl ElementState {
245246
fn as_ref<T: 'static>(&self) -> &T {
246247
match self {
247248
Self::Dropped => unreachable!(),
248-
Self::Alive(value) => self.try_as_ref().expect("invalid type"),
249+
Self::Alive(_) => self.try_as_ref().expect("invalid type"),
249250
}
250251
}
251252

anathema-value-resolver/src/expression.rs

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ pub(crate) fn resolve_value<'a, 'bp>(
213213
// - Operations and conditionals -
214214
// -----------------------------------------------------------------------------
215215
ValueExpr::Not(value_expr) => {
216-
let value = resolve_value(value_expr, ctx) else { return ValueKind::Null };
216+
let value = resolve_value(value_expr, ctx);
217217
// let ValueKind::Bool(val) = resolve_value(value_expr, ctx) else { return ValueKind::Null };
218218
ValueKind::Bool(!value.truthiness())
219219
}
@@ -252,9 +252,6 @@ pub(crate) fn resolve_value<'a, 'bp>(
252252
_ => ValueKind::Null,
253253
},
254254
ValueExpr::Either(first, second) => {
255-
let s1 = format!("{first:?}");
256-
let s2 = format!("{second:?}");
257-
258255
let value = resolve_value(first, ctx);
259256
match value {
260257
ValueKind::Null => resolve_value(second, ctx),
@@ -331,17 +328,12 @@ fn resolve_index<'bp>(
331328
) -> ValueExpr<'bp> {
332329
match src {
333330
ValueExpr::DynMap(value) | ValueExpr::Composite(value) => {
334-
let type_info = value.type_info();
335331
let state = or_null!(value.as_state());
336332
let map = match state.as_any_map() {
337-
Some(map) => {
338-
anathema_debug::debug_to_file!("there is a {type_info:?} | {:?}", value.key());
339-
map
340-
}
333+
Some(map) => map,
341334
None => {
342335
// This will happen in the event of an `Option<DynMap>`
343336
// where the `Option` is `None`
344-
anathema_debug::debug_to_file!("there is no {type_info:?} | {:?}", value.key());
345337
value.subscribe(ctx.sub);
346338
ctx.sub_to.push(value.sub_key());
347339
return ValueExpr::Null;
@@ -367,14 +359,8 @@ fn resolve_index<'bp>(
367359
ctx.sub_to.push(value.sub_key());
368360
let state = or_null!(value.as_state());
369361
let list = match state.as_any_list() {
370-
Some(list) => {
371-
let key = value.owned_key();
372-
value.unsubscribe(ctx.sub);
373-
ctx.sub_to.remove(value.sub_key());
374-
list
375-
}
362+
Some(list) => list,
376363
None => {
377-
let key = value.owned_key();
378364
value.subscribe(ctx.sub);
379365
ctx.sub_to.push(value.sub_key());
380366
return ValueExpr::Null;
@@ -388,11 +374,7 @@ fn resolve_index<'bp>(
388374
//
389375
// If the value does exist unsubscribe from the underlying map / state
390376
let val = match val {
391-
Some(val) => {
392-
value.unsubscribe(ctx.sub);
393-
ctx.sub_to.remove(value.sub_key());
394-
val
395-
}
377+
Some(val) => val,
396378
None => {
397379
value.subscribe(ctx.sub);
398380
ctx.sub_to.push(value.sub_key());
@@ -513,7 +495,7 @@ fn float_op(lhs: f64, rhs: f64, op: Op) -> f64 {
513495

514496
#[cfg(test)]
515497
mod test {
516-
use anathema_state::{Changes, Map, States, drain_changes};
498+
use anathema_state::{Changes, Map, Maybe, States, drain_changes};
517499
use anathema_templates::expressions::{ident, index, num, strlit};
518500

519501
use crate::testing::setup;
@@ -570,6 +552,8 @@ mod test {
570552
test.with_state(|state| state.list.remove(0));
571553

572554
drain_changes(&mut changes);
555+
assert!(!changes.is_empty());
556+
573557
for (subs, _) in changes.drain() {
574558
if subs.iter().any(|sub| sub == value.sub) {
575559
value.reload(&test.attributes);
@@ -588,13 +572,13 @@ mod test {
588572
// let expr = index(index(ident("state"), strlit("opt_map")), strlit("key"));
589573
let expr = index(ident("state"), strlit("opt_map"));
590574

591-
let mut value = test.eval(&expr);
575+
let value = test.eval(&expr);
592576
assert!(value.as_str().is_none());
593577

594578
test.with_state(|state| {
595579
let mut map = Map::empty();
596580
map.insert("key", 123);
597-
state.opt_map.set(Some(map));
581+
state.opt_map.set(Maybe::some(map));
598582
});
599583

600584
drain_changes(&mut changes);

anathema-value-resolver/src/testing.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anathema_state::{AnyMap, AnyState, List, Map, State, StateId, States, Subscriber, Value};
1+
use anathema_state::{AnyMap, List, Map, Maybe, State, StateId, States, Subscriber, Value};
22
use anathema_store::slab::Key;
33
use anathema_templates::{Expression, Globals, Variables};
44

@@ -14,7 +14,7 @@ pub(crate) struct TestState {
1414
pub(crate) float: Value<f64>,
1515
pub(crate) list: Value<List<&'static str>>,
1616
pub(crate) map: Value<Map<i32>>,
17-
pub(crate) opt_map: Value<Option<Map<i32>>>,
17+
pub(crate) opt_map: Value<Maybe<Map<i32>>>,
1818
}
1919

2020
impl TestState {
@@ -26,7 +26,7 @@ impl TestState {
2626
float: 0.0.into(),
2727
list: List::empty().into(),
2828
map: Map::empty().into(),
29-
opt_map: Value::new(None),
29+
opt_map: Value::new(Maybe::none()),
3030
}
3131
}
3232
}
@@ -107,10 +107,7 @@ pub(crate) fn setup<'bp, F>(states: &mut States, globals: Variables, mut f: F)
107107
where
108108
F: FnMut(&mut TestCase<'_, 'bp>),
109109
{
110-
// let state: Map<Box<dyn AnyState>> = Value::new(Map::empty());
111-
let state: Box<dyn AnyState> = Box::new(TestState::new());
112-
let state: anathema_state::Value<Box<dyn AnyState>> = anathema_state::Value::new(state);
113-
states.insert(state);
110+
states.insert(Box::new(TestState::new()));
114111
let mut test = TestCase::new(states, globals.into());
115112
f(&mut test)
116113
}

anathema-value-resolver/src/value.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ impl<'a, 'bp> TryFrom<&'a ValueKind<'bp>> for &'a str {
448448

449449
#[cfg(test)]
450450
pub(crate) mod test {
451-
use anathema_state::{Hex, States};
451+
use anathema_state::{Hex, Map, Maybe, States};
452452
use anathema_templates::Variables;
453453
use anathema_templates::expressions::{
454454
add, and, boolean, chr, div, either, eq, float, greater_than, greater_than_equal, hex, ident, index, less_than,
@@ -764,8 +764,18 @@ pub(crate) mod test {
764764
fn optional_map_resolve() {
765765
let mut states = States::new();
766766
setup(&mut states, Default::default(), |test| {
767+
// At first there is no map...
767768
let expr = index(ident("state"), strlit("opt_map"));
768-
let value = test.eval(&*expr);
769+
let mut value = test.eval(&*expr);
770+
assert!(matches!(value.kind, ValueKind::Null));
771+
772+
// ... then we insert a map
773+
test.with_state(|state| {
774+
let map = Map::empty();
775+
state.opt_map.set(Maybe::some(map));
776+
});
777+
778+
value.reload(&test.attributes);
769779
assert!(matches!(value.kind, ValueKind::DynMap(_)));
770780
});
771781
}

anathema-widgets/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ pub mod tabindex;
2121
pub mod tree;
2222
mod widget;
2323

24-
24+
#[cfg(test)]
2525
mod testing;

anathema-widgets/src/nodes/controlflow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use anathema_state::Change;
22
use anathema_templates::blueprints::Blueprint;
3-
use anathema_value_resolver::{AttributeStorage, Value, ValueKind};
3+
use anathema_value_resolver::{AttributeStorage, Value};
44

55
use crate::WidgetKind;
66
use crate::widget::WidgetTreeView;

0 commit comments

Comments
 (0)