Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 //! The essential objective of this crate is to provide an API for copying
2 //! directories and their contents in a straightforward and predictable way.
3 //! See the documentation of the `copy_dir` function for more info.
4
5 extern crate walkdirrust-analyzer cargo copy_dir 0.1.3 walkdir/;
6
7 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/;
8 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#, ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#, Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#};
9 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#;
10
11 macro_rules! push_errorrust-analyzer cargo copy_dir 0.1.3 push_error! {
12 ($expr:expr, $vec:ident) => {
13 match $expr {
14 Err(e) => $vec.push(e),
15 Ok(_) => (),
16 }
17 };
18 }
19
20 macro_rules! make_errrust-analyzer cargo copy_dir 0.1.3 make_err! {
21 ($text:expr, $kind:expr) => {
22 Error::new($kind, $text)
23 };
24
25 ($text:expr) => {
26 make_err!($text, ErrorKind::Other)
27 };
28 }
29
30 /// Copy a directory and its contents
31 ///
32 /// Unlike e.g. the `cp -r` command, the behavior of this function is simple
33 /// and easy to understand. The file or directory at the source path is copied
34 /// to the destination path. If the source path points to a directory, it will
35 /// be copied recursively with its contents.
36 ///
37 /// # Errors
38 ///
39 /// * It's possible for many errors to occur during the recursive copy
40 /// operation. These errors are all returned in a `Vec`. They may or may
41 /// not be helpful or useful.
42 /// * If the source path does not exist.
43 /// * If the destination path exists.
44 /// * If something goes wrong with copying a regular file, as with
45 /// `std::fs::copy()`.
46 /// * If something goes wrong creating the new root directory when copying
47 /// a directory, as with `std::fs::create_dir`.
48 /// * If you try to copy a directory to a path prefixed by itself i.e.
49 /// `copy_dir(".", "./foo")`. See below for more details.
50 ///
51 /// # Caveats/Limitations
52 ///
53 /// I would like to add some flexibility around how "edge cases" in the copying
54 /// operation are handled, but for now there is no flexibility and the following
55 /// caveats and limitations apply (not by any means an exhaustive list):
56 ///
57 /// * You cannot currently copy a directory into itself i.e.
58 /// `copy_dir(".", "./foo")`. This is because we are recursively walking
59 /// the directory to be copied *while* we're copying it, so in this edge
60 /// case you get an infinite recursion. Fixing this is the top of my list
61 /// of things to do with this crate.
62 /// * Hard links are not accounted for, i.e. if more than one hard link
63 /// pointing to the same inode are to be copied, the data will be copied
64 /// twice.
65 /// * Filesystem boundaries may be crossed.
66 /// * Symbolic links will be copied, not followed.
67 pub fn copy_dirrust-analyzer cargo copy_dir 0.1.3 copy_dir().<Q: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>, P: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>>(from: P, to: Q) -> Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#>> {
68 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().from.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().existsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]exists().() {
69 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(make_errrust-analyzer cargo copy_dir 0.1.3 make_err!!("source path does not exist", ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::NotFoundrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#NotFound#));
70 } else if to.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().existsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]exists().() {
71 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(make_errrust-analyzer cargo copy_dir 0.1.3 make_err!!("target path exists", ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::AlreadyExistsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#AlreadyExists#));
72 }
73
74 let mut errors = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
75
76 // copying a regular file is EZ
77 if from.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().is_filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]is_file().() {
78 return fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::copyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/copy().(&from, &to).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(|_| Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().());
79 }
80
81 fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::create_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/create_dir().(&to)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
82
83 // The approach taken by this code (i.e. walkdir) will not gracefully
84 // handle copying a directory into itself, so we're going to simply
85 // disallow it by checking the paths. This is a thornier problem than I
86 // wish it was, and I'd like to find a better solution, but for now I
87 // would prefer to return an error rather than having the copy blow up
88 // in users' faces. Ultimately I think a solution to this will involve
89 // not using walkdir at all, and might come along with better handling
90 // of hard links.
91 let target_is_under_source = from
92 .as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()
93 .canonicalizerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]canonicalize().()
94 .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]and_then().(|fc| to.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().canonicalizerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]canonicalize().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(|tc| (fc, tc)))
95 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(|(fc, tc)| tc.starts_withrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]starts_with().(fc))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
96
97 if target_is_under_source {
98 fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::remove_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/remove_dir().(&to)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
99
100 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(make_errrust-analyzer cargo copy_dir 0.1.3 make_err!!(
101 "cannot copy to a path prefixed by the source path"
102 ));
103 }
104
105 for entry in walkdirrust-analyzer cargo walkdir 2.5.0 crate/::WalkDirrust-analyzer cargo walkdir 2.5.0 WalkDir#::newrust-analyzer cargo walkdir 2.5.0 impl#[WalkDir]new().(&from)
106 .min_depthrust-analyzer cargo walkdir 2.5.0 impl#[WalkDir]min_depth().(1)
107 .into_iterrust-analyzer cargo walkdir 2.5.0 impl#[WalkDir][IntoIterator]into_iter().()
108 .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(|e| e.okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().())
109 {
110 let relative_path = match entry.pathrust-analyzer cargo walkdir 2.5.0 dent/impl#[DirEntry]path().().strip_prefixrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]strip_prefix().(&from) {
111 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(rp) => rp,
112 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("strip_prefix failed; this is a probably a bug in copy_dir"),
113 };
114
115 let target_path = {
116 let mut target_path = to.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().to_path_bufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_path_buf().();
117 target_path.pushrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]push().(relative_path);
118 target_path
119 };
120
121 let source_metadata = match entry.metadatarust-analyzer cargo walkdir 2.5.0 dent/impl#[DirEntry]metadata().() {
122 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => {
123 errors.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(make_errrust-analyzer cargo copy_dir 0.1.3 make_err!!(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
124 "walkdir metadata error for {:?}",
125 entry.pathrust-analyzer cargo walkdir 2.5.0 dent/impl#[DirEntry]path().()
126 )));
127
128 continue;
129 }
130
131 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(md) => md,
132 };
133
134 if source_metadata.is_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[Metadata]is_dir().() {
135 push_errorrust-analyzer cargo copy_dir 0.1.3 push_error!!(fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::create_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/create_dir().(&target_path), errors);
136 push_errorrust-analyzer cargo copy_dir 0.1.3 push_error!!(
137 fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::set_permissionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/set_permissions().(&target_path, source_metadata.permissionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[Metadata]permissions().()),
138 errors
139 );
140 } else {
141 push_errorrust-analyzer cargo copy_dir 0.1.3 push_error!!(fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::copyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/copy().(entry.pathrust-analyzer cargo walkdir 2.5.0 dent/impl#[DirEntry]path().(), &target_path), errors);
142 }
143 }
144
145 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(errors)
146 }
147
148 #[cfg(test)]
149 mod testsrust-analyzer cargo copy_dir 0.1.3 tests/ {
150 #![allow(unused_variables)]
151
152 extern crate stdrust-analyzer cargo copy_dir 0.1.3 tests/std/;
153 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/;
154 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#;
155 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::Commandrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Command#;
156
157 extern crate tempdirrust-analyzer cargo copy_dir 0.1.3 tests/tempdir/;
158 extern crate walkdirrust-analyzer cargo copy_dir 0.1.3 tests/walkdir/;
159
160 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
161 fn single_filerust-analyzer cargo copy_dir 0.1.3 tests/single_file().() {
162 let file = Filerust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#File#("foo.file");
163 assert_we_match_the_real_thingrust-analyzer cargo copy_dir 0.1.3 tests/assert_we_match_the_real_thing().(&file, true, Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
164 }
165
166 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
167 fn directory_with_filerust-analyzer cargo copy_dir 0.1.3 tests/directory_with_file().() {
168 let dir = Dirrust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#Dir#(
169 "foo",
170 vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![Filerust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#File#("bar"), Dirrust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#Dir#("baz", vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![Filerust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#File#("quux"), Filerust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#File#("fobe")])],
171 );
172 assert_we_match_the_real_thingrust-analyzer cargo copy_dir 0.1.3 tests/assert_we_match_the_real_thing().(&dir, true, Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
173 }
174
175 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
176 fn source_does_not_existrust-analyzer cargo copy_dir 0.1.3 tests/source_does_not_exist().() {
177 let base_dir = tempdirrust-analyzer cargo tempdir 0.3.7 crate/::TempDirrust-analyzer cargo tempdir 0.3.7 TempDir#::newrust-analyzer cargo tempdir 0.3.7 impl#[TempDir]new().("copy_dir_test").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
178 let source_path = base_dir.as_refrust-analyzer cargo tempdir 0.3.7 impl#[TempDir][`AsRef<Path>`]as_ref().().joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("noexist.file");
179 match superrust-analyzer cargo copy_dir 0.1.3 crate/::copy_dirrust-analyzer cargo copy_dir 0.1.3 copy_dir().(&source_path, "dest.file") {
180 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(_) => panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("expected Err"),
181 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => match err.kindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]kind().() {
182 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::NotFoundrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#NotFound# => (),
183 _ => panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("expected kind NotFound"),
184 },
185 }
186 }
187
188 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
189 fn target_existsrust-analyzer cargo copy_dir 0.1.3 tests/target_exists().() {
190 let base_dir = tempdirrust-analyzer cargo tempdir 0.3.7 crate/::TempDirrust-analyzer cargo tempdir 0.3.7 TempDir#::newrust-analyzer cargo tempdir 0.3.7 impl#[TempDir]new().("copy_dir_test").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
191 let source_path = base_dir.as_refrust-analyzer cargo tempdir 0.3.7 impl#[TempDir][`AsRef<Path>`]as_ref().().joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("exist.file");
192 let target_path = base_dir.as_refrust-analyzer cargo tempdir 0.3.7 impl#[TempDir][`AsRef<Path>`]as_ref().().joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("exist2.file");
193
194 {
195 fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::createrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]create().(&source_path).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
196 fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::createrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]create().(&target_path).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
197 }
198
199 match superrust-analyzer cargo copy_dir 0.1.3 crate/::copy_dirrust-analyzer cargo copy_dir 0.1.3 copy_dir().(&source_path, &target_path) {
200 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(_) => panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("expected Err"),
201 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => match err.kindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]kind().() {
202 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::AlreadyExistsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#AlreadyExists# => (),
203 _ => panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("expected kind AlreadyExists"),
204 },
205 }
206 }
207
208 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
209 fn attempt_copy_under_selfrust-analyzer cargo copy_dir 0.1.3 tests/attempt_copy_under_self().() {
210 let base_dir = tempdirrust-analyzer cargo tempdir 0.3.7 crate/::TempDirrust-analyzer cargo tempdir 0.3.7 TempDir#::newrust-analyzer cargo tempdir 0.3.7 impl#[TempDir]new().("copy_dir_test").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
211 let dir = Dirrust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#Dir#(
212 "foo",
213 vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![Filerust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#File#("bar"), Dirrust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#Dir#("baz", vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![Filerust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#File#("quux"), Filerust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#File#("fobe")])],
214 );
215 dir.createrust-analyzer cargo copy_dir 0.1.3 tests/impl#[`DirMaker<'a>`]create().(&base_dir).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
216
217 let from = base_dir.as_refrust-analyzer cargo tempdir 0.3.7 impl#[TempDir][`AsRef<Path>`]as_ref().().joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("foo");
218 let to = from.as_pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]as_path().().joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("beez");
219
220 let copy_result = superrust-analyzer cargo copy_dir 0.1.3 crate/::copy_dirrust-analyzer cargo copy_dir 0.1.3 copy_dir().(&from, &to);
221 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(copy_result.is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
222
223 let copy_err = copy_result.unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().();
224 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(copy_err.kindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]kind().(), stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Other#);
225 }
226
227 // utility stuff below here
228
229 enum DirMakerrust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#<'a> {
230 Dirrust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#Dir#(&'a str, Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<DirMakerrust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#<'a>>),
231 Filerust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#File#(&'a str),
232 }
233
234 use selfrust-analyzer cargo copy_dir 0.1.3 tests/::DirMakerrust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#::*;
235
236 impl<'a> DirMakerrust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#<'a> {
237 fn createrust-analyzer cargo copy_dir 0.1.3 tests/impl#[`DirMaker<'a>`]create().<P: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>>(&self, base: P) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> {
238 match *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self {
239 Dirrust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#Dir#(ref name, ref contents) => {
240 let path = base.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(name);
241 fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::create_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/create_dir().(&path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
242
243 for thing in contents {
244 thing.createrust-analyzer cargo copy_dir 0.1.3 tests/impl#[`DirMaker<'a>`]create().(&path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
245 }
246 }
247
248 Filerust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#File#(ref name) => {
249 let path = base.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(name);
250 fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::createrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]create().(path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
251 }
252 }
253
254 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
255 }
256
257 fn namerust-analyzer cargo copy_dir 0.1.3 tests/impl#[`DirMaker<'a>`]name().(&self) -> &str {
258 match *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self {
259 Dirrust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#Dir#(name, _) => name,
260 Filerust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#File#(name) => name,
261 }
262 }
263 }
264
265 fn assert_dirs_samerust-analyzer cargo copy_dir 0.1.3 tests/assert_dirs_same().<P: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>>(a: P, b: P) {
266 let mut wa = walkdirrust-analyzer cargo walkdir 2.5.0 crate/::WalkDirrust-analyzer cargo walkdir 2.5.0 WalkDir#::newrust-analyzer cargo walkdir 2.5.0 impl#[WalkDir]new().(a.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()).into_iterrust-analyzer cargo walkdir 2.5.0 impl#[WalkDir][IntoIterator]into_iter().();
267 let mut wb = walkdirrust-analyzer cargo walkdir 2.5.0 crate/::WalkDirrust-analyzer cargo walkdir 2.5.0 WalkDir#::newrust-analyzer cargo walkdir 2.5.0 impl#[WalkDir]new().(b.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()).into_iterrust-analyzer cargo walkdir 2.5.0 impl#[WalkDir][IntoIterator]into_iter().();
268
269 loop {
270 let o_na = wa.nextrust-analyzer cargo walkdir 2.5.0 impl#[IntoIter][Iterator]next().();
271 let o_nb = wb.nextrust-analyzer cargo walkdir 2.5.0 impl#[IntoIter][Iterator]next().();
272
273 if o_na.is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() && o_nb.is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() {
274 let r_na = o_na.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
275 let r_nb = o_nb.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
276
277 if r_na.is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().() && r_nb.is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().() {
278 let na = r_na.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
279 let nb = r_nb.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
280
281 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
282 na.pathrust-analyzer cargo walkdir 2.5.0 dent/impl#[DirEntry]path().().strip_prefixrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]strip_prefix().(a.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()),
283 nb.pathrust-analyzer cargo walkdir 2.5.0 dent/impl#[DirEntry]path().().strip_prefixrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]strip_prefix().(b.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().())
284 );
285
286 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(na.file_typerust-analyzer cargo walkdir 2.5.0 dent/impl#[DirEntry]file_type().(), nb.file_typerust-analyzer cargo walkdir 2.5.0 dent/impl#[DirEntry]file_type().());
287
288 // TODO test permissions
289 }
290 } else if o_na.is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() && o_nb.is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() {
291 return;
292 } else {
293 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(false);
294 }
295 }
296 }
297
298 fn assert_we_match_the_real_thingrust-analyzer cargo copy_dir 0.1.3 tests/assert_we_match_the_real_thing().(
299 dir: &DirMakerrust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#,
300 explicit_name: bool,
301 o_pre_state: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&DirMakerrust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#>,
302 ) {
303 let base_dir = tempdirrust-analyzer cargo tempdir 0.3.7 crate/::TempDirrust-analyzer cargo tempdir 0.3.7 TempDir#::newrust-analyzer cargo tempdir 0.3.7 impl#[TempDir]new().("copy_dir_test").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
304
305 let source_dir = base_dir.as_refrust-analyzer cargo tempdir 0.3.7 impl#[TempDir][`AsRef<Path>`]as_ref().().joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("source");
306 let our_dir = base_dir.as_refrust-analyzer cargo tempdir 0.3.7 impl#[TempDir][`AsRef<Path>`]as_ref().().joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("ours");
307 let their_dir = base_dir.as_refrust-analyzer cargo tempdir 0.3.7 impl#[TempDir][`AsRef<Path>`]as_ref().().joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("theirs");
308
309 fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::create_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/create_dir().(&source_dir).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
310 fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::create_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/create_dir().(&our_dir).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
311 fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::create_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/create_dir().(&their_dir).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
312
313 dir.createrust-analyzer cargo copy_dir 0.1.3 tests/impl#[`DirMaker<'a>`]create().(&source_dir).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
314 let source_path = source_dir.as_pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]as_path().().joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(dir.namerust-analyzer cargo copy_dir 0.1.3 tests/impl#[`DirMaker<'a>`]name().());
315
316 let (our_target, their_target) = if explicit_name {
317 (
318 our_dir.as_pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]as_path().().joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(dir.namerust-analyzer cargo copy_dir 0.1.3 tests/impl#[`DirMaker<'a>`]name().()),
319 their_dir.as_pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]as_path().().joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(dir.namerust-analyzer cargo copy_dir 0.1.3 tests/impl#[`DirMaker<'a>`]name().()),
320 )
321 } else {
322 (our_dir.clonerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][Clone]clone().(), their_dir.clonerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][Clone]clone().())
323 };
324
325 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(pre_state) = o_pre_state {
326 pre_state.createrust-analyzer cargo copy_dir 0.1.3 tests/impl#[`DirMaker<'a>`]create().(&our_dir).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
327 pre_state.createrust-analyzer cargo copy_dir 0.1.3 tests/impl#[`DirMaker<'a>`]create().(&their_dir).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
328 }
329
330 let we_good = superrust-analyzer cargo copy_dir 0.1.3 crate/::copy_dirrust-analyzer cargo copy_dir 0.1.3 copy_dir().(&source_path, &our_target).is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().();
331
332 let their_status = Commandrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Command#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]new().("cp")
333 .argrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]arg().("-r")
334 .argrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]arg().(source_path.as_os_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]as_os_str().())
335 .argrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]arg().(their_target.as_os_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]as_os_str().())
336 .statusrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]status().()
337 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
338
339 // TODO any way to ask cp whether it worked or not?
340 // portability?
341 // assert_eq!(we_good, their_status.success());
342 assert_dirs_samerust-analyzer cargo copy_dir 0.1.3 tests/assert_dirs_same().(&their_dir, &our_dir);
343 }
344
345 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
346 fn dir_maker_and_assert_dirs_same_baselinerust-analyzer cargo copy_dir 0.1.3 tests/dir_maker_and_assert_dirs_same_baseline().() {
347 let dir = Dirrust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#Dir#("foobar", vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![Filerust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#File#("bar"), Dirrust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#Dir#("baz", Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().())]);
348
349 let base_dir = tempdirrust-analyzer cargo tempdir 0.3.7 crate/::TempDirrust-analyzer cargo tempdir 0.3.7 TempDir#::newrust-analyzer cargo tempdir 0.3.7 impl#[TempDir]new().("copy_dir_test").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
350
351 let a_path = base_dir.as_refrust-analyzer cargo tempdir 0.3.7 impl#[TempDir][`AsRef<Path>`]as_ref().().joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("a");
352 let b_path = base_dir.as_refrust-analyzer cargo tempdir 0.3.7 impl#[TempDir][`AsRef<Path>`]as_ref().().joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("b");
353
354 fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::create_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/create_dir().(&a_path).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
355 fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::create_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/create_dir().(&b_path).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
356
357 dir.createrust-analyzer cargo copy_dir 0.1.3 tests/impl#[`DirMaker<'a>`]create().(&a_path).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
358 dir.createrust-analyzer cargo copy_dir 0.1.3 tests/impl#[`DirMaker<'a>`]create().(&b_path).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
359
360 assert_dirs_samerust-analyzer cargo copy_dir 0.1.3 tests/assert_dirs_same().(&a_path, &b_path);
361 }
362
363 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
364 #[should_panic]
365 fn assert_dirs_same_properly_failsrust-analyzer cargo copy_dir 0.1.3 tests/assert_dirs_same_properly_fails().() {
366 let dir = Dirrust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#Dir#("foobar", vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![Filerust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#File#("bar"), Dirrust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#Dir#("baz", Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().())]);
367
368 let dir2 = Dirrust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#Dir#("foobar", vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![Filerust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#File#("fobe"), Filerust-analyzer cargo copy_dir 0.1.3 tests/DirMaker#File#("beez")]);
369
370 let base_dir = tempdirrust-analyzer cargo tempdir 0.3.7 crate/::TempDirrust-analyzer cargo tempdir 0.3.7 TempDir#::newrust-analyzer cargo tempdir 0.3.7 impl#[TempDir]new().("copy_dir_test").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
371
372 let a_path = base_dir.as_refrust-analyzer cargo tempdir 0.3.7 impl#[TempDir][`AsRef<Path>`]as_ref().().joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("a");
373 let b_path = base_dir.as_refrust-analyzer cargo tempdir 0.3.7 impl#[TempDir][`AsRef<Path>`]as_ref().().joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("b");
374
375 fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::create_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/create_dir().(&a_path).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
376 fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::create_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/create_dir().(&b_path).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
377
378 dir.createrust-analyzer cargo copy_dir 0.1.3 tests/impl#[`DirMaker<'a>`]create().(&a_path).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
379 dir2.createrust-analyzer cargo copy_dir 0.1.3 tests/impl#[`DirMaker<'a>`]create().(&b_path).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
380
381 assert_dirs_samerust-analyzer cargo copy_dir 0.1.3 tests/assert_dirs_same().(&a_path, &b_path);
382 }
383 }