-
-
Notifications
You must be signed in to change notification settings - Fork 748
/
fs.rs
697 lines (601 loc) · 21.2 KB
/
fs.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
use gleam_core::{
build::{NullTelemetry, Target},
error::{Error, FileIoAction, FileKind},
io::{
CommandExecutor, Content, DirEntry, FileSystemReader, FileSystemWriter, OutputFile,
ReadDir, Stdio, WrappedReader,
},
language_server::{DownloadDependencies, Locker, MakeLocker},
manifest::Manifest,
paths::ProjectPaths,
warning::WarningEmitterIO,
Result, Warning,
};
use std::{
ffi::OsStr,
fmt::Debug,
fs::File,
io::{self, BufRead, BufReader, Write},
sync::OnceLock,
time::SystemTime,
};
use camino::{ReadDirUtf8, Utf8Path, Utf8PathBuf};
use crate::{dependencies::UseManifest, lsp::LspLocker};
#[cfg(test)]
mod tests;
/// Return the current directory as a UTF-8 Path
pub fn get_current_directory() -> Result<Utf8PathBuf, Error> {
let curr_dir = std::env::current_dir().map_err(|e| Error::FileIo {
kind: FileKind::Directory,
action: FileIoAction::Open,
path: ".".into(),
err: Some(e.to_string()),
})?;
Utf8PathBuf::from_path_buf(curr_dir.clone()).map_err(|_| Error::NonUtf8Path { path: curr_dir })
}
// Return the first directory with a gleam.toml as a UTF-8 Path
pub fn get_project_root(path: Utf8PathBuf) -> Result<Utf8PathBuf, Error> {
fn walk(dir: Utf8PathBuf) -> Option<Utf8PathBuf> {
match dir.join("gleam.toml").is_file() {
true => Some(dir),
false => match dir.parent() {
Some(p) => walk(p.into()),
None => None,
},
}
}
walk(path.clone()).ok_or(Error::UnableToFindProjectRoot {
path: path.to_string(),
})
}
/// A `FileWriter` implementation that writes to the file system.
#[derive(Debug, Clone, Copy)]
pub struct ProjectIO;
impl ProjectIO {
pub fn new() -> Self {
Self
}
pub fn boxed() -> Box<Self> {
Box::new(Self::new())
}
}
impl FileSystemReader for ProjectIO {
fn gleam_source_files(&self, dir: &Utf8Path) -> Vec<Utf8PathBuf> {
if !dir.is_dir() {
return vec![];
}
let dir = dir.to_path_buf();
walkdir::WalkDir::new(dir)
.follow_links(true)
.into_iter()
.filter_map(Result::ok)
.filter(|e| e.file_type().is_file())
.map(|d| d.into_path())
.filter(move |d| d.extension() == Some(OsStr::new("gleam")))
.map(|pb| Utf8PathBuf::from_path_buf(pb).expect("Non Utf-8 Path"))
.collect()
}
fn gleam_cache_files(&self, dir: &Utf8Path) -> Vec<Utf8PathBuf> {
if !dir.is_dir() {
return vec![];
}
let dir = dir.to_path_buf();
walkdir::WalkDir::new(dir)
.follow_links(true)
.into_iter()
.filter_map(Result::ok)
.filter(|e| e.file_type().is_file())
.map(|d| d.into_path())
.filter(|p| p.extension().and_then(OsStr::to_str) == Some("cache"))
.map(|pb| Utf8PathBuf::from_path_buf(pb).expect("Non Utf-8 Path"))
.collect()
}
fn read(&self, path: &Utf8Path) -> Result<String, Error> {
read(path)
}
fn read_bytes(&self, path: &Utf8Path) -> Result<Vec<u8>, Error> {
read_bytes(path)
}
fn is_file(&self, path: &Utf8Path) -> bool {
path.is_file()
}
fn is_directory(&self, path: &Utf8Path) -> bool {
path.is_dir()
}
fn reader(&self, path: &Utf8Path) -> Result<WrappedReader, Error> {
reader(path)
}
fn read_dir(&self, path: &Utf8Path) -> Result<ReadDir> {
read_dir(path).map(|entries| {
entries
.map(|result| result.map(|entry| DirEntry::from_path(entry.path())))
.collect()
})
}
fn modification_time(&self, path: &Utf8Path) -> Result<SystemTime, Error> {
path.metadata()
.map(|m| m.modified().unwrap_or_else(|_| SystemTime::now()))
.map_err(|e| Error::FileIo {
action: FileIoAction::ReadMetadata,
kind: FileKind::File,
path: path.to_path_buf(),
err: Some(e.to_string()),
})
}
fn canonicalise(&self, path: &Utf8Path) -> Result<Utf8PathBuf, Error> {
canonicalise(path)
}
}
impl FileSystemWriter for ProjectIO {
fn delete_directory(&self, path: &Utf8Path) -> Result<()> {
delete_directory(path)
}
fn copy(&self, from: &Utf8Path, to: &Utf8Path) -> Result<()> {
copy(from, to)
}
fn copy_dir(&self, from: &Utf8Path, to: &Utf8Path) -> Result<()> {
copy_dir(from, to)
}
fn mkdir(&self, path: &Utf8Path) -> Result<(), Error> {
mkdir(path)
}
fn hardlink(&self, from: &Utf8Path, to: &Utf8Path) -> Result<(), Error> {
hardlink(from, to)
}
fn symlink_dir(&self, from: &Utf8Path, to: &Utf8Path) -> Result<(), Error> {
symlink_dir(from, to)
}
fn delete_file(&self, path: &Utf8Path) -> Result<()> {
delete_file(path)
}
fn write(&self, path: &Utf8Path, content: &str) -> Result<(), Error> {
write(path, content)
}
fn write_bytes(&self, path: &Utf8Path, content: &[u8]) -> Result<(), Error> {
write_bytes(path, content)
}
}
impl CommandExecutor for ProjectIO {
fn exec(
&self,
program: &str,
args: &[String],
env: &[(&str, String)],
cwd: Option<&Utf8Path>,
stdio: Stdio,
) -> Result<i32, Error> {
tracing::trace!(program=program, args=?args.join(" "), env=?env, cwd=?cwd, "command_exec");
let result = std::process::Command::new(program)
.args(args)
.stdin(stdio.get_process_stdio())
.stdout(stdio.get_process_stdio())
.envs(env.iter().map(|pair| (pair.0, &pair.1)))
.current_dir(cwd.unwrap_or_else(|| Utf8Path::new("./")))
.status();
match result {
Ok(status) => Ok(status.code().unwrap_or_default()),
Err(error) => Err(match error.kind() {
io::ErrorKind::NotFound => Error::ShellProgramNotFound {
program: program.to_string(),
},
other => Error::ShellCommand {
program: program.to_string(),
err: Some(other),
},
}),
}
}
}
impl MakeLocker for ProjectIO {
fn make_locker(&self, paths: &ProjectPaths, target: Target) -> Result<Box<dyn Locker>> {
let locker = LspLocker::new(paths, target)?;
Ok(Box::new(locker))
}
}
impl DownloadDependencies for ProjectIO {
fn download_dependencies(&self, paths: &ProjectPaths) -> Result<Manifest> {
crate::dependencies::download(paths, NullTelemetry, None, UseManifest::Yes)
}
}
pub fn delete_directory(dir: &Utf8Path) -> Result<(), Error> {
tracing::trace!(path=?dir, "deleting_directory");
if dir.exists() {
std::fs::remove_dir_all(dir).map_err(|e| Error::FileIo {
action: FileIoAction::Delete,
kind: FileKind::Directory,
path: dir.to_path_buf(),
err: Some(e.to_string()),
})?;
} else {
tracing::trace!(path=?dir, "directory_did_not_exist_for_deletion");
}
Ok(())
}
pub fn delete_file(file: &Utf8Path) -> Result<(), Error> {
tracing::trace!("Deleting file {:?}", file);
if file.exists() {
std::fs::remove_file(file).map_err(|e| Error::FileIo {
action: FileIoAction::Delete,
kind: FileKind::File,
path: file.to_path_buf(),
err: Some(e.to_string()),
})?;
} else {
tracing::trace!("Did not exist for deletion: {:?}", file);
}
Ok(())
}
pub fn write_outputs_under(outputs: &[OutputFile], base: &Utf8Path) -> Result<(), Error> {
for file in outputs {
let path = base.join(&file.path);
match &file.content {
Content::Binary(buffer) => write_bytes(&path, buffer),
Content::Text(buffer) => write(&path, buffer),
}?;
}
Ok(())
}
pub fn write_output(file: &OutputFile) -> Result<(), Error> {
let OutputFile { path, content } = file;
match content {
Content::Binary(buffer) => write_bytes(path, buffer),
Content::Text(buffer) => write(path, buffer),
}
}
pub fn write(path: &Utf8Path, text: &str) -> Result<(), Error> {
write_bytes(path, text.as_bytes())
}
#[cfg(target_family = "unix")]
pub fn make_executable(path: impl AsRef<Utf8Path>) -> Result<(), Error> {
use std::os::unix::fs::PermissionsExt;
tracing::trace!(path = ?path.as_ref(), "setting_permissions");
std::fs::set_permissions(path.as_ref(), std::fs::Permissions::from_mode(0o755)).map_err(
|e| Error::FileIo {
action: FileIoAction::UpdatePermissions,
kind: FileKind::File,
path: path.as_ref().to_path_buf(),
err: Some(e.to_string()),
},
)?;
Ok(())
}
#[cfg(not(target_family = "unix"))]
pub fn make_executable(_path: impl AsRef<Utf8Path>) -> Result<(), Error> {
Ok(())
}
pub fn write_bytes(path: &Utf8Path, bytes: &[u8]) -> Result<(), Error> {
tracing::trace!(path=?path, "writing_file");
let dir_path = path.parent().ok_or_else(|| Error::FileIo {
action: FileIoAction::FindParent,
kind: FileKind::Directory,
path: path.to_path_buf(),
err: None,
})?;
std::fs::create_dir_all(dir_path).map_err(|e| Error::FileIo {
action: FileIoAction::Create,
kind: FileKind::Directory,
path: dir_path.to_path_buf(),
err: Some(e.to_string()),
})?;
let mut f = File::create(path).map_err(|e| Error::FileIo {
action: FileIoAction::Create,
kind: FileKind::File,
path: path.to_path_buf(),
err: Some(e.to_string()),
})?;
f.write_all(bytes).map_err(|e| Error::FileIo {
action: FileIoAction::WriteTo,
kind: FileKind::File,
path: path.to_path_buf(),
err: Some(e.to_string()),
})?;
Ok(())
}
fn is_gleam_path(path: &Utf8Path, dir: impl AsRef<Utf8Path>) -> bool {
use regex::Regex;
static RE: OnceLock<Regex> = OnceLock::new();
RE.get_or_init(|| {
Regex::new(&format!(
"^({module}{slash})*{module}\\.gleam$",
module = "[a-z][_a-z0-9]*",
slash = "(/|\\\\)",
))
.expect("is_gleam_path() RE regex")
})
.is_match(
path.strip_prefix(dir.as_ref())
.expect("is_gleam_path(): strip_prefix")
.as_str(),
)
}
fn is_gleam_build_dir(e: &ignore::DirEntry) -> bool {
if !e.path().is_dir() || !e.path().ends_with("build") {
return false;
}
let Some(parent_path) = e.path().parent() else {
return false;
};
parent_path.join("gleam.toml").exists()
}
pub fn gleam_files_excluding_gitignore(dir: &Utf8Path) -> impl Iterator<Item = Utf8PathBuf> + '_ {
ignore::WalkBuilder::new(dir)
.follow_links(true)
.require_git(false)
.filter_entry(|e| !is_gleam_build_dir(e))
.build()
.filter_map(Result::ok)
.filter(|e| e.file_type().map(|t| t.is_file()).unwrap_or(false))
.map(ignore::DirEntry::into_path)
.map(|pb| Utf8PathBuf::from_path_buf(pb).expect("Non Utf-8 Path"))
.filter(move |d| is_gleam_path(d, dir))
}
pub fn native_files(dir: &Utf8Path) -> Result<impl Iterator<Item = Utf8PathBuf> + '_> {
Ok(read_dir(dir)?
.flat_map(Result::ok)
.map(|e| e.into_path())
.filter(|path| {
let extension = path.extension().unwrap_or_default();
matches!(extension, "erl" | "hrl" | "ex" | "js" | "mjs" | "ts")
}))
}
pub fn private_files_excluding_gitignore(dir: &Utf8Path) -> impl Iterator<Item = Utf8PathBuf> + '_ {
ignore::WalkBuilder::new(dir)
.follow_links(true)
.require_git(false)
.build()
.filter_map(Result::ok)
.filter(|e| e.file_type().map(|t| t.is_file()).unwrap_or(false))
.map(ignore::DirEntry::into_path)
.map(|pb| Utf8PathBuf::from_path_buf(pb).expect("Non Utf-8 Path"))
}
pub fn erlang_files(dir: &Utf8Path) -> Result<impl Iterator<Item = Utf8PathBuf> + '_> {
Ok(read_dir(dir)?
.flat_map(Result::ok)
.map(|e| e.into_path())
.filter(|path| {
let extension = path.extension().unwrap_or_default();
extension == "erl" || extension == "hrl"
}))
}
pub fn create_tar_archive(outputs: Vec<OutputFile>) -> Result<Vec<u8>, Error> {
tracing::trace!("creating_tar_archive");
let encoder = flate2::write::GzEncoder::new(vec![], flate2::Compression::default());
let mut builder = tar::Builder::new(encoder);
for file in outputs {
let mut header = tar::Header::new_gnu();
header.set_path(&file.path).map_err(|e| Error::AddTar {
path: file.path.clone(),
err: e.to_string(),
})?;
header.set_size(file.content.as_bytes().len() as u64);
header.set_cksum();
builder
.append(&header, file.content.as_bytes())
.map_err(|e| Error::AddTar {
path: file.path.clone(),
err: e.to_string(),
})?;
}
builder
.into_inner()
.map_err(|e| Error::TarFinish(e.to_string()))?
.finish()
.map_err(|e| Error::Gzip(e.to_string()))
}
pub fn mkdir(path: impl AsRef<Utf8Path> + Debug) -> Result<(), Error> {
if path.as_ref().exists() {
return Ok(());
}
tracing::trace!(path=?path, "creating_directory");
std::fs::create_dir_all(path.as_ref()).map_err(|err| Error::FileIo {
kind: FileKind::Directory,
path: Utf8PathBuf::from(path.as_ref()),
action: FileIoAction::Create,
err: Some(err.to_string()),
})
}
pub fn read_dir(path: impl AsRef<Utf8Path> + Debug) -> Result<ReadDirUtf8, Error> {
tracing::trace!(path=?path,"reading_directory");
Utf8Path::read_dir_utf8(path.as_ref()).map_err(|e| Error::FileIo {
action: FileIoAction::Read,
kind: FileKind::Directory,
path: Utf8PathBuf::from(path.as_ref()),
err: Some(e.to_string()),
})
}
pub fn module_caches_paths(
path: impl AsRef<Utf8Path> + Debug,
) -> Result<impl Iterator<Item = Utf8PathBuf>, Error> {
Ok(read_dir(path)?
.filter_map(Result::ok)
.map(|f| f.into_path())
.filter(|p| p.extension() == Some("cache")))
}
pub fn read(path: impl AsRef<Utf8Path> + Debug) -> Result<String, Error> {
tracing::trace!(path=?path,"reading_file");
std::fs::read_to_string(path.as_ref()).map_err(|err| Error::FileIo {
action: FileIoAction::Read,
kind: FileKind::File,
path: Utf8PathBuf::from(path.as_ref()),
err: Some(err.to_string()),
})
}
pub fn read_bytes(path: impl AsRef<Utf8Path> + Debug) -> Result<Vec<u8>, Error> {
tracing::trace!(path=?path,"reading_file");
std::fs::read(path.as_ref()).map_err(|err| Error::FileIo {
action: FileIoAction::Read,
kind: FileKind::File,
path: Utf8PathBuf::from(path.as_ref()),
err: Some(err.to_string()),
})
}
pub fn reader(path: impl AsRef<Utf8Path> + Debug) -> Result<WrappedReader, Error> {
tracing::trace!(path=?path,"opening_file_reader");
let reader = File::open(path.as_ref()).map_err(|err| Error::FileIo {
action: FileIoAction::Open,
kind: FileKind::File,
path: Utf8PathBuf::from(path.as_ref()),
err: Some(err.to_string()),
})?;
Ok(WrappedReader::new(path.as_ref(), Box::new(reader)))
}
pub fn buffered_reader<P: AsRef<Utf8Path> + Debug>(path: P) -> Result<impl BufRead, Error> {
tracing::trace!(path=?path,"opening_file_buffered_reader");
let reader = File::open(path.as_ref()).map_err(|err| Error::FileIo {
action: FileIoAction::Open,
kind: FileKind::File,
path: Utf8PathBuf::from(path.as_ref()),
err: Some(err.to_string()),
})?;
Ok(BufReader::new(reader))
}
pub fn copy(
path: impl AsRef<Utf8Path> + Debug,
to: impl AsRef<Utf8Path> + Debug,
) -> Result<(), Error> {
tracing::trace!(from=?path, to=?to, "copying_file");
// TODO: include the destination in the error message
std::fs::copy(path.as_ref(), to.as_ref())
.map_err(|err| Error::FileIo {
action: FileIoAction::Copy,
kind: FileKind::File,
path: Utf8PathBuf::from(path.as_ref()),
err: Some(err.to_string()),
})
.map(|_| ())
}
// pub fn rename(path: impl AsRef<Utf8Path> + Debug, to: impl AsRef<Utf8Path> + Debug) -> Result<(), Error> {
// tracing::trace!(from=?path, to=?to, "renaming_file");
// // TODO: include the destination in the error message
// std::fs::rename(&path, &to)
// .map_err(|err| Error::FileIo {
// action: FileIoAction::Rename,
// kind: FileKind::File,
// path: Utf8PathBuf::from(path.as_ref()),
// err: Some(err.to_string()),
// })
// .map(|_| ())
// }
pub fn copy_dir(
path: impl AsRef<Utf8Path> + Debug,
to: impl AsRef<Utf8Path> + Debug,
) -> Result<(), Error> {
tracing::trace!(from=?path, to=?to, "copying_directory");
// TODO: include the destination in the error message
fs_extra::dir::copy(
path.as_ref(),
to.as_ref(),
&fs_extra::dir::CopyOptions::new()
.copy_inside(false)
.content_only(true),
)
.map_err(|err| Error::FileIo {
action: FileIoAction::Copy,
kind: FileKind::Directory,
path: Utf8PathBuf::from(path.as_ref()),
err: Some(err.to_string()),
})
.map(|_| ())
}
pub fn symlink_dir(
src: impl AsRef<Utf8Path> + Debug,
dest: impl AsRef<Utf8Path> + Debug,
) -> Result<(), Error> {
tracing::trace!(src=?src, dest=?dest, "symlinking");
let src = canonicalise(src.as_ref())?;
#[cfg(target_family = "windows")]
let result = std::os::windows::fs::symlink_dir(src, dest.as_ref());
#[cfg(not(target_family = "windows"))]
let result = std::os::unix::fs::symlink(src, dest.as_ref());
result.map_err(|err| Error::FileIo {
action: FileIoAction::Link,
kind: FileKind::File,
path: Utf8PathBuf::from(dest.as_ref()),
err: Some(err.to_string()),
})?;
Ok(())
}
pub fn hardlink(
from: impl AsRef<Utf8Path> + Debug,
to: impl AsRef<Utf8Path> + Debug,
) -> Result<(), Error> {
tracing::trace!(from=?from, to=?to, "hardlinking");
std::fs::hard_link(from.as_ref(), to.as_ref())
.map_err(|err| Error::FileIo {
action: FileIoAction::Link,
kind: FileKind::File,
path: Utf8PathBuf::from(from.as_ref()),
err: Some(err.to_string()),
})
.map(|_| ())
}
/// Check if the given path is inside a git work tree.
/// This is done by running `git rev-parse --is-inside-work-tree --quiet` in the
/// given path. If git is not installed then we assume we're not in a git work
/// tree.
///
pub fn is_inside_git_work_tree(path: &Utf8Path) -> Result<bool, Error> {
tracing::trace!(path=?path, "checking_for_git_repo");
let args: Vec<&str> = vec!["rev-parse", "--is-inside-work-tree", "--quiet"];
// Ignore all output, rely on the exit code instead.
// git will display a fatal error on stderr if rev-parse isn't run inside of a git work tree,
// so send stderr to /dev/null
let result = std::process::Command::new("git")
.args(args)
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.current_dir(path)
.status();
match result {
Ok(status) => Ok(status.success()),
Err(error) => match error.kind() {
io::ErrorKind::NotFound => Ok(false),
other => Err(Error::ShellCommand {
program: "git".into(),
err: Some(other),
}),
},
}
}
/// Run `git init` in the given path.
/// If git is not installed then we do nothing.
pub fn git_init(path: &Utf8Path) -> Result<(), Error> {
tracing::trace!(path=?path, "initializing git");
if is_inside_git_work_tree(path)? {
tracing::trace!(path=?path, "git_repo_already_exists");
return Ok(());
}
let args = vec!["init".into(), "--quiet".into(), path.to_string()];
match ProjectIO::new().exec("git", &args, &[], None, Stdio::Inherit) {
Ok(_) => Ok(()),
Err(err) => match err {
Error::ShellProgramNotFound { .. } => Ok(()),
_ => Err(Error::GitInitialization {
error: err.to_string(),
}),
},
}
}
pub fn canonicalise(path: &Utf8Path) -> Result<Utf8PathBuf, Error> {
std::fs::canonicalize(path)
.map_err(|err| Error::FileIo {
action: FileIoAction::Canonicalise,
kind: FileKind::File,
path: Utf8PathBuf::from(path),
err: Some(err.to_string()),
})
.map(|pb| Utf8PathBuf::from_path_buf(pb).expect("Non Utf8 Path"))
}
#[derive(Debug, Clone, Copy)]
pub struct ConsoleWarningEmitter;
impl WarningEmitterIO for ConsoleWarningEmitter {
fn emit_warning(&self, warning: Warning) {
let buffer_writer = crate::cli::stderr_buffer_writer();
let mut buffer = buffer_writer.buffer();
warning.pretty(&mut buffer);
buffer_writer
.print(&buffer)
.expect("Writing warning to stderr");
}
}