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
use std::path::Path;

use cfg_match::cfg_match;

cfg_match! {
	target_os = "linux" => {
		/// The separator for entries in the classpath
		pub const CLASSPATH_SEP: char = ':';
	}
	target_os = "windows" => {
		/// The separator for entries in the classpath
		pub const CLASSPATH_SEP: char = ';';
	}
}

/// A utility for working with Java classpaths
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Classpath {
	string: String,
}

impl Classpath {
	/// Create a new empty classpath
	pub fn new() -> Self {
		Self {
			string: String::new(),
		}
	}

	/// Append the classpath separator to the end of the string
	pub fn add_sep(&mut self) {
		self.string.push(CLASSPATH_SEP);
	}

	/// Appends a string to the end of the classpath
	pub fn add(&mut self, string: &str) {
		if let Some(last_char) = self.string.chars().last() {
			if last_char != CLASSPATH_SEP {
				self.add_sep();
			}
		}

		self.string.push_str(string);
	}

	/// Converts a path to a string and appends it to the classpath
	pub fn add_path(&mut self, path: &Path) {
		self.add(path.to_str().expect("Failed to convert path to a string"))
	}

	/// Extends the classpath with another classpath
	pub fn extend(&mut self, other: Classpath) {
		self.add(&other.string)
	}

	/// Obtain the classpath as a string
	pub fn get_str(&self) -> String {
		self.string.clone()
	}
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn test_classpath() {
		let mut classpath = Classpath::new();
		assert_eq!(classpath.get_str(), String::new());
		classpath.add("foo");
		assert_eq!(classpath.get_str(), "foo".to_string());
		classpath.add("bar");
		assert_eq!(
			classpath.get_str(),
			"foo".to_string() + &CLASSPATH_SEP.to_string() + "bar"
		);
	}
}