camino_tempfile_ext/fixture/
child.rs1use camino::{Utf8Path, Utf8PathBuf};
7use camino_tempfile::Utf8TempDir;
8use std::{
9 borrow::Cow,
10 path::{Path, PathBuf},
11};
12
13pub trait PathChild {
26 fn child<P: AsRef<Utf8Path>>(&self, path: P) -> ChildPath;
39}
40
41impl PathChild for Utf8TempDir {
42 fn child<P: AsRef<Utf8Path>>(&self, path: P) -> ChildPath {
43 ChildPath::new(self.path().join(path.as_ref()))
44 }
45}
46
47impl PathChild for ChildPath {
48 fn child<P: AsRef<Utf8Path>>(&self, path: P) -> ChildPath {
49 ChildPath::new(self.as_path().join(path.as_ref()))
50 }
51}
52
53#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
70pub struct ChildPath {
71 path: Utf8PathBuf,
72}
73
74impl ChildPath {
75 pub fn new<P: Into<Utf8PathBuf>>(path: P) -> Self {
79 Self { path: path.into() }
80 }
81
82 pub fn as_path(&self) -> &Utf8Path {
84 &self.path
86 }
87}
88
89impl AsRef<Utf8Path> for ChildPath {
90 fn as_ref(&self) -> &Utf8Path {
91 &self.path
92 }
93}
94
95impl AsRef<Path> for ChildPath {
96 fn as_ref(&self) -> &Path {
97 self.path.as_ref()
98 }
99}
100
101impl std::ops::Deref for ChildPath {
102 type Target = Utf8Path;
103 #[inline]
104 fn deref(&self) -> &Utf8Path {
105 &self.path
106 }
107}
108
109macro_rules! impl_partial_eq {
110 ($lhs:ty, $rhs: ty) => {
111 impl PartialEq<$rhs> for $lhs {
112 #[inline]
113 fn eq(&self, other: &$rhs) -> bool {
114 <Utf8Path as PartialEq>::eq(self, other)
115 }
116 }
117
118 impl PartialEq<$lhs> for $rhs {
119 #[inline]
120 fn eq(&self, other: &$lhs) -> bool {
121 <Utf8Path as PartialEq>::eq(self, other)
122 }
123 }
124 };
125}
126
127impl_partial_eq!(ChildPath, Utf8Path);
128impl_partial_eq!(ChildPath, &Utf8Path);
129impl_partial_eq!(ChildPath, Utf8PathBuf);
130impl_partial_eq!(ChildPath, Cow<'_, Utf8Path>);
131
132macro_rules! impl_partial_eq_std_path {
133 ($lhs:ty, $rhs: ty) => {
134 impl PartialEq<$rhs> for $lhs {
135 #[inline]
136 fn eq(&self, other: &$rhs) -> bool {
137 <Path as PartialEq>::eq(self.as_ref(), other)
138 }
139 }
140
141 impl PartialEq<$lhs> for $rhs {
142 #[inline]
143 fn eq(&self, other: &$lhs) -> bool {
144 <Path as PartialEq>::eq(self, other.as_ref())
145 }
146 }
147 };
148}
149
150impl_partial_eq_std_path!(ChildPath, Path);
151impl_partial_eq_std_path!(ChildPath, &Path);
152impl_partial_eq_std_path!(ChildPath, PathBuf);
153impl_partial_eq_std_path!(ChildPath, Cow<'_, Path>);