read_url/file/
context.rs

1use super::{
2    super::{context::*, url::*},
3    file_url::*,
4};
5
6use std::path::*;
7
8impl UrlContext {
9    /// Construct a [FileUrl].
10    pub fn file_url(
11        self: &UrlContextRef,
12        path: PathBuf,
13        host: Option<String>,
14        query: Option<UrlQuery>,
15        fragment: Option<String>,
16    ) -> UrlRef {
17        FileUrl::new(self, path, host, query, fragment).into()
18    }
19
20    /// A valid [FileUrl] for the current working directory.
21    #[cfg(feature = "blocking")]
22    pub fn working_dir_url(self: &UrlContextRef) -> Result<UrlRef, super::super::UrlError> {
23        use std::env::*;
24
25        let mut url = self.file_url(current_dir()?, None, None, None);
26        url.conform()?;
27        Ok(url.into())
28    }
29
30    /// Async version of [working_dir_url](UrlContext::working_dir_url).
31    ///
32    /// Note that the blocking version would also work in async. However, we are providing
33    /// an async version, too, in case the `blocking` feature is disabled.
34    #[cfg(feature = "async")]
35    pub async fn working_dir_url_async(self: &UrlContextRef) -> Result<UrlRef, super::super::UrlError> {
36        use std::env::*;
37
38        let url = self.file_url(current_dir()?, None, None, None);
39        let url = url.conform_async()?.await?;
40        Ok(url)
41    }
42
43    /// A valid [FileUrl] for the current working directory as a vector.
44    ///
45    /// Useful as the "base_urls" argument of [UrlContext::url].
46    #[cfg(feature = "blocking")]
47    pub fn working_dir_url_vec(self: &UrlContextRef) -> Result<Vec<UrlRef>, super::super::UrlError> {
48        Ok(vec![self.working_dir_url()?])
49    }
50
51    /// Async version of [working_dir_url_vec](UrlContext::working_dir_url_vec).
52    ///
53    /// Note that the blocking version would also work in async. However, we are providing
54    /// an async version, too, in case the `blocking` feature is disabled.
55    #[cfg(feature = "async")]
56    pub async fn working_dir_url_vec_async(self: &UrlContextRef) -> Result<Vec<UrlRef>, super::super::UrlError> {
57        Ok(vec![self.working_dir_url_async().await?])
58    }
59}