-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathbzl.rs
More file actions
175 lines (151 loc) · 5.38 KB
/
Copy pathbzl.rs
File metadata and controls
175 lines (151 loc) · 5.38 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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is dual-licensed under either the MIT license found in the
* LICENSE-MIT file in the root directory of this source tree or the Apache
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
* of this source tree. You may select, at your option, one of the
* above-listed licenses.
*/
use std::fmt::Display;
use std::fmt::Formatter;
use allocative::Allocative;
use buck2_fs::paths::file_name::FileName;
use pagable::Pagable;
use strong_hash::StrongHash;
use crate::cells::build_file_cell::BuildFileCell;
use crate::cells::cell_path::CellPath;
use crate::cells::cell_path::CellPathRef;
use crate::cells::name::CellName;
use crate::cells::paths::CellRelativePath;
#[derive(Debug, buck2_error::Error)]
#[buck2(input)]
enum ImportPathError {
#[error("Invalid import path `{0}`")]
Invalid(CellPath),
#[error("Import path must have suffix `.bzl`, `.json`, or `.toml`: `{0}`")]
Suffix(CellPath),
}
/// Path of a `.bzl` file.
#[derive(Clone, Hash, StrongHash, Eq, PartialEq, Debug, Allocative, Pagable)]
pub struct ImportPath {
/// The path to the import as a 'CellPath', which contains the cell
/// information and the cell relative path to the bzl file itself, including the bzl suffix
path: CellPath,
/// The cell of the top-level build module that this is being loaded
/// (perhaps transitively) into.
build_file_cell: BuildFileCell,
}
impl ImportPath {
/// We evaluate `bzl` files multiple times: for each cell we evaluate `bzl` file again.
/// We want to stop doing that.
/// This function is for call sites where we don't care about the build file cell.
pub fn new_same_cell(path: CellPath) -> buck2_error::Result<Self> {
let build_file_cell = BuildFileCell::new(path.cell());
Self::new_with_build_file_cells(path, build_file_cell)
}
pub fn new_with_build_file_cells(
path: CellPath,
build_file_cell: BuildFileCell,
) -> buck2_error::Result<Self> {
if path.parent().is_none() {
return Err(ImportPathError::Invalid(path).into());
}
if path.path().as_str().contains('?') {
return Err(ImportPathError::Invalid(path).into());
}
if !matches!(path.path().extension(), Some("bzl" | "json" | "toml")) {
return Err(ImportPathError::Suffix(path).into());
}
Ok(Self {
path,
build_file_cell,
})
}
/// Create an ImportPath for a file with an explicit format override (e.g. `?as=toml`).
/// Skips the file extension validation since the format is determined by the hint,
/// not the extension.
///
/// Do not include the ?as=toml hint as part of the path.
pub fn new_with_explicit_format(
path: CellPath,
build_file_cell: BuildFileCell,
) -> buck2_error::Result<Self> {
if path.parent().is_none() {
return Err(ImportPathError::Invalid(path).into());
}
if path.path().as_str().contains('?') {
return Err(ImportPathError::Invalid(path).into());
}
Ok(Self {
path,
build_file_cell,
})
}
pub fn new_same_cell_with_explicit_format(path: CellPath) -> buck2_error::Result<Self> {
let build_file_cell = BuildFileCell::new(path.cell());
Self::new_with_explicit_format(path, build_file_cell)
}
/// LSP creates imports for non-bzl files.
pub fn new_hack_for_lsp(
path: CellPath,
build_file_cell: BuildFileCell,
) -> buck2_error::Result<Self> {
if path.parent().is_none() {
return Err(ImportPathError::Invalid(path).into());
}
if path.path().as_str().contains('?') {
return Err(ImportPathError::Invalid(path).into());
}
Ok(Self {
path,
build_file_cell,
})
}
pub fn testing_new(path: &str) -> Self {
let (cell, rem) = path.split_once("//").unwrap();
let (cell_relative_path, filename) = rem.rsplit_once(':').unwrap();
Self::testing_new_cross_cell(cell, cell_relative_path, filename, cell)
}
pub fn testing_new_cross_cell(
cell: &str,
cell_relative_path: &str,
filename: &str,
build_file_cell: &str,
) -> Self {
let cell_path = CellPath::new(
CellName::testing_new(cell),
CellRelativePath::unchecked_new(cell_relative_path)
.join(FileName::unchecked_new(filename)),
);
Self::new_with_build_file_cells(
cell_path,
BuildFileCell::new(CellName::testing_new(build_file_cell)),
)
.unwrap()
}
pub fn cell(&self) -> CellName {
self.path.cell()
}
pub fn build_file_cell(&self) -> BuildFileCell {
self.build_file_cell
}
pub fn path(&self) -> &CellPath {
&self.path
}
/// Parent directory of the import path.
pub fn path_parent(&self) -> CellPathRef<'_> {
self.path
.parent()
.expect("constructor verified path has parent")
}
}
impl Display for ImportPath {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if self.build_file_cell.name() == self.path.cell() {
write!(f, "{}", self.path)
} else {
write!(f, "{}@{}", self.path, self.build_file_cell.name())
}
}
}