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
use std::ops::Deref;

use crate::common::authority_mut::AuthorityMutImpl;

use super::{Authority, Host, Port, UserInfo};

pub struct AuthorityMut<'a>(AuthorityMutImpl<'a, Authority>);

impl<'a> Deref for AuthorityMut<'a> {
	type Target = Authority;

	fn deref(&self) -> &Self::Target {
		self.as_authority()
	}
}

impl<'a> AuthorityMut<'a> {
	/// Creates a new mutable authority part.
	///
	/// # Safety
	///
	/// The buffer content between the range `start..end` must be a valid
	/// authority.
	pub unsafe fn new(buffer: &'a mut Vec<u8>, start: usize, end: usize) -> Self {
		Self(AuthorityMutImpl::new(buffer, start, end))
	}

	pub(crate) fn from_impl(i: AuthorityMutImpl<'a, Authority>) -> Self {
		Self(i)
	}

	#[inline]
	pub fn as_authority(&self) -> &Authority {
		self.0.as_authority()
	}

	#[inline]
	pub fn into_authority(self) -> &'a Authority {
		self.0.into_authority()
	}

	#[inline]
	pub fn set_userinfo(&mut self, userinfo: Option<&UserInfo>) {
		self.0.set_userinfo(userinfo)
	}

	#[inline]
	pub fn set_host(&mut self, host: &Host) {
		self.0.set_host(host)
	}

	#[inline]
	pub fn set_port(&mut self, port: Option<&Port>) {
		self.0.set_port(port)
	}
}

#[cfg(test)]
mod tests {
	use crate::Iri;

	#[test]
	fn explicit_empty_with_authority_alike_path() {
		let iri = Iri::new("scheme:////").unwrap();
		let authority = iri.authority();

		assert!(authority.unwrap().is_empty());
	}
}