Skip to content

Commit d8eb5d4

Browse files
committed
Rust: run cargo fmt
1 parent 60b349f commit d8eb5d4

7 files changed

Lines changed: 108 additions & 68 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

rust/extractor/src/archive.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use std::path::{Path, PathBuf};
2-
use std::fs;
31
use crate::path;
42
use anyhow;
53
use log::{debug, warn};
4+
use std::fs;
5+
use std::path::{Path, PathBuf};
66

77
pub struct Archiver {
8-
pub root: PathBuf
8+
pub root: PathBuf,
99
}
1010

1111
impl Archiver {
@@ -20,7 +20,7 @@ impl Archiver {
2020
dest.push(path::key(source));
2121
let parent = dest.parent().unwrap();
2222
if fs::metadata(&dest).is_ok() {
23-
return Ok(())
23+
return Ok(());
2424
}
2525
fs::create_dir_all(parent)?;
2626
fs::copy(source, dest)?;

rust/extractor/src/config.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
use std::path::PathBuf;
21
use anyhow::Context;
3-
use serde::{Deserialize, Serialize, Serializer, Deserializer};
4-
use serde_with;
5-
use figment::{Figment, providers::{Env, Serialized}};
6-
use clap::{Parser, ArgAction, ValueEnum};
72
use clap::builder::PossibleValue;
3+
use clap::{ArgAction, Parser, ValueEnum};
84
use codeql_extractor::trap;
5+
use figment::{
6+
providers::{Env, Serialized},
7+
Figment,
8+
};
9+
use serde::{Deserialize, Deserializer, Serialize, Serializer};
10+
use serde_with;
11+
use std::path::PathBuf;
912

1013
#[derive(Debug, PartialEq, Eq, Default, Serialize, Deserialize, Clone, Copy, ValueEnum)]
1114
#[serde(rename_all = "lowercase")]
@@ -65,11 +68,14 @@ impl Config {
6568
let mut cli_args = CliArgs::parse();
6669
if let Some(inputs_file) = cli_args.inputs_file.take() {
6770
let inputs_list = std::fs::read_to_string(inputs_file).context("reading file list")?;
68-
cli_args.inputs.extend(inputs_list.split("\n").map(PathBuf::from));
71+
cli_args
72+
.inputs
73+
.extend(inputs_list.split("\n").map(PathBuf::from));
6974
}
7075
Ok(Figment::new()
7176
.merge(Env::prefixed("CODEQL_EXTRACTOR_RUST_"))
7277
.merge(Serialized::defaults(cli_args))
73-
.extract().context("loading configuration")?)
78+
.extract()
79+
.context("loading configuration")?)
7480
}
7581
}

rust/extractor/src/main.rs

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
use std::fs;
2-
use std::path::{PathBuf, Path};
3-
use ra_ap_project_model::CargoConfig;
1+
use crate::trap::TrapId;
42
use anyhow::Context;
53
use log;
64
use ra_ap_hir::db::DefDatabase;
7-
use ra_ap_hir::{Crate, ModuleDefId};
8-
use ra_ap_hir::AdtId::{EnumId, UnionId, StructId};
95
use ra_ap_hir::sym::ge;
6+
use ra_ap_hir::AdtId::{EnumId, StructId, UnionId};
7+
use ra_ap_hir::{Crate, ModuleDefId};
108
use ra_ap_load_cargo::{load_workspace_at, LoadCargoConfig, ProcMacroServerChoice};
11-
use crate::trap::{TrapId};
9+
use ra_ap_project_model::CargoConfig;
10+
use std::fs;
11+
use std::path::{Path, PathBuf};
1212

13+
mod archive;
1314
mod config;
14-
pub mod trap;
1515
pub mod generated;
16-
mod translate;
17-
mod archive;
1816
pub mod path;
17+
mod translate;
18+
pub mod trap;
1919

