diff --git a/kythe/rust/extractor/src/bin/cli.rs b/kythe/rust/extractor/src/bin/cli.rs index e8ebcc3dca..12a8819b0f 100644 --- a/kythe/rust/extractor/src/bin/cli.rs +++ b/kythe/rust/extractor/src/bin/cli.rs @@ -26,7 +26,10 @@ pub struct ExtractorConfig { impl ExtractorConfig { /// Create a new ExtractorConfig using the supplied parameters pub fn new(extra_action_path: PathBuf, output_path: PathBuf) -> Self { - Self { extra_action_path, output_path } + Self { + extra_action_path, + output_path, + } } } diff --git a/kythe/rust/extractor/src/bin/extractor.rs b/kythe/rust/extractor/src/bin/extractor.rs index 7632a6ec26..348c2626d9 100644 --- a/kythe/rust/extractor/src/bin/extractor.rs +++ b/kythe/rust/extractor/src/bin/extractor.rs @@ -44,8 +44,12 @@ fn main() -> Result<()> { )?; // Create the output kzip - let kzip_file = File::create(&config.output_path) - .with_context(|| format!("Failed to create kzip file at path {:?}", config.output_path))?; + let kzip_file = File::create(&config.output_path).with_context(|| { + format!( + "Failed to create kzip file at path {:?}", + config.output_path + ) + })?; let mut kzip = ZipWriter::new(kzip_file); kzip.add_directory("root/", FileOptions::default())?; @@ -116,12 +120,15 @@ fn get_spawn_info(file_path: impl AsRef) -> Result { let mut file = File::open(file_path).context("Failed to open extra action file")?; let mut file_contents_bytes = Vec::new(); - file.read_to_end(&mut file_contents_bytes).context("Failed to read extra action file")?; + file.read_to_end(&mut file_contents_bytes) + .context("Failed to read extra action file")?; let extra_action = protobuf::parse_from_bytes::(&file_contents_bytes) .context("Failed to parse extra action protobuf")?; - SPAWN_INFO.get(&extra_action).ok_or_else(|| anyhow!("SpawnInfo extension missing")) + SPAWN_INFO + .get(&extra_action) + .ok_or_else(|| anyhow!("SpawnInfo extension missing")) } /// Create an IndexedCompilation protobuf from the supplied arguments @@ -183,7 +190,11 @@ fn kzip_add_required_input( .read_to_end(&mut source_file_contents) .with_context(|| format!("Failed read file {:?}", file_path_string))?; let digest = sha256digest(&source_file_contents); - kzip_add_file(format!("root/files/{}", digest), &source_file_contents, zip_writer)?; + kzip_add_file( + format!("root/files/{}", digest), + &source_file_contents, + zip_writer, + )?; // Generate FileInput and add it to the list of required inputs let mut file_input = CompilationUnit_FileInput::new(); diff --git a/kythe/rust/extractor/src/bin/save_analysis.rs b/kythe/rust/extractor/src/bin/save_analysis.rs index 488203d528..61aee99df0 100644 --- a/kythe/rust/extractor/src/bin/save_analysis.rs +++ b/kythe/rust/extractor/src/bin/save_analysis.rs @@ -48,9 +48,15 @@ fn generate_arguments(arguments: Vec, output_dir: &Path) -> Result, output_dir: PathBuf) -> Result<(), String> { - let first_arg = - rustc_arguments.get(0).ok_or_else(|| "Arguments vector should not be empty".to_string())?; + let first_arg = rustc_arguments + .get(0) + .ok_or_else(|| "Arguments vector should not be empty".to_string())?; if first_arg != &"".to_string() { return Err("The first argument must be an empty string".into()); } diff --git a/kythe/rust/extractor/tests/integration_test.rs b/kythe/rust/extractor/tests/integration_test.rs index 651a659559..dfe028bd54 100644 --- a/kythe/rust/extractor/tests/integration_test.rs +++ b/kythe/rust/extractor/tests/integration_test.rs @@ -86,7 +86,12 @@ fn main() -> Result<()> { missing_arguments_fail(); bad_extra_action_path_fails(); - correct_arguments_succeed(&extra_action_path_str, &temp_dir_str, &output_key, arguments); + correct_arguments_succeed( + &extra_action_path_str, + &temp_dir_str, + &output_key, + arguments, + ); Ok(()) } @@ -98,18 +103,24 @@ fn missing_arguments_fail() { .assert() .failure() .code(1) - .stderr(starts_with("error: The following required arguments were not provided:")); + .stderr(starts_with( + "error: The following required arguments were not provided:", + )); Command::new(&extractor_path) .arg("--extra_action=/tmp/wherever") .assert() .failure() .code(1) - .stderr(starts_with("error: The following required arguments were not provided:")); + .stderr(starts_with( + "error: The following required arguments were not provided:", + )); Command::new(&extractor_path) .assert() .failure() .code(1) - .stderr(starts_with("error: The following required arguments were not provided:")); + .stderr(starts_with( + "error: The following required arguments were not provided:", + )); } fn bad_extra_action_path_fails() { @@ -154,7 +165,10 @@ fn correct_arguments_succeed( cu_path_str = file.name().to_string(); } } - assert_ne!(cu_path_str, "", "IndexedCompilation protobuf missing from kzip"); + assert_ne!( + cu_path_str, "", + "IndexedCompilation protobuf missing from kzip" + ); // Read it into an IndexedCompilation let cu_file = kzip.by_name(&cu_path_str).unwrap(); @@ -173,24 +187,41 @@ fn correct_arguments_succeed( ); let output_key = compilation_unit.get_output_key(); - assert_eq!(output_key, expected_output_key, "output_key field doesn't match"); + assert_eq!( + output_key, expected_output_key, + "output_key field doesn't match" + ); let unit_vname = compilation_unit.get_v_name(); - assert_eq!(unit_vname.get_language(), "rust", "VName language field doesn't match"); + assert_eq!( + unit_vname.get_language(), + "rust", + "VName language field doesn't match" + ); let arguments = compilation_unit.get_argument(); - assert_eq!(arguments, expected_arguments, "Argument field doesn't match"); + assert_eq!( + arguments, expected_arguments, + "Argument field doesn't match" + ); let required_inputs = compilation_unit.get_required_input().to_vec(); - let source_input = required_inputs.get(0).expect("Failed to get first required input"); + let source_input = required_inputs + .get(0) + .expect("Failed to get first required input"); assert_eq!(source_input.get_v_name().get_corpus(), "test_corpus"); - assert_eq!(source_input.get_info().get_path(), "kythe/rust/extractor/main.rs"); + assert_eq!( + source_input.get_info().get_path(), + "kythe/rust/extractor/main.rs" + ); assert_eq!( source_input.get_info().get_digest(), "7cb3b3c74ecdf86f434548ba15c1651c92bf03b6690fd0dfc053ab09d094cf03" ); - let analysis_input = required_inputs.get(1).expect("Failed to get second required input"); + let analysis_input = required_inputs + .get(1) + .expect("Failed to get second required input"); assert_eq!(analysis_input.get_v_name().get_corpus(), "test_corpus"); let analysis_path = analysis_input.get_info().get_path(); assert!( diff --git a/kythe/rust/extractor/tests/test.rs b/kythe/rust/extractor/tests/test.rs index b0461db853..6e5fa83f85 100644 --- a/kythe/rust/extractor/tests/test.rs +++ b/kythe/rust/extractor/tests/test.rs @@ -25,7 +25,10 @@ fn empty_args_fails() { let temp_dir = TempDir::new("extractor_test").expect("Could not create temporary directory"); let analysis_directory = PathBuf::new().join(temp_dir.path()); let result = generate_analysis(args, analysis_directory); - assert_eq!(result.unwrap_err(), "Arguments vector should not be empty".to_string()); + assert_eq!( + result.unwrap_err(), + "Arguments vector should not be empty".to_string() + ); } #[test] @@ -34,7 +37,10 @@ fn nonempty_string_first_fails() { let temp_dir = TempDir::new("extractor_test").expect("Could not create temporary directory"); let analysis_directory = PathBuf::new().join(temp_dir.path()); let result = generate_analysis(args, analysis_directory); - assert_eq!(result.unwrap_err(), "The first argument must be an empty string".to_string()); + assert_eq!( + result.unwrap_err(), + "The first argument must be an empty string".to_string() + ); } #[test] diff --git a/kythe/rust/fuchsia_extractor/src/bin/main.rs b/kythe/rust/fuchsia_extractor/src/bin/main.rs index 549a899a8b..a3342b54c7 100644 --- a/kythe/rust/fuchsia_extractor/src/bin/main.rs +++ b/kythe/rust/fuchsia_extractor/src/bin/main.rs @@ -164,9 +164,10 @@ fn make_vname( vname.set_language(language.to_string()); vname.set_corpus(corpus.to_string()); vname.set_root(root.to_string()); - let rel_path_str = rel_path - .to_str() - .ok_or(anyhow::anyhow!("make_vname: could not convert to UTF-8: {:?}", &rel_path))?; + let rel_path_str = rel_path.to_str().ok_or(anyhow::anyhow!( + "make_vname: could not convert to UTF-8: {:?}", + &rel_path + ))?; if let Some(_) = rel_path_str.find("../") { return Err(anyhow::anyhow!( concat!(concat!( @@ -199,7 +200,10 @@ fn make_file_input( file_info.set_path( file_name .to_str() - .ok_or(anyhow::anyhow!("make_file_input: could not convert to UTF-8: {:?}", file_name))? + .ok_or(anyhow::anyhow!( + "make_file_input: could not convert to UTF-8: {:?}", + file_name + ))? .to_string(), ); file_info.set_digest(digest.to_string()); @@ -239,7 +243,10 @@ fn get_crate_name(prelude: &rls_data::CratePreludeData) -> String { /// Gets the path to the crate root directory from the prelude. fn get_crate_directory(prelude: &rls_data::CratePreludeData) -> PathBuf { let root_path = PathBuf::from(&prelude.crate_root); - root_path.parent().unwrap_or(&PathBuf::from("")).to_path_buf() + root_path + .parent() + .unwrap_or(&PathBuf::from("")) + .to_path_buf() } /// Populates a single input into the Writer. @@ -344,9 +351,10 @@ lazy_static! { /// build directory. fn corpus_root_for(relative_path: &PathBuf) -> &'static CorpusRoot { let pathstr = relative_path.to_string_lossy(); - ROOT_MAP.iter() + ROOT_MAP + .iter() .find(|r| pathstr.starts_with(r.path)) - .unwrap_or(&ROOT_MAP[ROOT_MAP.len()-1]) + .unwrap_or(&ROOT_MAP[ROOT_MAP.len() - 1]) } /// Adds the single `src_path` input to the archive and required inputs. @@ -358,27 +366,30 @@ fn add_source_input( options: &Options, mut required_inputs: &mut Vec, mut file_inputs: &mut Vec, - ) -> Result<()> { - // "../../file.txt" - let src_path_relative = make_relative_to(src_path, &options.base_dir)?; - let corpus_root_info = corpus_root_for(&src_path_relative); - // "../../", "" - let corpus_relative_path = PathBuf::from(&corpus_root_info.path); - // absolute path to " - let src_base_dir = partial_canonicalize_path(&options.base_dir.join(corpus_relative_path)) - .with_context(|| format!("add_source_input: while canonicalizing"))?; - println!("src_path:\n\t{:?}\ncorpus_root:\n\t{:?}", &src_path, &corpus_root_info); - add_input( - &mut archive, - &options.corpus_name, - &src_path, - corpus_root_info.name, - &src_base_dir, - &options.base_dir, - &mut required_inputs, - &mut file_inputs, - ) - .with_context(|| format!("add_source_input: while adding Rust file: {:?}", &src_path)) +) -> Result<()> { + // "../../file.txt" + let src_path_relative = make_relative_to(src_path, &options.base_dir)?; + let corpus_root_info = corpus_root_for(&src_path_relative); + // "../../", "" + let corpus_relative_path = PathBuf::from(&corpus_root_info.path); + // absolute path to " + let src_base_dir = partial_canonicalize_path(&options.base_dir.join(corpus_relative_path)) + .with_context(|| format!("add_source_input: while canonicalizing"))?; + println!( + "src_path:\n\t{:?}\ncorpus_root:\n\t{:?}", + &src_path, &corpus_root_info + ); + add_input( + &mut archive, + &options.corpus_name, + &src_path, + corpus_root_info.name, + &src_base_dir, + &options.base_dir, + &mut required_inputs, + &mut file_inputs, + ) + .with_context(|| format!("add_source_input: while adding Rust file: {:?}", &src_path)) } /// Process one save-analysis file. @@ -418,31 +429,41 @@ fn process_file( // Add the JSON file to the archive. let save_analysis_contents = fs::read(&file).with_context(|| { - format!("process_file: while reading save analysis for storage: {:?}", &file) + format!( + "process_file: while reading save analysis for storage: {:?}", + &file + ) })?; let save_analysis_digest = archive .write_file(&save_analysis_contents) - .with_context(|| format!( + .with_context(|| { + format!( "while saving save analysis for storage:\n\t{:?}\ninto:\n\t{:?}", - &file, &kzip_filename))?; + &file, &kzip_filename + ) + })?; file_inputs.push(make_file_input( - make_vname(file, &options.corpus_name, "save-analysis", &options.base_dir, "rust")?, + make_vname( + file, + &options.corpus_name, + "save-analysis", + &options.base_dir, + "rust", + )?, file, &options.base_dir, &save_analysis_digest, )?); // Add all arguments. - let compilation = analysis - .compilation - .as_ref() - .ok_or(anyhow::anyhow!("process_file: analysis JSON file has no compilation section"))?; + let compilation = analysis.compilation.as_ref().ok_or(anyhow::anyhow!( + "process_file: analysis JSON file has no compilation section" + ))?; let arguments: Vec = compilation.arguments.clone(); - let prelude = analysis - .prelude - .as_ref() - .ok_or(anyhow::anyhow!("process_file: analysis JSON file has no prelude section"))?; + let prelude = analysis.prelude.as_ref().ok_or(anyhow::anyhow!( + "process_file: analysis JSON file has no prelude section" + ))?; let crate_name = get_crate_name(&prelude); let mut required_inputs: Vec = vec![]; @@ -451,11 +472,20 @@ fn process_file( let crate_root_directory = get_crate_directory(&prelude); let crate_root_directory = &options.base_dir.join(crate_root_directory); let rust_files = read_dir(&crate_root_directory, true, &MATCH_RUST).with_context(|| { - format!("process_file: while reading crate root: {:?}", &crate_root_directory) + format!( + "process_file: while reading crate root: {:?}", + &crate_root_directory + ) })?; for src_path in rust_files { - add_source_input(&mut archive, &src_path, options, &mut required_inputs, &mut file_inputs)?; + add_source_input( + &mut archive, + &src_path, + options, + &mut required_inputs, + &mut file_inputs, + )?; } // For each rlib file under the directory, add it. @@ -488,7 +518,10 @@ fn process_file( compilation_unit.set_output_key(compilation_output_path.to_string_lossy().to_string()); let abs_base_dir = fs::canonicalize(&options.base_dir).with_context(|| { - format!("process_file: while trying to find absolute path of {:?}", &options.base_dir) + format!( + "process_file: while trying to find absolute path of {:?}", + &options.base_dir + ) })?; compilation_unit.set_working_directory(abs_base_dir.to_string_lossy().to_string()); @@ -499,7 +532,10 @@ fn process_file( indexed_compilation.set_unit(compilation_unit); indexed_compilation.set_index(index); archive.write_unit(&indexed_compilation).with_context(|| { - format!("process_file: while writing compilation unit for crate: {:?}", &crate_name) + format!( + "process_file: while writing compilation unit for crate: {:?}", + &crate_name + ) })?; let output_filename = output_dir.join(kzip_filename); Ok(output_filename) @@ -522,7 +558,10 @@ fn lenient_process_file(file_name: &PathBuf, options: &Options) -> Result Result> { if !options.quiet { println!("\nprocess_files: processing {:?}", &file_name); } - let kzip = lenient_process_file(&file_name, &options).with_context(|| format!("process_file: found error")); + let kzip = lenient_process_file(&file_name, &options) + .with_context(|| format!("process_file: found error")); match kzip { - Err(ref e) => { + Err(ref e) => { eprintln!("process_files: found error: {:?}", e); - }, + } Ok(ref pb) => { if !options.quiet { println!("process_files: made archive:\n\t{:?}", pb); } - }, + } } kzip }) @@ -638,8 +678,10 @@ fn main() -> Result<()> { .split(",") .map(|e| e.into()) .collect::>(); - let all_files: Vec = - files_from_dirs.into_iter().chain(explicit_files.into_iter()).collect(); + let all_files: Vec = files_from_dirs + .into_iter() + .chain(explicit_files.into_iter()) + .collect(); let output_dir: PathBuf = matches.value_of("OUTPUT_DIR").unwrap().into(); let corpus_name = matches .value_of("CORPUS") @@ -670,7 +712,10 @@ fn main() -> Result<()> { #[cfg(test)] mod testing { - use {super::*, std::collections::HashSet, std::fs, std::io::Read, tempdir::TempDir, serial_test::serial}; + use { + super::*, serial_test::serial, std::collections::HashSet, std::fs, std::io::Read, + tempdir::TempDir, + }; /// Rebases the given `relative_path`, such that it is relative to /// `rebase_dir`. For example, "./foo/bar/file.txt", relative to "./foo" @@ -734,13 +779,15 @@ mod testing { ]; for test in tests { let src = temp_dir.path().join(&test.source); - fs::create_dir_all(&src) - .expect(&format!("source dir created: {:?} in test:\n\t{:?}", - &src, &test)); + fs::create_dir_all(&src).expect(&format!( + "source dir created: {:?} in test:\n\t{:?}", + &src, &test + )); let dest_dir = temp_dir.path().join(&test.dest); - fs::create_dir_all(&dest_dir) - .expect(&format!("dest dir created: {:?} in test:\n\t{:?}", - &dest_dir, &test)); + fs::create_dir_all(&dest_dir).expect(&format!( + "dest dir created: {:?} in test:\n\t{:?}", + &dest_dir, &test + )); let actual = rebase_path(&src, &dest_dir) .expect(&format!("rebase_path fails in test: {:?}", &test)); assert_eq!(actual, test.expected, "mismatch in test: {:?}", &test); @@ -752,8 +799,7 @@ mod testing { fn test_make_file_input() { let temp_dir = TempDir::new("dir").expect("temp dir created"); let base_dir = temp_dir.path().join("src-root-dir"); - fs::create_dir_all(&base_dir) - .expect(&format!("base dir created: {:?}", &base_dir)); + fs::create_dir_all(&base_dir).expect(&format!("base dir created: {:?}", &base_dir)); let save_analysis_dir = base_dir.join("save-analysis-dir"); fs::create_dir_all(&save_analysis_dir).expect("save analysis dir created"); let file_name = save_analysis_dir.join("save-analysis.json"); @@ -801,7 +847,10 @@ mod testing { "some-other-thing=barrlib", ]; let result = extract_rlibs(&args); - assert_eq!(vec!["foo.rlib", "dir1/dir2/foo-something.rlib", "bar.rlib",], result); + assert_eq!( + vec!["foo.rlib", "dir1/dir2/foo-something.rlib", "bar.rlib",], + result + ); } #[test] @@ -892,10 +941,14 @@ mod testing { } fn unzip_compilation_unit(zip_path: impl AsRef) -> IndexedCompilation { - let file = fs::File::open(&zip_path.as_ref()) - .expect(&format!("could not open zip file: {:?}", &zip_path.as_ref())); - let mut zip = zip::ZipArchive::new(file) - .expect(&format!("could not create zip file handle: {:?}", &zip_path.as_ref())); + let file = fs::File::open(&zip_path.as_ref()).expect(&format!( + "could not open zip file: {:?}", + &zip_path.as_ref() + )); + let mut zip = zip::ZipArchive::new(file).expect(&format!( + "could not create zip file handle: {:?}", + &zip_path.as_ref() + )); for i in 0..zip.len() { let mut file = zip.by_index(i).unwrap(); if !file.is_file() || !file.name().starts_with("root/pbunits/") { @@ -936,7 +989,15 @@ mod testing { let only_archive = zips.get(0).unwrap(); let mut indexed_compilation = unzip_compilation_unit(&only_archive); // Sort the required_input by info path to give a predictable order. - indexed_compilation.mut_unit().mut_required_input().sort_by(|a, b| a.get_info().get_path().partial_cmp(b.get_info().get_path()).unwrap()); + indexed_compilation + .mut_unit() + .mut_required_input() + .sort_by(|a, b| { + a.get_info() + .get_path() + .partial_cmp(b.get_info().get_path()) + .unwrap() + }); let compilation_unit = indexed_compilation.get_unit(); let cu_vname = compilation_unit.get_v_name(); @@ -1016,8 +1077,8 @@ mod testing { let zips = process_files(&all_files, &options).expect("processing is successful"); let only_archive = zips.get(0).unwrap(); - let test_runfiles = PathBuf::from( - std::env::var("TEST_SRCDIR").expect("TEST_SRCDIR is available")); + let test_runfiles = + PathBuf::from(std::env::var("TEST_SRCDIR").expect("TEST_SRCDIR is available")); let kzip_util_path = test_runfiles.join("io_kythe/kythe/go/platform/tools/kzip/kzip"); @@ -1028,10 +1089,13 @@ mod testing { .output() .expect("failed to execute kzip info"); - assert!(output.status.success(), "failed kzip info: stderr:\n{:?}\nstdout:\n{:?}\ncode:{:?}", + assert!( + output.status.success(), + "failed kzip info: stderr:\n{:?}\nstdout:\n{:?}\ncode:{:?}", String::from_utf8_lossy(&output.stderr), String::from_utf8_lossy(&output.stdout), - output.status); + output.status + ); } #[test] @@ -1058,7 +1122,15 @@ mod testing { let only_archive = zips.get(0).unwrap(); let mut indexed_compilation = unzip_compilation_unit(&only_archive); // Sort the required_input by info path to give a predictable order. - indexed_compilation.mut_unit().mut_required_input().sort_by(|a, b| a.get_info().get_path().partial_cmp(b.get_info().get_path()).unwrap()); + indexed_compilation + .mut_unit() + .mut_required_input() + .sort_by(|a, b| { + a.get_info() + .get_path() + .partial_cmp(b.get_info().get_path()) + .unwrap() + }); let compilation_unit = indexed_compilation.get_unit(); let cu_vname = compilation_unit.get_v_name(); @@ -1088,5 +1160,4 @@ mod testing { ); assert_eq!("nom", cu_vname.get_signature()); } - } diff --git a/kythe/rust/fuchsia_extractor/src/kzip.rs b/kythe/rust/fuchsia_extractor/src/kzip.rs index 2658c0a4b6..288cd50768 100644 --- a/kythe/rust/fuchsia_extractor/src/kzip.rs +++ b/kythe/rust/fuchsia_extractor/src/kzip.rs @@ -65,9 +65,15 @@ impl Writer { ) }; if status != 0 { - return Err(anyhow::anyhow!("could not create KzipWriter: code: {}", status)); + return Err(anyhow::anyhow!( + "could not create KzipWriter: code: {}", + status + )); } - Ok(Writer { rep: std::ptr::NonNull::new(raw).expect("non-null in try_new"), closed: false }) + Ok(Writer { + rep: std::ptr::NonNull::new(raw).expect("non-null in try_new"), + closed: false, + }) } /// Closes the writer and flushes its buffers.i @@ -120,8 +126,9 @@ impl Writer { const CAPACITY: usize = 200; let mut buf: Vec = vec![0; CAPACITY]; let mut resulting_buffer_size: sys::size_t = 0; - let content = - unit.write_to_bytes().with_context(|| "kzip::write_unit: while writing protobuf")?; + let content = unit + .write_to_bytes() + .with_context(|| "kzip::write_unit: while writing protobuf")?; let status = unsafe { sys::KzipWriter_WriteUnit( self.rep.as_ptr(), @@ -155,13 +162,25 @@ mod tests { let output_file = temp_dir.path().join("out.kzip"); let content = "content"; let unit = analysis::IndexedCompilation::new(); - assert!(!Path::exists(&output_file), "file exists but should not: {:?}", &output_file); + assert!( + !Path::exists(&output_file), + "file exists but should not: {:?}", + &output_file + ); { let mut writer = Writer::try_new(&output_file, Encoding::Proto).expect("created writer"); - writer.write_file(content.as_bytes()).expect("wrote file with success"); - writer.write_unit(&unit).expect("wrote compilation unit with success"); + writer + .write_file(content.as_bytes()) + .expect("wrote file with success"); + writer + .write_unit(&unit) + .expect("wrote compilation unit with success"); } - assert!(Path::exists(&output_file), "file does not exist but should: {:?}", &output_file); + assert!( + Path::exists(&output_file), + "file does not exist but should: {:?}", + &output_file + ); } } diff --git a/kythe/rust/indexer/src/bin/main.rs b/kythe/rust/indexer/src/bin/main.rs index 6b11569213..13b223afd3 100644 --- a/kythe/rust/indexer/src/bin/main.rs +++ b/kythe/rust/indexer/src/bin/main.rs @@ -84,9 +84,9 @@ pub fn extract_from_kzip( let input_path_buf = PathBuf::new().join(input_path); let output_path = match input_path_buf.extension() { Some(os_str) => match os_str.to_str() { - Some("json") => { - temp_path.join("analysis").join(input_path_buf.file_name().unwrap()) - } + Some("json") => temp_path + .join("analysis") + .join(input_path_buf.file_name().unwrap()), _ => temp_path.join(input_path), }, _ => temp_path.join(input_path), @@ -97,13 +97,19 @@ pub fn extract_from_kzip( .parent() .ok_or_else(|| anyhow!("Failed to get parent for path: {:?}", output_path))?; std::fs::create_dir_all(parent).with_context(|| { - format!("Failed to create temporary directories for path: {}", parent.display()) + format!( + "Failed to create temporary directories for path: {}", + parent.display() + ) })?; // Copy the file contents to the output path in the temporary directory let digest = required_input.get_info().get_digest(); let file_contents = provider.contents(digest).with_context(|| { - format!("Failed to get contents of file \"{}\" with digest \"{}\"", input_path, digest) + format!( + "Failed to get contents of file \"{}\" with digest \"{}\"", + input_path, digest + ) })?; let mut output_file = File::create(&output_path).context("Failed to create file")?; output_file.write_all(&file_contents).with_context(|| { diff --git a/kythe/rust/indexer/src/indexer/analyzers.rs b/kythe/rust/indexer/src/indexer/analyzers.rs index 5d5717b7a3..ec192be7b2 100644 --- a/kythe/rust/indexer/src/indexer/analyzers.rs +++ b/kythe/rust/indexer/src/indexer/analyzers.rs @@ -118,10 +118,12 @@ impl<'a> UnitAnalyzer<'a> { let vname = self.get_file_vname(source_file)?; // Create the file node fact - self.emitter.emit_node(&vname, "/kythe/node/kind", b"file".to_vec())?; + self.emitter + .emit_node(&vname, "/kythe/node/kind", b"file".to_vec())?; // Create language fact - self.emitter.emit_node(&vname, "/kythe/language", b"rust".to_vec())?; + self.emitter + .emit_node(&vname, "/kythe/language", b"rust".to_vec())?; // Read the file contents and set it on the fact // Returns a FileReadError if we can't read the file @@ -131,7 +133,8 @@ impl<'a> UnitAnalyzer<'a> { self.offset_index.add_file(&source_file, &file_contents); // Create text fact - self.emitter.emit_node(&vname, "/kythe/text", file_contents.into_bytes())?; + self.emitter + .emit_node(&vname, "/kythe/text", file_contents.into_bytes())?; } Ok(()) } @@ -163,7 +166,10 @@ impl<'a> UnitAnalyzer<'a> { "Failed to find VName for file \"{}\" located in the save analysis. Is it included in the required inputs of the Compilation Unit?", file_name ); - let vname = self.file_vnames.get(file_name).ok_or(KytheError::IndexerError(err_msg))?; + let vname = self + .file_vnames + .get(file_name) + .ok_or(KytheError::IndexerError(err_msg))?; Ok(vname.clone()) } } @@ -256,20 +262,28 @@ impl<'a, 'b> CrateAnalyzer<'a, 'b> { // First emit the node for our own crate and add it to the hashmap let krate_id = &krate_prelude.crate_id; - let krate_signature = format!("{}_{}_{}", krate_id.disambiguator.0, krate_id.disambiguator.1, krate_id.name); + let krate_signature = format!( + "{}_{}_{}", + krate_id.disambiguator.0, krate_id.disambiguator.1, krate_id.name + ); let krate_vname = self.generate_crate_vname(&krate_signature); self.krate_vname = krate_vname.clone(); - self.emitter.emit_node(&krate_vname, "/kythe/node/kind", b"package".to_vec())?; + self.emitter + .emit_node(&krate_vname, "/kythe/node/kind", b"package".to_vec())?; self.krate_ids.insert(0u32, krate_id.clone()); // Then, do the same for all of the external crates for (krate_num, external_krate) in krate_prelude.external_crates.iter().enumerate() { let krate_id = &external_krate.id; - let krate_signature = - format!("{}_{}_{}", krate_id.disambiguator.0, krate_id.disambiguator.1, krate_id.name); + let krate_signature = format!( + "{}_{}_{}", + krate_id.disambiguator.0, krate_id.disambiguator.1, krate_id.name + ); let krate_vname = self.generate_crate_vname(&krate_signature); - self.emitter.emit_node(&krate_vname, "/kythe/node/kind", b"package".to_vec())?; - self.krate_ids.insert((krate_num + 1) as u32, krate_id.clone()); + self.emitter + .emit_node(&krate_vname, "/kythe/node/kind", b"package".to_vec())?; + self.krate_ids + .insert((krate_num + 1) as u32, krate_id.clone()); } Ok(()) @@ -278,7 +292,8 @@ impl<'a, 'b> CrateAnalyzer<'a, 'b> { /// Emits tbuiltin nodes for all of the Rust built-in types pub fn emit_tbuiltin_nodes(&mut self) -> Result<(), KytheError> { for vname in self.type_vnames.values() { - self.emitter.emit_node(vname, "/kythe/node/kind", b"tbuiltin".to_vec())?; + self.emitter + .emit_node(vname, "/kythe/node/kind", b"tbuiltin".to_vec())?; } Ok(()) @@ -321,9 +336,18 @@ impl<'a, 'b> CrateAnalyzer<'a, 'b> { // The save_analysis file will have the krate and index be maxint if the // implementation is not on a trait. let max_int = 4294967295; - let trait_target = - if relation.to.krate != max_int { Some(relation.to) } else { None }; - method_index.insert(*child, MethodImpl { struct_target, trait_target }); + let trait_target = if relation.to.krate != max_int { + Some(relation.to) + } else { + None + }; + method_index.insert( + *child, + MethodImpl { + struct_target, + trait_target, + }, + ); } } } @@ -358,7 +382,12 @@ impl<'a, 'b> CrateAnalyzer<'a, 'b> { // If the file name is mod.rs, then the expected name is the directory name if Some(OsStr::new("mod.rs")) == file_path.file_name() { if let Some(parent_directory) = file_path.parent() { - expected_name = parent_directory.file_name().unwrap().to_str().unwrap().to_string(); + expected_name = parent_directory + .file_name() + .unwrap() + .to_str() + .unwrap() + .to_string(); } else { // This should only happen if there is something wrong with the file path we // were provided in the CompilationUnit @@ -422,7 +451,8 @@ impl<'a, 'b> CrateAnalyzer<'a, 'b> { child_id )) })?; - self.emitter.emit_edge(&child_vname, parent_vname, "/kythe/edge/childof")?; + self.emitter + .emit_edge(&child_vname, parent_vname, "/kythe/edge/childof")?; } Ok(()) @@ -446,7 +476,8 @@ impl<'a, 'b> CrateAnalyzer<'a, 'b> { if let Some(child_vname) = self.definition_vnames.get(child) { // The child definition has already been visited and we can emit the childof // edge now - self.emitter.emit_edge(child_vname, def_vname, "/kythe/edge/childof")?; + self.emitter + .emit_edge(child_vname, def_vname, "/kythe/edge/childof")?; } else { self.children_ids.insert(*child, def_vname.clone()); } @@ -456,7 +487,8 @@ impl<'a, 'b> CrateAnalyzer<'a, 'b> { // Check if the current definition is a child of another node and remove the // entry if it existed if let Some(parent_vname) = self.children_ids.remove(&def.id) { - self.emitter.emit_edge(def_vname, &parent_vname, "/kythe/edge/childof")?; + self.emitter + .emit_edge(def_vname, &parent_vname, "/kythe/edge/childof")?; } // Store the definition's VName so it can be referenced by its id later @@ -492,7 +524,8 @@ impl<'a, 'b> CrateAnalyzer<'a, 'b> { )) })?; // Emit the childof edge between this node and the parent - self.emitter.emit_edge(def_vname, parent_vname, "/kythe/edge/childof")?; + self.emitter + .emit_edge(def_vname, parent_vname, "/kythe/edge/childof")?; } } DefKind::Function => { @@ -562,7 +595,8 @@ impl<'a, 'b> CrateAnalyzer<'a, 'b> { vname }; // Emit a childof edge to the parent struct - self.emitter.emit_edge(def_vname, &parent_vname, "/kythe/edge/childof")?; + self.emitter + .emit_edge(def_vname, &parent_vname, "/kythe/edge/childof")?; } } DefKind::Mod => { @@ -571,7 +605,8 @@ impl<'a, 'b> CrateAnalyzer<'a, 'b> { facts.push(("/kythe/complete", b"definition")); // Emit the childof edge on the crate if this is the main module if def.qualname == "::" { - self.emitter.emit_edge(def_vname, &self.krate_vname, "/kythe/edge/childof")?; + self.emitter + .emit_edge(def_vname, &self.krate_vname, "/kythe/edge/childof")?; } } DefKind::Struct => { @@ -608,7 +643,8 @@ impl<'a, 'b> CrateAnalyzer<'a, 'b> { // If it aliases a builtin type, emit an aliases edge // TODO: Make this work with custom types if let Some(type_vname) = self.type_vnames.get(&def.value) { - self.emitter.emit_edge(def_vname, type_vname, "/kythe/edge/aliases")?; + self.emitter + .emit_edge(def_vname, type_vname, "/kythe/edge/aliases")?; } } DefKind::Union => { @@ -622,13 +658,18 @@ impl<'a, 'b> CrateAnalyzer<'a, 'b> { // Emit nodes for all fact/value pairs for (fact_name, fact_value) in facts.iter() { - self.emitter.emit_node(def_vname, fact_name, fact_value.to_vec())?; + self.emitter + .emit_node(def_vname, fact_name, fact_value.to_vec())?; } // Calculate the byte_start and byte_end using the OffsetIndex let byte_start = self .offset_index - .get_byte_offset(file_vname.get_path(), def.span.line_start.0, def.span.column_start.0) + .get_byte_offset( + file_vname.get_path(), + def.span.line_start.0, + def.span.column_start.0, + ) .ok_or_else(|| { KytheError::IndexerError(format!( "Failed to get starting offset for definition {:?}", @@ -637,7 +678,11 @@ impl<'a, 'b> CrateAnalyzer<'a, 'b> { })?; let byte_end = self .offset_index - .get_byte_offset(file_vname.get_path(), def.span.line_end.0, def.span.column_end.0) + .get_byte_offset( + file_vname.get_path(), + def.span.line_end.0, + def.span.column_end.0, + ) .ok_or_else(|| { KytheError::IndexerError(format!( "Failed to get ending offset for definition {:?}", @@ -652,12 +697,17 @@ impl<'a, 'b> CrateAnalyzer<'a, 'b> { // Module definitions need special logic if they are implicit if def.kind == DefKind::Mod && self.is_module_implicit(def) { // Emit a 0-length anchor and defines edge at the top of the file - self.emitter.emit_node(&anchor_vname, "/kythe/node/kind", b"anchor".to_vec())?; - self.emitter.emit_node(&anchor_vname, "/kythe/loc/start", b"0".to_vec())?; - self.emitter.emit_node(&anchor_vname, "/kythe/loc/end", b"0".to_vec())?; - self.emitter.emit_edge(&anchor_vname, def_vname, "/kythe/edge/defines/implicit")?; + self.emitter + .emit_node(&anchor_vname, "/kythe/node/kind", b"anchor".to_vec())?; + self.emitter + .emit_node(&anchor_vname, "/kythe/loc/start", b"0".to_vec())?; + self.emitter + .emit_node(&anchor_vname, "/kythe/loc/end", b"0".to_vec())?; + self.emitter + .emit_edge(&anchor_vname, def_vname, "/kythe/edge/defines/implicit")?; } else { - self.emitter.emit_anchor(&anchor_vname, def_vname, byte_start, byte_end)?; + self.emitter + .emit_anchor(&anchor_vname, def_vname, byte_start, byte_end)?; } // If documentation isn't "" also generate a documents node @@ -667,13 +717,15 @@ impl<'a, 'b> CrateAnalyzer<'a, 'b> { let mut doc_vname = def_vname.clone(); let doc_signature = format!("{}_doc", def_vname.get_signature()); doc_vname.set_signature(doc_signature); - self.emitter.emit_node(&doc_vname, "/kythe/node/kind", b"doc".to_vec())?; + self.emitter + .emit_node(&doc_vname, "/kythe/node/kind", b"doc".to_vec())?; self.emitter.emit_node( &doc_vname, "/kythe/text", def.docs.trim().as_bytes().to_vec(), )?; - self.emitter.emit_edge(&doc_vname, def_vname, "/kythe/edge/documents")?; + self.emitter + .emit_edge(&doc_vname, def_vname, "/kythe/edge/documents")?; } Ok(()) @@ -722,8 +774,10 @@ impl<'a, 'b> CrateAnalyzer<'a, 'b> { })?; // Create signature based on span - reference_vname - .set_signature(format!("{}_ref_{}_{}", krate_signature, start_byte, end_byte)); + reference_vname.set_signature(format!( + "{}_ref_{}_{}", + krate_signature, start_byte, end_byte + )); // Create VName being referenced let krate_id = self.krate_ids.get(&reference.ref_id.krate).ok_or_else(|| { @@ -737,11 +791,14 @@ impl<'a, 'b> CrateAnalyzer<'a, 'b> { krate_id.disambiguator.0, krate_id.disambiguator.1, krate_id.name ); let mut target_vname = template_vname.clone(); - target_vname - .set_signature(format!("{}_def_{}", krate_signature, reference.ref_id.index)); + target_vname.set_signature(format!( + "{}_def_{}", + krate_signature, reference.ref_id.index + )); target_vname.set_language("rust".to_string()); - self.emitter.emit_reference(&reference_vname, &target_vname, start_byte, end_byte)?; + self.emitter + .emit_reference(&reference_vname, &target_vname, start_byte, end_byte)?; } Ok(()) } diff --git a/kythe/rust/indexer/src/indexer/entries.rs b/kythe/rust/indexer/src/indexer/entries.rs index e3cbcee67f..9058b472da 100644 --- a/kythe/rust/indexer/src/indexer/entries.rs +++ b/kythe/rust/indexer/src/indexer/entries.rs @@ -83,7 +83,11 @@ impl<'a> EntryEmitter<'a> { "/kythe/loc/start", byte_start.to_string().into_bytes().to_vec(), )?; - self.emit_node(anchor_vname, "/kythe/loc/end", byte_end.to_string().into_bytes().to_vec())?; + self.emit_node( + anchor_vname, + "/kythe/loc/end", + byte_end.to_string().into_bytes().to_vec(), + )?; self.emit_edge(anchor_vname, target_vname, "/kythe/edge/defines/binding") } @@ -105,7 +109,11 @@ impl<'a> EntryEmitter<'a> { "/kythe/loc/start", byte_start.to_string().into_bytes().to_vec(), )?; - self.emit_node(anchor_vname, "/kythe/loc/end", byte_end.to_string().into_bytes().to_vec())?; + self.emit_node( + anchor_vname, + "/kythe/loc/end", + byte_end.to_string().into_bytes().to_vec(), + )?; self.emit_edge(anchor_vname, target_vname, "/kythe/edge/ref") } } diff --git a/kythe/rust/indexer/src/indexer/mod.rs b/kythe/rust/indexer/src/indexer/mod.rs index ea95f42ae7..9974a14e18 100644 --- a/kythe/rust/indexer/src/indexer/mod.rs +++ b/kythe/rust/indexer/src/indexer/mod.rs @@ -37,11 +37,7 @@ impl<'a> KytheIndexer<'a> { /// Accepts a CompilationUnit and the root directory of source files and /// indexes the CompilationUnit - pub fn index_cu( - &mut self, - unit: &CompilationUnit, - root_dir: &Path, - ) -> Result<(), KytheError> { + pub fn index_cu(&mut self, unit: &CompilationUnit, root_dir: &Path) -> Result<(), KytheError> { let mut generator = UnitAnalyzer::new(unit, self.writer, root_dir); // First, create file nodes for all of the source files in the CompilationUnit diff --git a/kythe/rust/indexer/src/indexer/offset.rs b/kythe/rust/indexer/src/indexer/offset.rs index 927691825e..8b47bd4747 100644 --- a/kythe/rust/indexer/src/indexer/offset.rs +++ b/kythe/rust/indexer/src/indexer/offset.rs @@ -22,7 +22,9 @@ pub struct OffsetIndex { impl OffsetIndex { /// Initialize an empty OffsetIndex pub fn new() -> Self { - Self { files: HashMap::new() } + Self { + files: HashMap::new(), + } } /// Adds a new file using the provided string content @@ -109,7 +111,12 @@ impl LineIndex { num_columns: u32, column_indices: Vec, ) -> Self { - Self { all_single_byte, offset, num_columns, column_indices } + Self { + all_single_byte, + offset, + num_columns, + column_indices, + } } /// Get the byte offset of the character on this line at the provided @@ -126,7 +133,9 @@ impl LineIndex { Some(self.offset + column - 1) } else { // Get column offset from vector if it exists - self.column_indices.get(column as usize - 1).map(|column_offset| self.offset + column_offset) + self.column_indices + .get(column as usize - 1) + .map(|column_offset| self.offset + column_offset) } } } diff --git a/kythe/rust/indexer/src/indexer/save_analysis.rs b/kythe/rust/indexer/src/indexer/save_analysis.rs index 2e117cc80a..b67d2cd203 100644 --- a/kythe/rust/indexer/src/indexer/save_analysis.rs +++ b/kythe/rust/indexer/src/indexer/save_analysis.rs @@ -46,7 +46,10 @@ impl AnalysisLoader for Loader { None } fn search_directories(&self) -> Vec { - vec![SearchDirectory { path: self.deps_dir.clone(), prefix_rewrite: None }] + vec![SearchDirectory { + path: self.deps_dir.clone(), + prefix_rewrite: None, + }] } } diff --git a/kythe/rust/indexer/src/providers.rs b/kythe/rust/indexer/src/providers.rs index cbfd89ff73..54cdaffe09 100644 --- a/kythe/rust/indexer/src/providers.rs +++ b/kythe/rust/indexer/src/providers.rs @@ -83,7 +83,10 @@ impl KzipFileProvider { path.to_str().unwrap().to_owned().replace("/", "") }; - Ok(Self { zip_archive, root_name }) + Ok(Self { + zip_archive, + root_name, + }) } /// Retrieve the Compilation Units from the kzip. @@ -126,7 +129,10 @@ impl FileProvider for KzipFileProvider { fn contents(&mut self, file_hash: &str) -> Result, KytheError> { // Ensure the file exists in the kzip let name = format!("{}/files/{}", self.root_name, file_hash); - let file = self.zip_archive.by_name(&name).map_err(|_| KytheError::FileNotFoundError)?; + let file = self + .zip_archive + .by_name(&name) + .map_err(|_| KytheError::FileNotFoundError)?; let mut reader = BufReader::new(file); let mut file_contents: Vec = Vec::new(); reader.read_to_end(&mut file_contents)?; diff --git a/kythe/rust/indexer/src/writer.rs b/kythe/rust/indexer/src/writer.rs index fdac17eb67..e4a60c0a2d 100644 --- a/kythe/rust/indexer/src/writer.rs +++ b/kythe/rust/indexer/src/writer.rs @@ -50,7 +50,9 @@ impl<'a> CodedOutputStreamWriter<'a> { /// /// Takes a writer that implements the `Write` trait as the only argument. pub fn new(writer: &'a mut dyn Write) -> CodedOutputStreamWriter<'a> { - Self { output_stream: CodedOutputStream::new(writer) } + Self { + output_stream: CodedOutputStream::new(writer), + } } } @@ -64,8 +66,12 @@ impl<'a> KytheWriter for CodedOutputStreamWriter<'a> { /// An error is returned if the write fails. fn write_entry(&mut self, entry: Entry) -> Result<(), KytheError> { let entry_size_bytes = entry.compute_size(); - self.output_stream.write_raw_varint32(entry_size_bytes).map_err(KytheError::WriterError)?; - entry.write_to_with_cached_sizes(&mut self.output_stream).map_err(KytheError::WriterError) + self.output_stream + .write_raw_varint32(entry_size_bytes) + .map_err(KytheError::WriterError)?; + entry + .write_to_with_cached_sizes(&mut self.output_stream) + .map_err(KytheError::WriterError) } fn flush(&mut self) -> Result<(), KytheError> { diff --git a/kythe/rust/indexer/testdata/struct.rs b/kythe/rust/indexer/testdata/struct.rs index 9cbdf8e686..e64740808a 100644 --- a/kythe/rust/indexer/testdata/struct.rs +++ b/kythe/rust/indexer/testdata/struct.rs @@ -21,4 +21,4 @@ impl _TestStruct { fn _test() {} } -fn main(){} +fn main() {} diff --git a/kythe/rust/indexer/testdata/type.rs b/kythe/rust/indexer/testdata/type.rs index e5fed85d00..50da9480d9 100644 --- a/kythe/rust/indexer/testdata/type.rs +++ b/kythe/rust/indexer/testdata/type.rs @@ -6,4 +6,4 @@ //- TestType aliases U32Type type _TestType = u32; -fn main(){} +fn main() {} diff --git a/kythe/rust/indexer/testdata/xrefs.rs b/kythe/rust/indexer/testdata/xrefs.rs index ee80da429f..dd86f478ce 100644 --- a/kythe/rust/indexer/testdata/xrefs.rs +++ b/kythe/rust/indexer/testdata/xrefs.rs @@ -21,7 +21,7 @@ fn add(arg1: u32, arg2: u32) -> u32 { //- @TestTrait defines/binding Trait trait TestTrait { //- @hello defines/binding TraitHelloFn - fn hello(){} + fn hello() {} } //- @TestStruct defines/binding Struct diff --git a/kythe/rust/indexer/testdata/xrefs/main.rs b/kythe/rust/indexer/testdata/xrefs/main.rs index dfbb63a7a1..71da250d3a 100644 --- a/kythe/rust/indexer/testdata/xrefs/main.rs +++ b/kythe/rust/indexer/testdata/xrefs/main.rs @@ -41,7 +41,7 @@ fn add(arg1: u32, arg2: u32) -> u32 { //- @TestTrait defines/binding Trait trait TestTrait { //- @hello defines/binding TraitHelloFn - fn hello(){} + fn hello() {} } //- @TestStruct defines/binding Struct diff --git a/kythe/rust/indexer/tests/array_writer.rs b/kythe/rust/indexer/tests/array_writer.rs index b680434013..b08511ed14 100644 --- a/kythe/rust/indexer/tests/array_writer.rs +++ b/kythe/rust/indexer/tests/array_writer.rs @@ -26,7 +26,9 @@ pub struct ArrayWriter { impl ArrayWriter { /// Returns a new ArrayWriter pub fn new() -> Self { - Self { entries: Vec::new() } + Self { + entries: Vec::new(), + } } } diff --git a/kythe/rust/indexer/tests/providers.rs b/kythe/rust/indexer/tests/providers.rs index 7b62b587a9..762ccd3914 100644 --- a/kythe/rust/indexer/tests/providers.rs +++ b/kythe/rust/indexer/tests/providers.rs @@ -40,15 +40,24 @@ fn test_kzip_provider() { // Check the `exists` function let file_hash = "c9d04c9565fc665c80681fb1d829938026871f66e14f501e08531df66938a789"; - assert!(kzip_provider.exists(file_hash), "File should exist in kzip but doesn't"); - assert!(!kzip_provider.exists("invalid"), "File shouldn't exist but does"); + assert!( + kzip_provider.exists(file_hash), + "File should exist in kzip but doesn't" + ); + assert!( + !kzip_provider.exists("invalid"), + "File shouldn't exist but does" + ); // Check the `contents` function let contents_result = kzip_provider.contents(file_hash); assert!(!contents_result.is_err()); let contents_string = String::from_utf8(contents_result.unwrap()).expect("File contents was not valid UTF-8"); - assert_eq!(contents_string, "Test\n", "File contents did not match expected contents"); + assert_eq!( + contents_string, "Test\n", + "File contents did not match expected contents" + ); let invalid_contents = kzip_provider.contents("invalid"); assert!(invalid_contents.is_err(), "Expected Err while reading contents for non-existent file, but received file contents: {:?}", invalid_contents.unwrap());