1 use { 2 super::*, 3 crate::{ 4 browser::BrowserState, 5 command::Sequence, 6 errors::TreeBuildError, 7 launchable::Launchable, 8 verb::Internal, 9 }, 10 std::fmt, 11 }; 12 13 /// Either left or right 14 #[derive(Debug, Clone, Copy, PartialEq)] 15 pub enum HDir { 16 Left, 17 Right, 18 } 19 20 /// the symbolic reference to the panel to close 21 #[derive(Debug, Clone, Copy, PartialEq)] 22 pub enum PanelReference { 23 Active, 24 Leftest, 25 Rightest, 26 Id(PanelId), 27 Preview, 28 } 29 30 /// Result of applying a command to a state 31 pub enum CmdResult { 32 ApplyOnPanel { 33 id: PanelId, 34 }, 35 ClosePanel { 36 validate_purpose: bool, 37 panel_ref: PanelReference, 38 }, 39 DisplayError(String), 40 ExecuteSequence { 41 sequence: Sequence, 42 }, 43 HandleInApp(Internal), // command must be handled at the app level 44 Keep, 45 Launch(Box<Launchable>), 46 NewPanel { 47 state: Box<dyn PanelState>, 48 purpose: PanelPurpose, 49 direction: HDir, 50 }, 51 NewState(Box<dyn PanelState>), 52 PopStateAndReapply, // the state asks the command be executed on a previous state 53 PopState, 54 Quit, 55 RefreshState { 56 clear_cache: bool, 57 }, 58 } 59 60 impl CmdResult { verb_not_found(text: &str) -> CmdResult61 pub fn verb_not_found(text: &str) -> CmdResult { 62 CmdResult::DisplayError(format!("verb not found: {:?}", &text)) 63 } from_optional_state( os: Result<Option<BrowserState>, TreeBuildError>, in_new_panel: bool, ) -> CmdResult64 pub fn from_optional_state( 65 os: Result<Option<BrowserState>, TreeBuildError>, 66 in_new_panel: bool, 67 ) -> CmdResult { 68 match os { 69 Ok(Some(os)) => { 70 if in_new_panel { 71 CmdResult::NewPanel { 72 state: Box::new(os), 73 purpose: PanelPurpose::None, 74 direction: HDir::Right, 75 } 76 } else { 77 CmdResult::NewState(Box::new(os)) 78 } 79 } 80 Ok(None) => CmdResult::Keep, 81 Err(e) => CmdResult::error(e.to_string()), 82 } 83 } error<S: Into<String>>(message: S) -> Self84 pub fn error<S: Into<String>>(message: S) -> Self { 85 Self::DisplayError(message.into()) 86 } 87 } 88 89 impl From<Launchable> for CmdResult { from(launchable: Launchable) -> Self90 fn from(launchable: Launchable) -> Self { 91 CmdResult::Launch(Box::new(launchable)) 92 } 93 } 94 95 impl fmt::Debug for CmdResult { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result96 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 97 write!( 98 f, 99 "{}", 100 match self { 101 CmdResult::ApplyOnPanel { .. } => "ApplyOnPanel", 102 CmdResult::ClosePanel { 103 validate_purpose: false, .. 104 } => "CancelPanel", 105 CmdResult::ClosePanel { 106 validate_purpose: true, .. 107 } => "OkPanel", 108 CmdResult::DisplayError(_) => "DisplayError", 109 CmdResult::ExecuteSequence{ .. } => "ExecuteSequence", 110 CmdResult::Keep => "Keep", 111 CmdResult::Launch(_) => "Launch", 112 CmdResult::NewState { .. } => "NewState", 113 CmdResult::NewPanel { .. } => "NewPanel", 114 CmdResult::PopStateAndReapply => "PopStateAndReapply", 115 CmdResult::PopState => "PopState", 116 CmdResult::HandleInApp(_) => "HandleInApp", 117 CmdResult::Quit => "Quit", 118 CmdResult::RefreshState { .. } => "RefreshState", 119 } 120 ) 121 } 122 } 123