2020
fn main() -> anyhow::Result<()> {
2121
let cfg = config::Config::extract().context("failed to load configuration")?;
@@ -25,30 +25,42 @@ fn main() -> anyhow::Result<()> {
2525
.init()?;
2626
log::info!("{cfg:?}");
2727
let traps = trap::TrapFileProvider::new(&cfg).context("failed to set up trap files")?;
28-
let archiver = archive::Archiver { root: cfg.source_archive_dir };
28+
let archiver = archive::Archiver {
29+
root: cfg.source_archive_dir,
30+
};
2931

30-
let config = CargoConfig { ..Default::default() };
32+
let config = CargoConfig {
33+
..Default::default()
34+
};
3135
let no_progress = |_| ();
3236
let load_config = LoadCargoConfig {
3337
load_out_dirs_from_check: true,
3438
with_proc_macro_server: ProcMacroServerChoice::Sysroot,
3539
prefill_caches: false,
3640
};
3741
for input in cfg.inputs {
38-
let (db, vfs, _macro_server) = load_workspace_at(&input, &config, &load_config, &no_progress).context("loading inputs")?;
39-
let crates = <dyn DefDatabase>::crate_graph(&db);
42+
let (db, vfs, _macro_server) =
43+
load_workspace_at(&input, &config, &load_config, &no_progress)
44+
.context("loading inputs")?;
45+
let crates = <dyn DefDatabase>::crate_graph(&db);
4046
for crate_id in crates.iter() {
4147
let krate = Crate::from(crate_id);
4248
let name = krate.display_name(&db);
43-
let crate_name = name.as_ref().map(|n| n.canonical_name().as_str()).unwrap_or("");
44-
let trap = traps.create("crates", &PathBuf::from(format!("/{}_{}", crate_name, crate_id.into_raw().into_u32())));
45-
translate::CrateTranslator::new(
46-
&db,
47-
trap,
48-
&krate,
49-
&vfs,
50-
&archiver,
51-
).emit_crate().context("writing trap file")?;
49+
let crate_name = name
50+
.as_ref()
51+
.map(|n| n.canonical_name().as_str())
52+
.unwrap_or("");
53+
let trap = traps.create(
54+
"crates",
55+
&PathBuf::from(format!(
56+
"/{}_{}",
57+
crate_name,
58+
crate_id.into_raw().into_u32()
59+
)),
60+
);
61+
translate::CrateTranslator::new(&db, trap, &krate, &vfs, &archiver)
62+
.emit_crate()
63+
.context("writing trap file")?;
5264
}
5365
}
5466
Ok(())

rust/extractor/src/path.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
use std::path::{absolute, Path, PathBuf};
21
use std::fs::canonicalize;
2+
use std::path::{absolute, Path, PathBuf};
33

44
pub fn key(p: &Path) -> PathBuf {
5-
let normalized = canonicalize(p).or_else(|_| absolute(p)).unwrap_or_else(|_| p.into());
6-
let root = normalized.ancestors().last().unwrap(); // ancestors always yields at least one
7-
normalized.strip_prefix(root).unwrap().into() // stripping an ancestor always works
5+
let normalized = canonicalize(p)
6+
.or_else(|_| absolute(p))
7+
.unwrap_or_else(|_| p.into());
8+
let root = normalized.ancestors().last().unwrap(); // ancestors always yields at least one
9+
normalized.strip_prefix(root).unwrap().into() // stripping an ancestor always works
810
}

rust/extractor/src/translate.rs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
use std::collections::HashMap;
2-
use std::fs;
3-
use std::path::{PathBuf};
4-
use crate::trap::{TrapFile, TrapId, AsTrapKeyPart};
1+
use crate::archive::Archiver;
2+
use crate::trap::{AsTrapKeyPart, TrapFile, TrapId};
53
use crate::{generated, trap_key};
6-
use ra_ap_hir::{Crate, Module, ModuleDef};
74
use anyhow;
8-
use ra_ap_hir::{HasSource};
9-
use ra_ap_vfs::{AbsPath, FileId, Vfs};
5+
use codeql_extractor::trap;
6+
use ra_ap_hir::HasSource;
7+
use ra_ap_hir::{Crate, Module, ModuleDef};
8+
use ra_ap_ide_db::line_index::LineIndex;
9+
use ra_ap_ide_db::{LineIndexDatabase, RootDatabase};
1010
use ra_ap_syntax::ast::HasName;
11-
use crate::archive::Archiver;
11+
use ra_ap_syntax::AstNode;
12+
use ra_ap_vfs::{AbsPath, FileId, Vfs};
13+
use std::collections::HashMap;
14+
use std::fs;
1215
use std::io::Result;
16+
use std::path::PathBuf;
1317
use triomphe::Arc;
14-
use ra_ap_ide_db::{LineIndexDatabase, RootDatabase};
15-
use ra_ap_ide_db::line_index::LineIndex;
16-
use ra_ap_syntax::AstNode;
17-
use codeql_extractor::trap;
1818

1919
#[derive(Clone)]
2020
struct FileData {
@@ -30,7 +30,6 @@ pub struct CrateTranslator<'a> {
3030
file_labels: HashMap<PathBuf, FileData>,
3131
}
3232

33-
3433
impl CrateTranslator<'_> {
3534
pub fn new<'a>(
3635
db: &'a RootDatabase,
@@ -56,9 +55,13 @@ impl CrateTranslator<'_> {
5655
self.archiver.archive(&canonical);
5756
canonical = fs::canonicalize(&canonical).unwrap_or(canonical);
5857
let name = canonical.to_string_lossy();
59-
let label = self.trap.emit(generated::DbFile { id: trap_key!["file;", name.as_ref()], name: String::from(name) });
58+
let label = self.trap.emit(generated::DbFile {
59+
id: trap_key!["file;", name.as_ref()],
60+
name: String::from(name),
61+
});
6062
let line_index = <dyn LineIndexDatabase>::line_index(self.db, file_id);
61-
self.file_labels.insert(canonical.clone(), FileData { label, line_index });
63+
self.file_labels
64+
.insert(canonical.clone(), FileData { label, line_index });
6265
}
6366
self.file_labels.get(&canonical).cloned()
6467
})
@@ -68,7 +71,8 @@ impl CrateTranslator<'_> {
6871
where
6972
T::Ast: AstNode,
7073
{
71-
entity.source(self.db)
74+
entity
75+
.source(self.db)
7276
.and_then(|source| source.file_id.file_id().map(|f| (f.file_id(), source)))
7377
.and_then(|(file_id, source)| self.emit_file(file_id).map(|data| (data, source)))
7478
.and_then(|(data, source)| {
@@ -79,7 +83,12 @@ impl CrateTranslator<'_> {
7983
})
8084
}
8185

