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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//! Godwit State Handler
//!
//! A core state management utility for context switching and global singletons.
use crate::errors::StateError;
use crate::glyph::Glyph;
use crate::settings;
use getter_derive_rs::Getter;
use serde::{Deserialize, Serialize};
use std::{error::Error, fs::File, path::PathBuf};

#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
pub enum Status {
	// TODO: Elaborate enums into impl
	Active,
	Remote,
	Local,
	Tracking,
	Stale,
}

/// Defines a singular project and its corresponding state.
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub struct State {
	pub glyph: Glyph,
	pub directory: Option<PathBuf>,
	pub status: Option<Vec<Status>>,
}

impl Default for State {
	fn default() -> Self {
		State {
			glyph: Default::default(),
			directory: Default::default(),
			status: Default::default(),
		}
	}
}

/// Defines combination of multiple states and represents the snapshot of the application.
#[derive(Debug, Deserialize, Serialize, Getter)]
#[serde(rename_all = "snake_case")]
pub struct StateGraph {
	default: Option<State>,
	active: Option<State>,
	states: Vec<State>,
	ignore: Vec<PathBuf>,
}

impl StateGraph {
	/// Returns new state-graph.
	pub fn init(
		default: Option<State>,
		active: Option<State>,
		states: Option<Vec<State>>,
		ignore: Option<Vec<PathBuf>>,
	) -> Self {
		StateGraph {
			default: default,
			active: active,
			states: states.unwrap_or_default(), // TODO: Templates
			ignore: ignore.unwrap_or_default(), // TODO: Templates
		}
	}

	/// Sets the active state in a state-graph.
	pub fn active(&mut self, state: &State) -> &mut Self {
		self.active = Some(state.clone());
		self
	}

	/// Sets the fallback state in a state-graph.
	pub fn fallback(&mut self, state: &State) -> &mut Self {
		self.default = Some(state.clone());
		self
	}

	/// Sets the working states in a state-graph.
	pub fn states(&mut self, states: &Vec<State>) -> &mut Self {
		self.states = states.to_vec();
		self
	}

	/// Appends a state in the working states list.
	pub fn append_state(&mut self, state: &State) -> &mut Self {
		self.states.push(state.clone());
		self
	}

	/// Drops state from the working states list.
	pub fn drop_state(&mut self, state: &State) -> &mut Self {
		self.states.retain(|p_state| p_state != state);
		self
	}
	/// Searches state by glyph and directory
	pub fn search_states(&self, q_term: String, fuzzy: bool) -> Option<State> {
		for state in &self.states {
			if fuzzy {
				if state
					.glyph
					.to_string()
					.to_lowercase()
					.contains(&q_term.to_lowercase())
					|| state
						.directory
						.clone()
						.unwrap_or_default()
						.to_string_lossy()
						.to_lowercase()
						.contains(&q_term.to_lowercase())
				{
					return Some(state.clone());
				}
			} else {
				if state.glyph.to_string().to_lowercase() == q_term.to_lowercase() {
					return Some(state.clone());
				}
			}
		}
		None
	}

	/// Commits changes to state-graph file.
	pub fn propagate(&self) -> Result<(), Box<dyn Error>> {
		File::open(settings::get_settings()?.get_save_state()?).and_then(|state_file| {
			serde_json::to_writer_pretty(state_file, &self)?;
			Ok(())
		})?;
		Ok(())
	}
}

impl Default for StateGraph {
	fn default() -> Self {
		StateGraph {
			default: Default::default(),
			active: Default::default(),
			states: Default::default(),
			ignore: Default::default(),
		}
	}
}

/// Returns a state-graph instance from the state-graph file.
pub fn load_stategraph() -> Result<StateGraph, Box<dyn Error>> {
	let state_graph =
		File::open(settings::get_settings()?.get_save_state()?).and_then(|state_file| {
			let state_graph: StateGraph = serde_json::from_reader(state_file)?;
			Ok(state_graph)
		})?;
	Ok(state_graph)
}

/// Sets the active state in state-graph and propagates it.
pub fn set_active(q_glyph: &Glyph) -> Result<(), Box<dyn Error>> {
	let mut sg_snapshot: StateGraph = load_stategraph()?;

	sg_snapshot
		.search_states(q_glyph.to_string(), true)
		.map_or_else(
			|| {
				Err(StateError::StateNotFound {
					state: q_glyph.clone().into(),
				}
				.into())
			},
			|q_state| {
				sg_snapshot.active(&q_state).propagate()?;
				Ok(())
			},
		)
}

/// Sets the default state in state-graph and propagates it.
pub fn set_default(q_glyph: &Glyph) -> Result<(), Box<dyn Error>> {
	let mut sg_snapshot: StateGraph = load_stategraph()?;

	sg_snapshot
		.search_states(q_glyph.to_string(), true)
		.map_or_else(
			|| {
				Err(StateError::StateNotFound {
					state: q_glyph.clone().into(),
				}
				.into())
			},
			|q_state| {
				sg_snapshot.fallback(&q_state).propagate()?;
				Ok(())
			},
		)
}

/// Adds a new state in state-graph.
pub fn add_state(
	glyph: Glyph,
	location: PathBuf,
	status: Option<Vec<Status>>,
	as_active: bool,
	as_default: bool,
) -> Result<(), Box<dyn Error>> {
	let mut sg_snapshot = load_stategraph().unwrap_or_default();

	if sg_snapshot
		.search_states(glyph.to_string(), false)
		.is_none()
		&& sg_snapshot
			.search_states(location.to_string_lossy().to_string(), false)
			.is_none()
	{
		let new_state = State {
			glyph: glyph,
			directory: Some(location),
			status: status,
		};

		let mut sg_snapshot = sg_snapshot.append_state(&new_state);

		if sg_snapshot.get_default().is_none() || as_default {
			sg_snapshot = sg_snapshot.fallback(&new_state);
		}

		if settings::get_settings()?.get_switch_on_add() || as_active {
			sg_snapshot = sg_snapshot.active(&new_state);
		}

		sg_snapshot.propagate()?;

		Ok(())
	} else {
		Err(StateError::StateAlreadyExists {
			state: glyph.into(),
		}
		.into())
	}
}

/// Removes the state from state-graph
pub fn purge_state(q_glyph: Glyph) -> Result<(), Box<dyn Error>> {
	let mut sg_snapshot: StateGraph = load_stategraph()?;

	sg_snapshot
		.search_states(q_glyph.to_string(), true)
		.map_or_else(
			|| {
				Err(StateError::StateNotFound {
					state: q_glyph.into(),
				}
				.into())
			},
			|q_state| {
				let mut sg_snapshot = sg_snapshot.drop_state(&q_state);

				if q_state == sg_snapshot.get_default().unwrap_or_default() {
					sg_snapshot = sg_snapshot.fallback(&sg_snapshot.get_states()[0]);
				}

				if q_state == sg_snapshot.get_active().unwrap_or_default() {
					sg_snapshot =
						sg_snapshot.active(&sg_snapshot.get_default().unwrap_or_default());
				}

				sg_snapshot.propagate()?;

				Ok(())
			},
		)
}