camino_tempfile_ext/fixture/
child.rs

1// Copyright (c) The camino-tempfile Contributors
2// Adapted from assert_fs: Copyright (c) The assert_fs Contributors
3//
4// SPDX-License-Identifier: MIT OR Apache-2.0
5
6use camino::{Utf8Path, Utf8PathBuf};
7use camino_tempfile::Utf8TempDir;
8use std::{
9    borrow::Cow,
10    path::{Path, PathBuf},
11};
12
13/// Access paths within a [`Utf8TempDir`] for testing.
14///
15/// See [`ChildPath`] trait implementations.
16///
17/// ```rust
18/// use camino_tempfile_ext::prelude::*;
19///
20/// let temp = Utf8TempDir::new().unwrap();
21/// let input_file = temp.child("foo.txt");
22/// input_file.touch().unwrap();
23/// temp.close().unwrap();
24/// ```
25pub trait PathChild {
26    /// Access a path within the temporary directory.
27    ///
28    /// # Examples
29    ///
30    /// ```rust
31    /// use camino_tempfile_ext::prelude::*;
32    ///
33    /// let temp = Utf8TempDir::new().unwrap();
34    /// println!("{}", temp.path());
35    /// println!("{}", temp.child("foo/bar.txt").as_path());
36    /// temp.close().unwrap();
37    /// ```
38    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/// A path within a temporary directory.
54///
55/// # Examples
56///
57/// ```rust
58/// use camino_tempfile_ext::prelude::*;
59///
60/// let temp = Utf8TempDir::new().unwrap();
61///
62/// let input_file = temp.child("foo.txt");
63/// input_file.touch().unwrap();
64///
65/// temp.child("bar.txt").touch().unwrap();
66///
67/// temp.close().unwrap();
68/// ```
69#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
70pub struct ChildPath {
71    path: Utf8PathBuf,
72}
73
74impl ChildPath {
75    /// Wrap a path for use with extension traits.
76    ///
77    /// See trait implementations or [`PathChild`] for more details.
78    pub fn new<P: Into<Utf8PathBuf>>(path: P) -> Self {
79        Self { path: path.into() }
80    }
81
82    /// Access the path.
83    pub fn as_path(&self) -> &Utf8Path {
84        // Note: name is `as_path` to match `Deref` impl's `as_std_path`.
85        &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>);