82-
fn emit_definition(&mut self, module_label: trap::Label, id: ModuleDef, labels: &mut Vec<trap::Label>) {
86+
fn emit_definition(
87+
&mut self,
88+
module_label: trap::Label,
89+
id: ModuleDef,
90+
labels: &mut Vec<trap::Label>,
91+
) {
8392
match id {
8493
ModuleDef::Module(_) => {}
8594
ModuleDef::Function(function) => {
@@ -115,7 +124,7 @@ impl CrateTranslator<'_> {
115124
});
116125
}
117126

118-
pub fn emit_crate(&mut self) -> std::io::Result<()> {
127+
pub fn emit_crate(&mut self) -> std::io::Result<()> {
119128
self.emit_file(self.krate.root_file(self.db));
120129
let mut map = HashMap::<Module, trap::Label>::new();
121130
for module in self.krate.modules(self.db) {

rust/extractor/src/trap.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
use crate::config::Compression;
2+
use crate::generated;
3+
use crate::{config, path};
4+
use codeql_extractor::trap;
5+
use log::{debug, trace};
6+
use ra_ap_ide_db::line_index::LineCol;
17
use std::ffi::OsString;
28
use std::fmt::{Debug, Display, Formatter};
39
use std::fs::File;
410
use std::io::Write;
511
use std::path::{Path, PathBuf};
6-
use log::{debug, trace};
7-
use crate::{config, path};
8-
use codeql_extractor::trap;
9-
use ra_ap_ide_db::line_index::LineCol;
10-
use crate::config::Compression;
11-
use crate::generated;
1212

1313
//TODO: typed labels
1414
pub trait AsTrapKeyPart {
@@ -81,7 +81,12 @@ pub struct TrapFile {
8181
}
8282

8383
impl TrapFile {
84-
pub fn emit_location(&mut self, file_label: trap::Label, start: LineCol, end: LineCol) -> trap::Label {
84+
pub fn emit_location(
85+
&mut self,
86+
file_label: trap::Label,
87+
start: LineCol,
88+
end: LineCol,
89+
) -> trap::Label {
8590
let start_line = start.line as usize;
8691
let start_column = start.col as usize;
8792
let end_line = end.line as usize;
@@ -119,7 +124,8 @@ impl TrapFile {
119124

120125
pub fn commit(&self) -> std::io::Result<()> {
121126
std::fs::create_dir_all(self.path.parent().unwrap())?;
122-
self.writer.write_to_file(&self.path, self.compression.into())
127+
self.writer
128+
.write_to_file(&self.path, self.compression.into())
123129
}
124130
}
125131

@@ -141,11 +147,15 @@ impl TrapFileProvider {
141147
pub fn create(&self, category: &str, key: &Path) -> TrapFile {
142148
let mut path = PathBuf::from(category);
143149
path.push(path::key(key));
144-
path.set_extension(path.extension().map(|e| {
145-
let mut o: OsString = e.to_owned();
146-
o.push(".trap");
147-
o
148-
}).unwrap_or("trap".into()));
150+
path.set_extension(
151+
path.extension()
152+
.map(|e| {
153+
let mut o: OsString = e.to_owned();
154+
o.push(".trap");
155+
o
156+
})
157+
.unwrap_or("trap".into()),
158+
);
149159
debug!("creating trap file {}", path.display());
150160
path = self.trap_dir.join(path);
151161
TrapFile {

0 commit comments

Comments
 (0)