This repository was archived by the owner on Sep 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathutil.rs
More file actions
256 lines (236 loc) · 7.37 KB
/
util.rs
File metadata and controls
256 lines (236 loc) · 7.37 KB
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
// -*- coding: utf-8 -*-
// ------------------------------------------------------------------------------------------------
// Copyright © 2021, stack-graphs authors.
// Licensed under either of Apache License, Version 2.0, or MIT license, at your option.
// Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details.
// ------------------------------------------------------------------------------------------------
use anyhow::anyhow;
use colored::Colorize;
use std::ffi::OsStr;
use std::ffi::OsString;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
use std::time::Duration;
#[cfg(debug_assertions)]
use std::time::Instant;
use tree_sitter_graph::parse_error::TreeWithParseErrorVec;
use crate::cli::MAX_PARSE_ERRORS;
pub fn path_exists(path: &OsStr) -> anyhow::Result<PathBuf> {
let path = PathBuf::from(path);
if !path.exists() {
return Err(anyhow!("path does not exist"));
}
Ok(path)
}
/// A path specification that can be formatted into a path based on a root and path
/// contained in that root.
pub struct PathSpec {
spec: String,
}
impl PathSpec {
pub fn format(&self, root: &Path, full_path: &Path) -> PathBuf {
if !full_path.starts_with(root) {
panic!(
"Path {} not contained in root {}",
full_path.display(),
root.display()
);
}
let relative_path = full_path.strip_prefix(root).unwrap();
if relative_path.is_absolute() {
panic!(
"Path {} not relative to root {}",
full_path.display(),
root.display()
);
}
self.format_path(
&self.dir_os_str(Some(root)),
&self.dir_os_str(relative_path.parent()),
relative_path.file_stem(),
relative_path.extension(),
)
}
/// Convert an optional directory path to an OsString representation. If the
/// path is missing or empty, we return `.`.
fn dir_os_str(&self, path: Option<&Path>) -> OsString {
let s = path.map_or("".into(), |p| p.as_os_str().to_os_string());
let s = if s.is_empty() { ".".into() } else { s };
s
}
fn format_path(
&self,
root: &OsStr,
dirs: &OsStr,
name: Option<&OsStr>,
ext: Option<&OsStr>,
) -> PathBuf {
let mut path = OsString::new();
let mut in_placeholder = false;
for c in self.spec.chars() {
if in_placeholder {
in_placeholder = false;
match c {
'%' => path.push("%"),
'd' => {
path.push(dirs);
}
'e' => {
if let Some(ext) = ext {
path.push(".");
path.push(ext);
}
}
'n' => {
if let Some(name) = name {
path.push(name);
}
}
'r' => path.push(root),
c => panic!("Unsupported placeholder '%{}'", c),
}
} else if c == '%' {
in_placeholder = true;
} else {
path.push(c.to_string());
}
}
if in_placeholder {
panic!("Unsupported '%' at end");
}
let path = Path::new(&path);
match crate::functions::path::normalize(&path) {
Some(path) => path,
None => panic!("Cannot normalize '{}'", path.display()),
}
}
}
impl std::str::FromStr for PathSpec {
type Err = clap::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self { spec: s.into() })
}
}
impl From<&str> for PathSpec {
fn from(s: &str) -> Self {
Self { spec: s.into() }
}
}
pub fn map_parse_errors(
test_path: &Path,
parse_errors: &TreeWithParseErrorVec,
source: &str,
prefix: &str,
) -> anyhow::Error {
let mut error = String::new();
let parse_errors = parse_errors.errors();
for parse_error in parse_errors.iter().take(MAX_PARSE_ERRORS) {
let line = parse_error.node().start_position().row;
let column = parse_error.node().start_position().column;
error.push_str(&format!(
"{}{}:{}:{}: {}\n",
prefix,
test_path.display(),
line + 1,
column + 1,
parse_error.display(&source, false)
));
}
if parse_errors.len() > MAX_PARSE_ERRORS {
let more_errors = parse_errors.len() - MAX_PARSE_ERRORS;
error.push_str(&format!(
" {} more parse error{} omitted\n",
more_errors,
if more_errors > 1 { "s" } else { "" },
));
}
anyhow!(error)
}
pub fn duration_from_seconds_str(s: &str) -> Result<Duration, anyhow::Error> {
Ok(Duration::new(s.parse()?, 0))
}
pub struct FileStatusLogger<'a> {
path: &'a Path,
verbose: bool,
path_logged: bool,
#[cfg(debug_assertions)]
processing_started: Option<Instant>,
}
impl<'a> FileStatusLogger<'a> {
pub fn new(path: &'a Path, verbose: bool) -> Self {
Self {
path,
verbose,
path_logged: false,
#[cfg(debug_assertions)]
processing_started: None,
}
}
pub fn processing(&mut self) -> std::io::Result<()> {
#[cfg(debug_assertions)]
{
self.processing_started = Some(Instant::now());
}
if !self.verbose {
return Ok(());
}
self.print_path();
std::io::stdout().flush()
}
pub fn ok(&mut self, status: &str) -> std::io::Result<()> {
if !self.verbose {
return Ok(());
}
self.print_path();
print!("{}", status.green());
#[cfg(debug_assertions)]
self.print_processing_time();
println!();
self.path_logged = false;
std::io::stdout().flush()
}
pub fn info(&mut self, status: &str) -> std::io::Result<()> {
if !self.verbose {
return Ok(());
}
self.print_path();
print!("{}", status.dimmed());
#[cfg(debug_assertions)]
self.print_processing_time();
println!();
self.path_logged = false;
std::io::stdout().flush()
}
pub fn warn(&mut self, status: &str) -> std::io::Result<()> {
self.print_path();
print!("{}", status.yellow());
#[cfg(debug_assertions)]
self.print_processing_time();
println!();
self.path_logged = false;
std::io::stdout().flush()
}
pub fn error(&mut self, status: &str) -> std::io::Result<()> {
self.print_path();
print!("{}", status.red());
#[cfg(debug_assertions)]
self.print_processing_time();
println!();
self.path_logged = false;
std::io::stdout().flush()
}
fn print_path(&mut self) {
if self.path_logged {
return;
}
print!("{}: ", self.path.display());
self.path_logged = true;
}
#[cfg(debug_assertions)]
fn print_processing_time(&mut self) {
if let Some(processing_started) = self.processing_started {
print!(" [{:.2} s]", processing_started.elapsed().as_secs_f64());
}
}
}