1use crate::prelude::HelpContext;
2use crate::Return;
3
4#[derive(Debug, Clone)]
5pub struct Frame {
6 pub name: String,
7
8 pub failure: Option<Failure>,
9}
10
11impl Frame {
12 pub fn new(name: String) -> Self {
13 Self {
14 name,
15 failure: None,
16 }
17 }
18
19 pub fn with_failure(mut self, failure: Failure) -> Self {
20 self.failure = Some(failure);
21 self
22 }
23
24 pub fn set_failure(&mut self, failure: Failure) -> &mut Self {
25 self.failure = Some(failure);
26 self
27 }
28}
29
30#[derive(Debug, Clone)]
31pub struct Failure {
32 pub cmd: String,
33
34 pub retval: Return,
35}
36
37impl Failure {
38 pub fn new(cmd: String, retval: Return) -> Self {
39 Self { cmd, retval }
40 }
41}
42
43#[derive(Debug, Clone, Default)]
45pub struct RunningCtx {
46 name: String,
47
48 frames: Vec<Frame>,
49
50 sub_level: u8,
51
52 sub_parser: bool,
53
54 exit: bool,
55
56 display_help: bool,
57
58 help_context: Option<HelpContext>,
59}
60
61impl RunningCtx {
62 pub fn name(&self) -> &str {
64 self.name.as_ref()
65 }
66
67 pub fn frames(&self) -> &[Frame] {
68 &self.frames
69 }
70
71 pub fn sub_level(&self) -> u8 {
72 self.sub_level
73 }
74
75 pub fn sub_parser(&self) -> bool {
76 self.sub_parser
77 }
78
79 pub fn exit(&self) -> bool {
80 self.exit
81 }
82
83 pub fn display_help(&self) -> bool {
84 self.display_help
85 }
86
87 pub fn help_context(&self) -> Option<&HelpContext> {
88 self.help_context.as_ref()
89 }
90
91 pub fn frames_mut(&mut self) -> &mut Vec<Frame> {
93 &mut self.frames
94 }
95
96 pub fn frame_mut(&mut self, index: usize) -> Option<&mut Frame> {
97 self.frames.get_mut(index)
98 }
99
100 pub fn set_name(&mut self, value: String) -> &mut Self {
102 self.name = value;
103 self
104 }
105
106 pub fn set_frames(&mut self, value: Vec<Frame>) -> &mut Self {
107 self.frames = value;
108 self
109 }
110
111 pub fn set_sub_level(&mut self, value: u8) -> &mut Self {
112 self.sub_level = value;
113 self
114 }
115
116 pub fn set_sub_parser(&mut self, value: bool) -> &mut Self {
117 self.sub_parser = value;
118 self
119 }
120
121 pub fn set_exit(&mut self, value: bool) -> &mut Self {
122 self.exit = value;
123 self
124 }
125
126 pub fn set_display_help(&mut self, value: bool) -> &mut Self {
127 self.display_help = value;
128 self
129 }
130
131 pub fn set_help_context(&mut self, value: HelpContext) -> &mut Self {
132 self.help_context = Some(value);
133 self
134 }
135
136 pub fn with_name(mut self, value: String) -> Self {
138 self.name = value;
139 self
140 }
141
142 pub fn with_frames(mut self, value: Vec<Frame>) -> Self {
143 self.frames = value;
144 self
145 }
146
147 pub fn with_sub_level(mut self, value: u8) -> Self {
148 self.sub_level = value;
149 self
150 }
151
152 pub fn with_sub_parser(mut self, value: bool) -> Self {
153 self.sub_parser = value;
154 self
155 }
156
157 pub fn with_exit(mut self, value: bool) -> Self {
158 self.exit = value;
159 self
160 }
161
162 pub fn with_display_help(mut self, value: bool) -> Self {
163 self.display_help = value;
164 self
165 }
166
167 pub fn with_help_context(mut self, value: HelpContext) -> Self {
168 self.help_context = Some(value);
169 self
170 }
171
172 pub fn take_frames(&mut self) -> Vec<Frame> {
173 std::mem::take(&mut self.frames)
174 }
175
176 pub fn take_help_context(&mut self) -> Option<HelpContext> {
177 self.help_context.take()
178 }
179
180 pub fn clear_frames(&mut self) {
181 self.frames.clear();
182 }
183
184 pub fn push_frame(&mut self, frame: Frame) -> &mut Self {
185 self.frames.push(frame);
186 self
187 }
188
189 pub fn pop_frame(&mut self) -> Option<Frame> {
190 self.frames.pop()
191 }
192
193 pub fn top_frame(&mut self) -> Option<&mut Frame> {
194 self.frames.last_mut()
195 }
196
197 pub fn reset_at(&self, level: u8) -> Self {
198 Self {
199 name: self.name.clone(),
200 frames: self.frames[..level as usize].to_vec(),
201 sub_level: level,
202 sub_parser: false,
203 exit: false,
204 display_help: false,
205 help_context: None,
206 }
207 }
208
209 pub fn inc_sub_level(&mut self) -> &mut Self {
210 self.sub_level += 1;
211 self
212 }
213
214 pub fn dec_sub_level(&mut self) -> &mut Self {
215 self.sub_level -= 1;
216 self
217 }
218
219 pub fn chain_error(&mut self) -> Option<aopt::Error> {
220 let mut iter = self.frames.iter_mut();
221
222 if let Some(frame) = iter.next() {
223 let mut error = frame.failure.take()?.retval.take_failure()?;
224
225 for frame in iter {
226 error = error.cause(frame.failure.take()?.retval.take_failure()?);
227 }
228 Some(error)
229 } else {
230 None
231 }
232 }
233}
234
235#[doc(hidden)]
236#[derive(Debug, Clone, Copy, Default)]
237pub struct HideValue<T>(pub T);