1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use prelude::v1::*;
use property::*;
use terminal::*;
use autocomplete::*;
use cli_property::*;
use cli_command::*;
use super::i18n::Strings;

pub trait CliContext<'a> {
	/// Creates a new prefixed execution context, but only if the current line matches. Reduces the
	/// processing overhead for large tree command environments.
	fn with_prefix<'b>(&'b mut self, prefix: &str) -> Option<PrefixedExecutor<'a, 'b>>;

	/// Announces a command to be executed. Returns an execution context in case the command is invoked.
	fn command<'b>(&'b mut self, cmd: &str) -> Option<CommandContext<'b>>;

	/// Announces a property that can be manipulated. Returns an execution context in case the property
	/// is to be either retrieved or updated.
	fn property<'b, V, P, Id: Into<Cow<'b, str>>>(&'b mut self, property_id: Id, input_parser: P) -> Option<PropertyContext<'b, V>> where P: ValueInput<V>, V: Display;
}

/// Helper for matching commands and properties against an input line.
pub struct CliExecutor<'a> {
	matcher: CliLineMatcher<'a>,
	strings: &'a Strings,
	terminal: &'a mut CharacterTerminalWriter
}

impl<'a> CliContext<'a> for CliExecutor<'a> {	
	fn with_prefix<'b>(&'b mut self, prefix: &str) -> Option<PrefixedExecutor<'a, 'b>> {
		if self.matcher.starts_with(&prefix) {
			let p = PrefixedExecutor {
				prefix: prefix.to_string().into(),
				executor: self
			};

			return Some(p);
		} else {
			self.matcher.add_unmatched_prefix(&prefix);
		}
		
		None
	}
	
	fn command<'b>(&'b mut self, cmd: &str) -> Option<CommandContext<'b>> {

		if self.matcher.match_cmd_str(cmd, None) == LineMatcherProgress::MatchFound {
			let args = if let &LineBufferResult::Match { ref args, .. } = self.matcher.get_state() {
				Some(args.clone())
			} else {
				None
			};

			if let Some(args) = args {
				let ctx = CommandContext {
					args: args.into(),
					terminal: self.terminal,
					current_path: ""
				};
				
				return Some(ctx);
			}
		}

		None
	}
		
	fn property<'b, V, P, Id: Into<Cow<'b, str>>>(&'b mut self, property_id: Id, input_parser: P) -> Option<PropertyContext<'b, V>> where P: ValueInput<V>, V: Display {
		let property_id: Cow<str> = property_id.into();

		if self.matcher.match_cmd_str(&format!("{}/get", property_id), None) == LineMatcherProgress::MatchFound {
			let args = if let &LineBufferResult::Match { ref args, .. } = self.matcher.get_state() {
				args.clone()
			} else {
				"".into()
			};

			return Some(PropertyContext::Get(PropertyContextGet {
				common: PropertyContextCommon {
					args: args.into(),
					terminal: self.terminal,
					current_path: "",
					id: property_id,
					style: PropertyCommandStyle::DelimitedGetSet,
					strings: &*self.strings
				}
			}));
		}

		if self.matcher.match_cmd_str(&format!("{}/set", property_id), None) == LineMatcherProgress::MatchFound {
			let args = if let &LineBufferResult::Match { ref args, .. } = self.matcher.get_state() {
				args.trim()
			} else {
				"".into()
			};

			match input_parser.input(&args) {
				Ok(val) => {
					return Some(PropertyContext::Set(PropertyContextSet {
						common: PropertyContextCommon {
							args: args.into(),
							terminal: self.terminal,
							current_path: "",
							id: property_id,
							style: PropertyCommandStyle::DelimitedGetSet,
							strings: &*self.strings
						},
						value: val
					}));
				},
				Err(e) => {

					match e {
						PropertyValidationError::InvalidInput => {
							self.strings.property_invalid_value(self.terminal, &property_id, &args);
						},
						PropertyValidationError::ValueTooSmall { min, val } => {
							self.strings.property_value_too_small(self.terminal, &property_id, &val, &min);
						},
						PropertyValidationError::ValueTooBig { max, val } => {
							self.strings.property_value_too_big(self.terminal, &property_id, &val, &max);
						}
					}

					self.terminal.newline();
				}
			}
		}

		None
	}
}

impl<'a> CliExecutor<'a> {
	pub fn new<T: CharacterTerminalWriter>(matcher: CliLineMatcher<'a>, strings: &'a Strings, terminal: &'a mut T) -> Self {
		CliExecutor {
			matcher: matcher,
			strings: strings,
			terminal: terminal
		}
	}

	/// Finish the execution of this line invocation.
	pub fn close(self) -> CliLineMatcher<'a> {
		self.matcher
	}


	

	/// Get the associated terminal.
	pub fn get_terminal(&mut self) -> &mut CharacterTerminalWriter {
		self.terminal
	}
}

impl<'a> Deref for CliExecutor<'a> {
	type Target = &'a mut CharacterTerminalWriter;

    fn deref<'b>(&'b self) -> &'b &'a mut CharacterTerminalWriter {
        &self.terminal
    }
}

pub struct PrefixedExecutor<'a: 'p, 'p> {
	prefix: Cow<'p, str>,
	executor: &'p mut CliExecutor<'a>
}

impl<'a, 'p> PrefixedExecutor<'a, 'p> {
	fn add_prefix(&self, cmd: &str) -> String {
		format!("{}{}", self.prefix, cmd)
	}
}


impl<'a, 'p> CliContext<'a> for PrefixedExecutor<'a, 'p> {
	fn with_prefix<'b>(&'b mut self, prefix: &str) -> Option<PrefixedExecutor<'a, 'b>> {
		let prefix = self.add_prefix(prefix);
		self.executor.with_prefix(&prefix)
	}

	fn command<'b>(&'b mut self, cmd: &str) -> Option<CommandContext<'b>> {
		let cmd = self.add_prefix(cmd);

		self.executor.command(&cmd)
	}
	
	fn property<'b, V, P, Id: Into<Cow<'b, str>>>(&'b mut self, property_id: Id, input_parser: P) -> Option<PropertyContext<'b, V>> where P: ValueInput<V>, V: Display {
		let property_id: Cow<str> = property_id.into();
		let property_id = self.add_prefix(&property_id);

		self.executor.property(property_id, input_parser)
	}
}