use std::path::Path;
use cfg_match::cfg_match;
cfg_match! {
target_os = "linux" => {
pub const CLASSPATH_SEP: char = ':';
}
target_os = "windows" => {
pub const CLASSPATH_SEP: char = ';';
}
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Classpath {
string: String,
}
impl Classpath {
pub fn new() -> Self {
Self {
string: String::new(),
}
}
pub fn add_sep(&mut self) {
self.string.push(CLASSPATH_SEP);
}
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);
}
pub fn add_path(&mut self, path: &Path) {
self.add(path.to_str().expect("Failed to convert path to a string"))
}
pub fn extend(&mut self, other: Classpath) {
self.add(&other.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"
);
}
}