Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::{selfrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/, Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#};
2 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::{Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#};
3 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/::ToStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/ToString#;
4
5 use serderust-analyzer cargo serde 1.0.229 crate/::derust-analyzer cargo serde_core 1.0.229 de/::{selfrust-analyzer cargo serde_core 1.0.229 de/, Deserializerrust-analyzer cargo serde_core 1.0.229 de/Deserializer#, Errorrust-analyzer cargo serde_core 1.0.229 de/Error# as SerdeErrorrust-analyzer cargo serde_core 1.0.229 de/Error#, Visitorrust-analyzer cargo serde_core 1.0.229 de/Visitor#};
6 use serderust-analyzer cargo serde 1.0.229 crate/::Deserializerust-analyzer cargo serde_core 1.0.229 de/Deserialize#;
7
8 /// Represents a `compile_commands.json` file
9 pub type CompilationDatabaserust-analyzer cargo compile_commands 0.3.0 CompilationDatabase# = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<CompileCommandrust-analyzer cargo compile_commands 0.3.0 CompileCommand#>;
10
11 /// `All` if `CompilationDatabase` is generated from a `compile_flags.txt` file,
12 /// otherwise `File()` containing the `file` field from a `compile_commands.json`
13 /// entry
14 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)]
15 pub enum SourceFilerust-analyzer cargo compile_commands 0.3.0 SourceFile# {
16 Allrust-analyzer cargo compile_commands 0.3.0 SourceFile#All#,
17 Filerust-analyzer cargo compile_commands 0.3.0 SourceFile#File#(PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#),
18 }
19
20 impl<'de> Deserializerust-analyzer cargo serde_core 1.0.229 de/Deserialize#<'de> for SourceFilerust-analyzer cargo compile_commands 0.3.0 SourceFile# {
21 fn deserializerust-analyzer cargo compile_commands 0.3.0 impl#[SourceFile][`Deserialize<'de>`]deserialize().<D>(deserializer: D) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo compile_commands 0.3.0 impl#[SourceFile][`Deserialize<'de>`], D::Errorrust-analyzer cargo serde_core 1.0.229 de/Deserializer#Error#>
22 where
23 D: Deserializerrust-analyzer cargo serde_core 1.0.229 de/Deserializer#<'de>,
24 {
25 #[allow(dead_code)]
26 struct SourceFileVisitorrust-analyzer cargo compile_commands 0.3.0 SourceFileVisitor#;
27
28 impl<'de> Visitorrust-analyzer cargo serde_core 1.0.229 de/Visitor#<'de> for SourceFileVisitorrust-analyzer cargo compile_commands 0.3.0 SourceFileVisitor# {
29 type Valuerust-analyzer cargo compile_commands 0.3.0 impl#[SourceFileVisitor][`Visitor<'de>`]Value# = SourceFilerust-analyzer cargo compile_commands 0.3.0 SourceFile#;
30
31 fn expectingrust-analyzer cargo compile_commands 0.3.0 impl#[SourceFileVisitor][`Visitor<'de>`]expecting().(&self, formatter: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
32 formatter.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().("a string representing a file path")
33 }
34
35 fn visit_strrust-analyzer cargo compile_commands 0.3.0 impl#[SourceFileVisitor][`Visitor<'de>`]visit_str().<E>(self, value: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo compile_commands 0.3.0 impl#[SourceFileVisitor][`Visitor<'de>`]::Valuerust-analyzer cargo serde_core 1.0.229 de/Visitor#Value#, E>
36 where
37 E: SerdeErrorrust-analyzer cargo serde_core 1.0.229 de/Error#,
38 {
39 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(SourceFilerust-analyzer cargo compile_commands 0.3.0 SourceFile#::Filerust-analyzer cargo compile_commands 0.3.0 SourceFile#File#(PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][`From<&T>`]from().(value)))
40 }
41 }
42
43 match serde_jsonrust-analyzer cargo serde_json 1.0.151 crate/::Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::deserializerust-analyzer cargo serde_json 1.0.151 value/de/impl#[Value][`Deserialize<'de>`]deserialize().(deserializer)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). {
44 serde_jsonrust-analyzer cargo serde_json 1.0.151 crate/::Valuerust-analyzer cargo serde_json 1.0.151 value/Value#::Stringrust-analyzer cargo serde_json 1.0.151 value/Value#String#(s) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(SourceFilerust-analyzer cargo compile_commands 0.3.0 SourceFile#::Filerust-analyzer cargo compile_commands 0.3.0 SourceFile#File#(PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][`From<String>`]from().(s))),
45 _ => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(SerdeErrorrust-analyzer cargo serde_core 1.0.229 de/Error#::customrust-analyzer cargo serde_core 1.0.229 de/Error#custom().("expected a string")),
46 }
47 }
48 }
49
50 /// The `arguments` field in a `compile_commands.json` file can be invoked as is,
51 /// whereas the flags from a `compile_flags.txt` file must be invoked with a compiler,
52 /// e.g. gcc @compile_flags.txt. Because the `CompileCommand` struct is used to
53 /// represent both file types, we utilize a tagged union here to differentitate
54 /// between the two files
55 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)]
56 pub enum CompileArgsrust-analyzer cargo compile_commands 0.3.0 CompileArgs# {
57 Argumentsrust-analyzer cargo compile_commands 0.3.0 CompileArgs#Arguments#(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>),
58 Flagsrust-analyzer cargo compile_commands 0.3.0 CompileArgs#Flags#(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>),
59 }
60
61 impl<'de> Deserializerust-analyzer cargo serde_core 1.0.229 de/Deserialize#<'de> for CompileArgsrust-analyzer cargo compile_commands 0.3.0 CompileArgs# {
62 fn deserializerust-analyzer cargo compile_commands 0.3.0 impl#[CompileArgs][`Deserialize<'de>`]deserialize().<D>(deserializer: D) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo compile_commands 0.3.0 impl#[CompileArgs][`Deserialize<'de>`], D::Errorrust-analyzer cargo serde_core 1.0.229 de/Deserializer#Error#>
63 where
64 D: Deserializerrust-analyzer cargo serde_core 1.0.229 de/Deserializer#<'de>,
65 {
66 #[allow(dead_code)]
67 struct CompileArgVisitorrust-analyzer cargo compile_commands 0.3.0 CompileArgVisitor#;
68
69 impl<'de> Visitorrust-analyzer cargo serde_core 1.0.229 de/Visitor#<'de> for CompileArgVisitorrust-analyzer cargo compile_commands 0.3.0 CompileArgVisitor# {
70 type Valuerust-analyzer cargo compile_commands 0.3.0 impl#[CompileArgVisitor][`Visitor<'de>`]Value# = CompileArgsrust-analyzer cargo compile_commands 0.3.0 CompileArgs#;
71
72 fn expectingrust-analyzer cargo compile_commands 0.3.0 impl#[CompileArgVisitor][`Visitor<'de>`]expecting().(&self, formatter: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
73 formatter.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().("a string representing a command line argument")
74 }
75
76 fn visit_seqrust-analyzer cargo compile_commands 0.3.0 impl#[CompileArgVisitor][`Visitor<'de>`]visit_seq().<A>(self, mut seq: A) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo compile_commands 0.3.0 impl#[CompileArgVisitor][`Visitor<'de>`]::Valuerust-analyzer cargo serde_core 1.0.229 de/Visitor#Value#, A::Errorrust-analyzer cargo serde_core 1.0.229 de/SeqAccess#Error#>
77 where
78 A: derust-analyzer cargo serde_core 1.0.229 de/::SeqAccessrust-analyzer cargo serde_core 1.0.229 de/SeqAccess#<'de>,
79 {
80 let mut args = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
81
82 while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(arg) = seq.next_elementrust-analyzer cargo serde_core 1.0.229 de/SeqAccess#next_element().::<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). {
83 args.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(arg);
84 }
85
86 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(CompileArgsrust-analyzer cargo compile_commands 0.3.0 CompileArgs#::Argumentsrust-analyzer cargo compile_commands 0.3.0 CompileArgs#Arguments#(args))
87 }
88 }
89
90 deserializer.deserialize_seqrust-analyzer cargo serde_core 1.0.229 de/Deserializer#deserialize_seq().(CompileArgVisitorrust-analyzer cargo compile_commands 0.3.0 CompileArgVisitor#)
91 }
92 }
93
94 /// Represents a single entry within a `compile_commands.json` file, or a compile_flags.txt file
95 /// Either `arguments` or `command` is required. `arguments` is preferred, as shell (un)escaping
96 /// is a possible source of errors.
97 ///
98 /// See: <https://clang.llvm.org/docs/JSONCompilationDatabase.html#format>
99 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Deserializerust-analyzer cargo serde_derive 1.0.229 Deserialize:)]
100 pub struct CompileCommandrust-analyzer cargo compile_commands 0.3.0 CompileCommand# {
101 /// The working directory of the compilation. All paths specified in the `command`
102 /// or `file` fields must be either absolute or relative to this directory.
103 pub directoryrust-analyzer cargo compile_commands 0.3.0 CompileCommand#directory.: PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#,
104 /// The main translation unit source processed by this compilation step. This
105 /// is used by tools as the key into the compilation database. There can be
106 /// multiple command objects for the same file, for example if the same source
107 /// file is compiled with different configurations.
108 pub filerust-analyzer cargo compile_commands 0.3.0 CompileCommand#file.: SourceFilerust-analyzer cargo compile_commands 0.3.0 SourceFile#,
109 /// The compile command argv as list of strings. This should run the compilation
110 /// step for the translation unit file. arguments[0] should be the executable
111 /// name, such as clang++. Arguments should not be escaped, but ready to pass
112 /// to execvp().
113 pub argumentsrust-analyzer cargo compile_commands 0.3.0 CompileCommand#arguments.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<CompileArgsrust-analyzer cargo compile_commands 0.3.0 CompileArgs#>,
114 /// The compile command as a single shell-escaped string. Arguments may be
115 /// shell quoted and escaped following platform conventions, with ‘"’ and ‘\’
116 /// being the only special characters. Shell expansion is not supported.
117 pub commandrust-analyzer cargo compile_commands 0.3.0 CompileCommand#command.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>,
118 /// The name of the output created by this compilation step. This field is optional.
119 /// It can be used to distinguish different processing modes of the same input
120 /// file.
121 pub outputrust-analyzer cargo compile_commands 0.3.0 CompileCommand#output.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#>,
122 }
123
124 impl Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for CompileCommandrust-analyzer cargo compile_commands 0.3.0 CompileCommand# {
125 fn fmtrust-analyzer cargo compile_commands 0.3.0 impl#[CompileCommand][Display]fmt().(&self, f: &mut stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
126 writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(f, "{{ \"directory\": \"{}\",", self.directoryrust-analyzer cargo compile_commands 0.3.0 CompileCommand#directory..displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
127
128 match &self.argumentsrust-analyzer cargo compile_commands 0.3.0 CompileCommand#arguments. {
129 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(CompileArgsrust-analyzer cargo compile_commands 0.3.0 CompileArgs#::Argumentsrust-analyzer cargo compile_commands 0.3.0 CompileArgs#Arguments#(arguments)) => {
130 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "\"arguments\": [")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
131 if arguments.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() {
132 writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(f, "],")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
133 } else {
134 for arg in arguments.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take().(arguments.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1) {
135 writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(f, "\"{arg}\", ")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
136 }
137 writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(f, "\"{}\"],", arguments[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().arguments.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). 1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
138 }
139 }
140 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(CompileArgsrust-analyzer cargo compile_commands 0.3.0 CompileArgs#::Flagsrust-analyzer cargo compile_commands 0.3.0 CompileArgs#Flags#(flags)) => {
141 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "\"flags\": [")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
142 if flags.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() {
143 writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(f, "],")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
144 } else {
145 for flag in flags.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take().(flags.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1) {
146 writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(f, "\"{flag}\", ")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
147 }
148 writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(f, "\"{}\"],", flags[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().flags.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). 1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
149 }
150 }
151 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => {}
152 }
153
154 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(command) = &self.commandrust-analyzer cargo compile_commands 0.3.0 CompileCommand#command. {
155 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "\"command\": \"{command}\"")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
156 }
157
158 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(output) = &self.outputrust-analyzer cargo compile_commands 0.3.0 CompileCommand#output. {
159 writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(f, "\"output\": \"{}\"", output.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
160 }
161
162 match &self.filerust-analyzer cargo compile_commands 0.3.0 CompileCommand#file. {
163 SourceFilerust-analyzer cargo compile_commands 0.3.0 SourceFile#::Allrust-analyzer cargo compile_commands 0.3.0 SourceFile#All# => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "\"file\": all }}")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
164 SourceFilerust-analyzer cargo compile_commands 0.3.0 SourceFile#::Filerust-analyzer cargo compile_commands 0.3.0 SourceFile#File#(file) => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "\"file\": \"{}\" }}", file.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
165 }
166
167 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
168 }
169 }
170
171 impl CompileCommandrust-analyzer cargo compile_commands 0.3.0 CompileCommand# {
172 /// Transforms the command field, if present, into a `Vec<String>` of equivalent
173 /// arguments
174 ///
175 /// Replaces escaped '"' and '\' characters with their respective literals
176 pub fn args_from_cmdrust-analyzer cargo compile_commands 0.3.0 impl#[CompileCommand]args_from_cmd().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>> {
177 let escaped = if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ref cmd) = self.commandrust-analyzer cargo compile_commands 0.3.0 CompileCommand#command. {
178 // "Arguments may be shell quoted and escaped following platform conventions,
179 // with ‘"’ and ‘\’ being the only special characters."
180 cmd.trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().().replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().("\\\\", "\\").replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().("\\\"", "\"")
181 } else {
182 return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
183 };
184
185 let mut args = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
186 let mut start: usize = 0;
187 let mut end: usize = 0;
188 let mut in_quotes = false;
189
190 for c in escaped.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().() {
191 if c rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().'"' {
192 in_quotes = !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().in_quotes;
193 end rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1;
194 } else if c.is_whitespacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_whitespace().() && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().in_quotes && start rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().end {
195 args.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(escaped[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index().start..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#end]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index()..to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().());
196 end rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1;
197 start = end;
198 } else {
199 end rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1;
200 }
201 }
202
203 if start rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().end {
204 args.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(escaped[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index().start..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#end]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index()..to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().());
205 }
206
207 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(args)
208 }
209 }
210
211 /// For simple projects, Clang tools also recognize a `compile_flags.txt` file.
212 /// This should contain one argument per line. The same flags will be used to
213 /// compile any file.
214 ///
215 /// See: <https://clang.llvm.org/docs/JSONCompilationDatabase.html#alternatives>
216 ///
217 /// This helper allows you to translate the contents of a `compile_flags.txt` file
218 /// to a `CompilationDatabase` object
219 #[must_use]
220 pub fn from_compile_flags_txtrust-analyzer cargo compile_commands 0.3.0 from_compile_flags_txt().(directory: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, contents: &str) -> CompilationDatabaserust-analyzer cargo compile_commands 0.3.0 CompilationDatabase# {
221 let args = CompileArgsrust-analyzer cargo compile_commands 0.3.0 CompileArgs#::Flagsrust-analyzer cargo compile_commands 0.3.0 CompileArgs#Flags#(contents.linesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]lines().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(ToStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/ToString#::to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().());
222 vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![CompileCommandrust-analyzer cargo compile_commands 0.3.0 CompileCommand# {
223 directoryrust-analyzer cargo compile_commands 0.3.0 CompileCommand#directory.: directory.to_path_bufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_path_buf().(),
224 filerust-analyzer cargo compile_commands 0.3.0 CompileCommand#file.: SourceFilerust-analyzer cargo compile_commands 0.3.0 SourceFile#::Allrust-analyzer cargo compile_commands 0.3.0 SourceFile#All#,
225 argumentsrust-analyzer cargo compile_commands 0.3.0 CompileCommand#arguments.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(args),
226 commandrust-analyzer cargo compile_commands 0.3.0 CompileCommand#command.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
227 outputrust-analyzer cargo compile_commands 0.3.0 CompileCommand#output.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
228 }]
229 }
230
231 #[cfg(test)]
232 mod testsrust-analyzer cargo compile_commands 0.3.0 tests/ {
233 use superrust-analyzer cargo compile_commands 0.3.0 crate/::*;
234
235 fn test_args_from_cmdrust-analyzer cargo compile_commands 0.3.0 tests/test_args_from_cmd().(comp_cmd: &CompileCommandrust-analyzer cargo compile_commands 0.3.0 CompileCommand#, expected_args: &Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<&str>) {
236 let translated_args = comp_cmd.args_from_cmdrust-analyzer cargo compile_commands 0.3.0 impl#[CompileCommand]args_from_cmd().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
237
238 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(expected_args.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() == translated_args.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().());
239 for (expected, actual) in expected_args.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(translated_args.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()) {
240 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(expected == actual);
241 }
242 }
243
244 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
245 fn it_translates_args_from_empty_cmdrust-analyzer cargo compile_commands 0.3.0 tests/it_translates_args_from_empty_cmd().() {
246 let comp_cmd = CompileCommandrust-analyzer cargo compile_commands 0.3.0 CompileCommand# {
247 directoryrust-analyzer cargo compile_commands 0.3.0 CompileCommand#directory.: PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]new().(),
248 filerust-analyzer cargo compile_commands 0.3.0 CompileCommand#file.: SourceFilerust-analyzer cargo compile_commands 0.3.0 SourceFile#::Allrust-analyzer cargo compile_commands 0.3.0 SourceFile#All#,
249 argumentsrust-analyzer cargo compile_commands 0.3.0 CompileCommand#arguments.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
250 commandrust-analyzer cargo compile_commands 0.3.0 CompileCommand#command.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`From<&str>`]from().("")),
251 outputrust-analyzer cargo compile_commands 0.3.0 CompileCommand#output.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
252 };
253
254 let expected_args: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<&str> = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
255 test_args_from_cmdrust-analyzer cargo compile_commands 0.3.0 tests/test_args_from_cmd().(&comp_cmd, &expected_args);
256 }
257
258 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
259 fn it_translates_args_from_cmd_1rust-analyzer cargo compile_commands 0.3.0 tests/it_translates_args_from_cmd_1().() {
260 let comp_cmd = CompileCommandrust-analyzer cargo compile_commands 0.3.0 CompileCommand# {
261 directoryrust-analyzer cargo compile_commands 0.3.0 CompileCommand#directory.: PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]new().(),
262 filerust-analyzer cargo compile_commands 0.3.0 CompileCommand#file.: SourceFilerust-analyzer cargo compile_commands 0.3.0 SourceFile#::Allrust-analyzer cargo compile_commands 0.3.0 SourceFile#All#,
263 argumentsrust-analyzer cargo compile_commands 0.3.0 CompileCommand#arguments.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
264 commandrust-analyzer cargo compile_commands 0.3.0 CompileCommand#command.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`From<&str>`]from().(
265 r#"/usr/bin/clang++ -Irelative -DSOMEDEF=\"With spaces, quotes and \\-es.\" -c -o file.o file.cc"#,
266 )),
267 outputrust-analyzer cargo compile_commands 0.3.0 CompileCommand#output.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
268 };
269
270 let expected_args: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<&str> = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![
271 "/usr/bin/clang++",
272 "-Irelative",
273 r#"-DSOMEDEF="With spaces, quotes and \-es.""#,
274 "-c",
275 "-o",
276 "file.o",
277 "file.cc",
278 ];
279 test_args_from_cmdrust-analyzer cargo compile_commands 0.3.0 tests/test_args_from_cmd().(&comp_cmd, &expected_args);
280 }
281 }