1use super::{
2 super::{context::*, url::*, util::*},
3 mock_url::*,
4};
5
6use relative_path::*;
7
8impl URL for MockUrl {
9 fn context(&self) -> &UrlContext {
10 &*self.context
11 }
12
13 fn query(&self) -> Option<UrlQuery> {
14 self.query.clone()
15 }
16
17 fn fragment(&self) -> Option<String> {
18 self.fragment.clone()
19 }
20
21 fn format(&self) -> Option<String> {
22 self.format.clone()
23 }
24
25 fn base(&self) -> Option<UrlRef> {
26 if self.slashable {
27 get_relative_path_parent(&self.url_representation).map(|path| self.new_with(path.into()).into())
28 } else {
29 self.base_url_representation.as_ref().map(|path| self.new_with(path.clone()).into())
30 }
31 }
32
33 fn relative(&self, path: &str) -> UrlRef {
34 if self.slashable {
35 let path = RelativePath::new(&self.url_representation).join(path);
36 self.new_with(path.into()).into()
37 } else {
38 let url_representation = self.url_representation.clone() + path;
39 self.new_with(url_representation).into()
40 }
41 }
42
43 #[cfg(feature = "blocking")]
44 fn conform(&mut self) -> Result<(), super::super::UrlError> {
45 Ok(())
46 }
47
48 #[cfg(feature = "async")]
49 fn conform_async(&self) -> Result<ConformFuture, super::super::UrlError> {
50 use super::super::errors::*;
51
52 async fn conform_async(url: MockUrl) -> Result<UrlRef, UrlError> {
53 Ok(url.into())
54 }
55
56 Ok(Box::pin(conform_async(self.clone())))
57 }
58
59 #[cfg(feature = "blocking")]
60 fn open(&self) -> Result<ReadRef, super::super::UrlError> {
61 use super::super::errors::*;
62
63 let content = self.content.as_ref().ok_or_else(|| UrlError::new_io_not_found(self))?;
64 Ok(Box::new(content.reader()))
65 }
66
67 #[cfg(feature = "async")]
68 fn open_async(&self) -> Result<OpenFuture, super::super::UrlError> {
69 use super::super::errors::*;
70
71 async fn open_async(url: MockUrl) -> Result<AsyncReadRef, UrlError> {
72 match url.content {
73 Some(content) => Ok(Box::pin(content.reader())),
74 None => Err(UrlError::new_io_not_found(url)),
75 }
76 }
77
78 Ok(Box::pin(open_async(self.clone())))
79 }
80}