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
use prelude::v1::*;
use terminal::*;
use i18n::Strings;

pub enum PropertyCommandStyle {
	DelimitedGetSet
}

pub enum PropertyContext<'b, V> {
	Get(PropertyContextGet<'b>),
	Set(PropertyContextSet<'b, V>)
}

impl<'b, V> PropertyContext<'b, V> {
	/// Retrieve or update the variable. The type of the property has to implement the Display
	/// and Copy traits.
	pub fn apply(&mut self, property_value: &mut V) where V: Display + Copy {
	    match self {
	    	&mut PropertyContext::Get(ref mut get) => {
				get.print_value_display(property_value);
            },
            &mut PropertyContext::Set(ref mut set) => {
                *property_value = set.value;
				set.common.strings.property_value_set(set.common.terminal, &set.common.id, &set.value);
				set.common.terminal.newline();
            }
	    }
	}
}

pub struct PropertyContextGet<'b> {
	pub common: PropertyContextCommon<'b>
}

impl<'b> PropertyContextGet<'b> {
	pub fn print_value_display<V: Display>(&mut self, val: V) {
		self.common.terminal.print_line(&format!("{} = {}", self.common.id, val));
	}

	pub fn print_value_debug<V: Debug>(&mut self, val: V) {
		self.common.terminal.print_line(&format!("{} = {:?}", self.common.id, val));
	}
}

pub struct PropertyContextSet<'b, V> {
	pub common: PropertyContextCommon<'b>,
	pub value: V
}

pub struct PropertyContextCommon<'b> {
	pub args: Cow<'b, str>,
	pub terminal: &'b mut CharacterTerminalWriter,
	pub current_path: &'b str,
	pub id: Cow<'b, str>,
	pub style: PropertyCommandStyle,
	pub strings: &'b Strings
}

impl<'b> PropertyContextCommon<'b> {
	#[inline]
	pub fn get_args(&self) -> &str {
		&self.args
	}

	#[inline]
	pub fn get_terminal(&mut self) -> &mut CharacterTerminalWriter {
		self.terminal
	}

	#[inline]
	pub fn get_current_path(&self) -> &str {
		&self.current_path
	}

	#[inline]
	pub fn get_property_id(&self) -> &str {
		&self.id
	}
}