Skip to content

Commit 01d0ab7

Browse files
style: cargo fmt (fs fast paths, types --install, its test)
Formatting-only: rustfmt the let-chains in system_fs's small-file fast paths (un-formatted since they were added) and the new install_types/update_tsconfig code and its test.
1 parent 303dd46 commit 01d0ab7

3 files changed

Lines changed: 58 additions & 42 deletions

File tree

crates/default-providers/src/system_fs.rs

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,12 @@ impl FileSystem for SystemFileSystem {
136136
let resolved = self.jailed(&path);
137137
if let Ok(p) = &resolved
138138
&& let Ok(md) = std::fs::metadata(p)
139-
&& md.len() < 64 * 1024 {
140-
return Box::pin(std::future::ready(
141-
std::fs::read(p).map_err(|e| other(&path, e)),
142-
));
143-
}
139+
&& md.len() < 64 * 1024
140+
{
141+
return Box::pin(std::future::ready(
142+
std::fs::read(p).map_err(|e| other(&path, e)),
143+
));
144+
}
144145
Box::pin(async move {
145146
let p = resolved?;
146147
tokio::fs::read(&p).await.map_err(|e| other(&path, e))
@@ -157,24 +158,25 @@ impl FileSystem for SystemFileSystem {
157158
let len = data.len() as u64;
158159

159160
if let Ok(p) = &resolved
160-
&& len < 64 * 1024 {
161-
let res = (|| -> std::io::Result<()> {
162-
let mut opts = std::fs::OpenOptions::new();
163-
opts.write(true).create(true);
164-
if append {
165-
opts.append(true);
166-
} else {
167-
opts.truncate(true);
168-
}
169-
use std::io::Write;
170-
let mut f = opts.open(p)?;
171-
f.write_all(&data)?;
172-
Ok(())
173-
})();
174-
return Box::pin(std::future::ready(
175-
res.map(|_| len).map_err(|e| other(&path, e)),
176-
));
177-
}
161+
&& len < 64 * 1024
162+
{
163+
let res = (|| -> std::io::Result<()> {
164+
let mut opts = std::fs::OpenOptions::new();
165+
opts.write(true).create(true);
166+
if append {
167+
opts.append(true);
168+
} else {
169+
opts.truncate(true);
170+
}
171+
use std::io::Write;
172+
let mut f = opts.open(p)?;
173+
f.write_all(&data)?;
174+
Ok(())
175+
})();
176+
return Box::pin(std::future::ready(
177+
res.map(|_| len).map_err(|e| other(&path, e)),
178+
));
179+
}
178180
Box::pin(async move {
179181
let p = resolved?;
180182
let mut opts = tokio::fs::OpenOptions::new();
@@ -193,18 +195,19 @@ impl FileSystem for SystemFileSystem {
193195
fn stat(&self, path: String) -> BoxFuture<Result<FileStat, ProviderError>> {
194196
let resolved = self.jailed(&path);
195197
if let Ok(p) = &resolved
196-
&& let Ok(md) = std::fs::metadata(p) {
197-
let is_symlink = std::fs::symlink_metadata(p)
198-
.map(|m| m.file_type().is_symlink())
199-
.unwrap_or(false);
200-
return Box::pin(std::future::ready(Ok(FileStat {
201-
size: md.len(),
202-
is_file: md.is_file(),
203-
is_dir: md.is_dir(),
204-
is_symlink,
205-
mtime_ms: mtime_ms(&md),
206-
})));
207-
}
198+
&& let Ok(md) = std::fs::metadata(p)
199+
{
200+
let is_symlink = std::fs::symlink_metadata(p)
201+
.map(|m| m.file_type().is_symlink())
202+
.unwrap_or(false);
203+
return Box::pin(std::future::ready(Ok(FileStat {
204+
size: md.len(),
205+
is_file: md.is_file(),
206+
is_dir: md.is_dir(),
207+
is_symlink,
208+
mtime_ms: mtime_ms(&md),
209+
})));
210+
}
208211
Box::pin(async move {
209212
let p = resolved?;
210213
let md = tokio::fs::metadata(&p).await.map_err(|e| other(&path, e))?;

crates/runtime-cli/src/main.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,17 @@ fn update_tsconfig() -> Result<String, Box<dyn std::error::Error>> {
172172
"tsconfig.json is not a JSON object — left it untouched.\n{manual}"
173173
));
174174
};
175-
let co = obj
176-
.entry("compilerOptions")
177-
.or_insert_with(|| json!({}));
175+
let co = obj.entry("compilerOptions").or_insert_with(|| json!({}));
178176
let Some(co) = co.as_object_mut() else {
179177
return Ok(format!(
180178
"tsconfig.json compilerOptions is not an object — left it untouched.\n{manual}"
181179
));
182180
};
183-
merge_str_array(co, "typeRoots", &["node_modules/@types", "node_modules/@opentf"]);
181+
merge_str_array(
182+
co,
183+
"typeRoots",
184+
&["node_modules/@types", "node_modules/@opentf"],
185+
);
184186
merge_str_array(co, "types", &["esrun"]);
185187
fs::write(path, format!("{}\n", serde_json::to_string_pretty(&cfg)?))?;
186188
Ok("Updated tsconfig.json (typeRoots + types).".into())

crates/runtime-cli/tests/modules.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,11 +432,18 @@ fn types_install_writes_package_and_wires_tsconfig() {
432432
let dts = dir.join("node_modules/@opentf/esrun/index.d.ts");
433433
assert!(dts.exists(), "index.d.ts not written");
434434
assert!(dir.join("node_modules/@opentf/esrun/package.json").exists());
435-
assert!(std::fs::read_to_string(&dts).unwrap().contains("declare module \"runtime:fs\""));
435+
assert!(
436+
std::fs::read_to_string(&dts)
437+
.unwrap()
438+
.contains("declare module \"runtime:fs\"")
439+
);
436440

437441
// tsconfig.json is created and wired up (typeRoots + types).
438442
let ts = std::fs::read_to_string(dir.join("tsconfig.json")).unwrap();
439-
assert!(ts.contains("node_modules/@opentf"), "typeRoots missing:\n{ts}");
443+
assert!(
444+
ts.contains("node_modules/@opentf"),
445+
"typeRoots missing:\n{ts}"
446+
);
440447
assert!(ts.contains("\"esrun\""), "types entry missing:\n{ts}");
441448

442449
// Re-running is idempotent — `esrun` isn't duplicated in `types`.
@@ -448,7 +455,11 @@ fn types_install_writes_package_and_wires_tsconfig() {
448455
.expect("spawn esrun");
449456
assert!(out2.status.success());
450457
let ts2 = std::fs::read_to_string(dir.join("tsconfig.json")).unwrap();
451-
assert_eq!(ts2.matches("\"esrun\"").count(), 1, "esrun duplicated:\n{ts2}");
458+
assert_eq!(
459+
ts2.matches("\"esrun\"").count(),
460+
1,
461+
"esrun duplicated:\n{ts2}"
462+
);
452463

453464
let _ = std::fs::remove_dir_all(&dir);
454465
}

0 commit comments

Comments
 (0)