unrspack_resolver/
resolution.rs

1use std::{
2    fmt,
3    path::{Path, PathBuf},
4    sync::Arc,
5};
6
7use crate::{Cache, PackageJson};
8
9/// The final path resolution with optional `?query` and `#fragment`
10pub struct Resolution<C: Cache> {
11    pub(crate) path: PathBuf,
12
13    /// path query `?query`, contains `?`.
14    pub(crate) query: Option<String>,
15
16    /// path fragment `#query`, contains `#`.
17    pub(crate) fragment: Option<String>,
18
19    pub(crate) package_json: Option<Arc<C::Pj>>,
20}
21
22impl<C: Cache> Clone for Resolution<C> {
23    fn clone(&self) -> Self {
24        Self {
25            path: self.path.clone(),
26            query: self.query.clone(),
27            fragment: self.fragment.clone(),
28            package_json: self.package_json.clone(),
29        }
30    }
31}
32
33impl<C: Cache> fmt::Debug for Resolution<C> {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        f.debug_struct("Resolution")
36            .field("path", &self.path)
37            .field("query", &self.query)
38            .field("fragment", &self.fragment)
39            .field("package_json", &self.package_json.as_ref().map(|p| p.path()))
40            .finish()
41    }
42}
43
44impl<C: Cache> PartialEq for Resolution<C> {
45    fn eq(&self, other: &Self) -> bool {
46        self.path == other.path && self.query == other.query && self.fragment == other.fragment
47    }
48}
49impl<C: Cache> Eq for Resolution<C> {}
50
51impl<C: Cache> Resolution<C> {
52    /// Returns the path without query and fragment
53    #[must_use]
54    pub fn path(&self) -> &Path {
55        &self.path
56    }
57
58    /// Returns the path without query and fragment
59    #[must_use]
60    pub fn into_path_buf(self) -> PathBuf {
61        self.path
62    }
63
64    /// Returns the path query `?query`, contains the leading `?`
65    #[must_use]
66    pub fn query(&self) -> Option<&str> {
67        self.query.as_deref()
68    }
69
70    /// Returns the path fragment `#fragment`, contains the leading `#`
71    #[must_use]
72    pub fn fragment(&self) -> Option<&str> {
73        self.fragment.as_deref()
74    }
75
76    /// Returns serialized package_json
77    #[must_use]
78    pub const fn package_json(&self) -> Option<&Arc<C::Pj>> {
79        self.package_json.as_ref()
80    }
81
82    /// Returns the full path with query and fragment
83    #[must_use]
84    pub fn full_path(&self) -> PathBuf {
85        let mut path = self.path.clone().into_os_string();
86        if let Some(query) = &self.query {
87            path.push(query);
88        }
89        if let Some(fragment) = &self.fragment {
90            path.push(fragment);
91        }
92        PathBuf::from(path)
93    }
94}