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
use std::{fmt, cmp};
use std::cmp::{PartialOrd, Ord, Ordering};
use std::hash::{Hash, Hasher};
use std::convert::TryFrom;
use pct_str::PctStr;
use crate::parsing;
use super::Error;

#[derive(Clone, Copy)]
pub struct Segment<'a> {
	/// The path segment slice.
	pub(crate) data: &'a [u8],

	pub(crate) open: bool
}

impl<'a> Segment<'a> {
	pub fn current() -> Segment<'static> {
		Segment {
			data: &[0x2e],
			open: false
		}
	}

	pub fn parent() -> Segment<'static> {
		Segment {
			data: &[0x2e, 0x2e],
			open: false
		}
	}

	pub fn len(&self) -> usize {
		self.data.len()
	}

    pub fn as_ref(&self) -> &[u8] {
		self.data
	}

    /// Get the underlying segment slice as a string slice.
	pub fn as_str(&self) -> &str {
		unsafe {
			std::str::from_utf8_unchecked(&self.data)
		}
	}

    /// Get the underlying segment slice as a percent-encoded string slice.
	pub fn as_pct_str(&self) -> &PctStr {
		unsafe {
			PctStr::new_unchecked(self.as_str())
		}
	}

	pub fn is_open(&self) -> bool {
		self.open
	}

    /// Checks if the segment is empty.
	pub fn is_empty(&self) -> bool {
		self.data.is_empty()
	}

	pub fn open(&mut self) {
		self.open = true
	}
}

impl<'a> TryFrom<&'a str> for Segment<'a> {
	type Error = Error;

	fn try_from(str: &'a str) -> Result<Segment<'a>, Error> {
		let segment_len = parsing::parse_path_segment(str.as_ref(), 0)?;
		let data: &[u8] = str.as_ref();
		if segment_len < data.len() {
			if segment_len == data.len() - 1 && data[segment_len] == 0x2f {
				Ok(Segment {
					data: &data[0..segment_len],
					open: true
				})
			} else {
				Err(Error::InvalidSegment)
			}
		} else {
			Ok(Segment {
				data,
				open: false
			})
		}
	}
}

impl<'a> fmt::Display for Segment<'a> {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		if self.open {
			write!(f, "{}/", self.as_str())
		} else {
			self.as_str().fmt(f)
		}
	}
}

impl<'a> fmt::Debug for Segment<'a> {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		if self.open {
			write!(f, "{}/", self.as_str())
		} else {
			self.as_str().fmt(f)
		}
	}
}

impl<'a> cmp::PartialEq for Segment<'a> {
	fn eq(&self, other: &Segment) -> bool {
		self.open == other.open && self.as_pct_str() == other.as_pct_str()
	}
}

impl<'a> Eq for Segment<'a> { }

impl<'a> cmp::PartialEq<&'a str> for Segment<'a> {
	fn eq(&self, other: &&'a str) -> bool {
		self.as_pct_str() == *other
	}
}

impl<'a> PartialOrd for Segment<'a> {
	fn partial_cmp(&self, other: &Segment<'a>) -> Option<Ordering> {
		Some(self.cmp(other))
	}
}

impl<'a> Ord for Segment<'a> {
	fn cmp(&self, other: &Segment<'a>) -> Ordering {
		self.as_pct_str().cmp(other.as_pct_str())
	}
}

impl<'a> Hash for Segment<'a> {
	fn hash<H: Hasher>(&self, hasher: &mut H) {
		self.as_pct_str().hash(hasher)
	}
}