Skip to content

Commit

Permalink
Merge pull request #34 from Respo/cast-branch
Browse files Browse the repository at this point in the history
simplify cast_branch by removing result container
  • Loading branch information
NoEgAm authored Jul 2, 2024
2 parents 57ba5a2 + 1fb29d0 commit a2df129
Show file tree
Hide file tree
Showing 12 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion demo_respo/src/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct MainState {
pub fn comp_counter(states: &RespoStatesTree, global_counted: i32) -> Result<RespoElement<ActionOp>, String> {
let cursor = states.path();

let state = states.cast_branch::<MainState>()?;
let state = states.cast_branch::<MainState>();
let counted = state.counted;

let on_inc = {
Expand Down
2 changes: 1 addition & 1 deletion demo_respo/src/panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl RespoEffect for PanelMount {

pub fn comp_panel(states: &RespoStatesTree) -> Result<RespoNode<ActionOp>, String> {
let cursor = states.path();
let state = states.cast_branch::<PanelState>()?;
let state = states.cast_branch::<PanelState>();

let on_input = {
let cursor = cursor.to_owned();
Expand Down
2 changes: 1 addition & 1 deletion demo_respo/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn comp_task(
let task_id = &task.id;

let cursor = states.path();
let state = states.cast_branch::<TaskState>()?;
let state = states.cast_branch::<TaskState>();

let on_toggle = {
let tid = task_id.to_owned();
Expand Down
2 changes: 1 addition & 1 deletion demo_respo/src/todolist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct TodolistState {

pub fn comp_todolist(states: &RespoStatesTree, tasks: &[Task]) -> Result<RespoElement<ActionOp>, String> {
let cursor = states.path();
let state = states.cast_branch::<TodolistState>()?;
let state = states.cast_branch::<TodolistState>();

let mut children: Vec<(RespoIndexKey, RespoNode<_>)> = vec![];
for task in tasks {
Expand Down
2 changes: 1 addition & 1 deletion respo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "respo"
version = "0.1.8"
version = "0.1.9"
edition = "2021"
description = "a tiny virtual DOM library migrated from ClojureScript"
license = "Apache-2.0"
Expand Down
16 changes: 8 additions & 8 deletions respo/src/states_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::fmt::Debug;
use std::hash::Hash;
use std::rc::Rc;

use crate::log;
use crate::warn_log;
pub(crate) use dyn_eq::DynEq;

pub use state::RespoState;
Expand Down Expand Up @@ -61,27 +61,27 @@ impl RespoStatesTree {
}

/// get shared data from state tree. fallback to backup and then default
pub fn cast_branch<T>(&self) -> Result<Rc<T>, String>
pub fn cast_branch<T>(&self) -> Rc<T>
where
T: Clone + Default + RespoState + 'static,
{
if let Some(v) = &self.data {
if let Some(v) = v.0.as_ref().as_any().downcast_ref::<T>() {
return Ok(Rc::new(v.to_owned()));
return Rc::new(v.to_owned());
} else {
log!("failed to cast state to {}", std::any::type_name::<T>());
warn_log!("failed to cast state to {} , at {:?}", std::any::type_name::<T>(), self.cursor);
}
}

match &self.backup {
Some(v) => {
let mut t = T::default();
match t.restore_from(v) {
Ok(_) => Ok(Rc::new(t)),
Err(e) => Err(e),
if let Err(e) = t.restore_from(v) {
warn_log!("failed to restore from backup: {} , at {:?}", e, self.cursor);
}
Rc::new(t)
}
None => Ok(Rc::new(T::default())),
None => Rc::new(T::default()),
}
}

Expand Down
2 changes: 1 addition & 1 deletion respo/src/ui/dialog/alert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ where

fn new(states: RespoStatesTree, options: AlertOptions, on_read: U) -> Result<Self, String> {
let cursor = states.path();
let state = states.cast_branch::<AlertPluginState>()?;
let state = states.cast_branch::<AlertPluginState>();

let instance = Self {
state,
Expand Down
2 changes: 1 addition & 1 deletion respo/src/ui/dialog/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ where

fn new(states: RespoStatesTree, options: ConfirmOptions, on_confirm: U) -> Result<Self, String> {
let cursor = states.path();
let state = states.cast_branch::<ConfirmPluginState>()?;
let state = states.cast_branch::<ConfirmPluginState>();

let instance = Self {
state,
Expand Down
2 changes: 1 addition & 1 deletion respo/src/ui/dialog/drawer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ where

fn new(states: RespoStatesTree, options: DrawerOptions<T>) -> Result<Self, String> {
let cursor = states.path();
let state = states.cast_branch::<DrawerPluginState>()?;
let state = states.cast_branch::<DrawerPluginState>();

let instance = Self {
state,
Expand Down
2 changes: 1 addition & 1 deletion respo/src/ui/dialog/modal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ where

fn new(states: RespoStatesTree, options: ModalOptions<T>) -> Result<Self, String> {
let cursor = states.path();
let state = states.cast_branch::<ModalPluginState>()?;
let state = states.cast_branch::<ModalPluginState>();

let instance = Self {
state,
Expand Down
4 changes: 2 additions & 2 deletions respo/src/ui/dialog/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ where
T: Clone + Debug + RespoAction,
{
let cursor = states.path();
let mut state = states.cast_branch::<InputState>()?;
let mut state = states.cast_branch::<InputState>();
if let Some(text) = &options.initial_value {
state = Rc::new(InputState {
draft: text.to_string(),
Expand Down Expand Up @@ -365,7 +365,7 @@ where

fn new(states: RespoStatesTree, options: PromptOptions, on_submit: U) -> Result<Self, String> {
let cursor = states.path();
let state = states.cast_branch::<PromptPluginState>()?;
let state = states.cast_branch::<PromptPluginState>();

let instance = Self {
states,
Expand Down

0 comments on commit a2df129

Please sign in to comment.