Skip to content

Commit 747addb

Browse files
committed
Make ConfigData Ser and TOML De
This commit makes rust-analyzer::config module TOML ser and de.
1 parent 657b33b commit 747addb

File tree

14 files changed

+1375
-928
lines changed

14 files changed

+1375
-928
lines changed

Cargo.lock

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/base-db/src/input.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@ use vfs::{file_set::FileSet, AbsPathBuf, AnchoredPath, FileId, VfsPath};
1919
// Map from crate id to the name of the crate and path of the proc-macro. If the value is `None`,
2020
// then the crate for the proc-macro hasn't been build yet as the build data is missing.
2121
pub type ProcMacroPaths = FxHashMap<CrateId, Result<(Option<String>, AbsPathBuf), String>>;
22+
23+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
24+
pub struct SourceRootId(pub u32);
25+
2226
/// Files are grouped into source roots. A source root is a directory on the
2327
/// file systems which is watched for changes. Typically it corresponds to a
2428
/// Rust crate. Source roots *might* be nested: in this case, a file belongs to
2529
/// the nearest enclosing source root. Paths to files are always relative to a
2630
/// source root, and the analyzer does not know the root path of the source root at
2731
/// all. So, a file from one source root can't refer to a file in another source
2832
/// root by path.
29-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
30-
pub struct SourceRootId(pub u32);
31-
3233
#[derive(Clone, Debug, PartialEq, Eq)]
3334
pub struct SourceRoot {
3435
/// Sysroot or crates.io library.

crates/ide/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ use hir::ChangeWithProcMacros;
6464
use ide_db::{
6565
base_db::{
6666
salsa::{self, ParallelDatabase},
67-
CrateOrigin, Env, FileLoader, FileSet, SourceDatabase, VfsPath,
67+
CrateOrigin, Env, FileLoader, FileSet, SourceDatabase, SourceDatabaseExt, VfsPath,
6868
},
6969
prime_caches, symbol_index, FxHashMap, FxIndexSet, LineIndexDatabase,
7070
};
@@ -271,6 +271,10 @@ impl Analysis {
271271
self.with_db(|db| status::status(db, file_id))
272272
}
273273

274+
pub fn source_root(&self, file_id: FileId) -> Cancellable<SourceRootId> {
275+
self.with_db(|db| db.file_source_root(file_id))
276+
}
277+
274278
pub fn parallel_prime_caches<F>(&self, num_worker_threads: u8, cb: F) -> Cancellable<()>
275279
where
276280
F: Fn(ParallelPrimeCachesProgress) + Sync + std::panic::UnwindSafe,
@@ -280,7 +284,7 @@ impl Analysis {
280284

281285
/// Gets the text of the source file.
282286
pub fn file_text(&self, file_id: FileId) -> Cancellable<Arc<str>> {
283-
self.with_db(|db| db.file_text(file_id))
287+
self.with_db(|db| SourceDatabaseExt::file_text(db, file_id))
284288
}
285289

286290
/// Gets the syntax tree of the file.
@@ -290,7 +294,6 @@ impl Analysis {
290294

291295
/// Returns true if this file belongs to an immutable library.
292296
pub fn is_library_file(&self, file_id: FileId) -> Cancellable<bool> {
293-
use ide_db::base_db::SourceDatabaseExt;
294297
self.with_db(|db| db.source_root(db.file_source_root(file_id)).is_library)
295298
}
296299

crates/project-model/src/cfg_flag.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
use std::{fmt, str::FromStr};
55

66
use cfg::CfgOptions;
7+
use serde::Serialize;
78

8-
#[derive(Clone, Eq, PartialEq, Debug)]
9+
#[derive(Clone, Eq, PartialEq, Debug, Serialize)]
910
pub enum CfgFlag {
1011
Atom(String),
1112
KeyValue { key: String, value: String },

crates/project-model/src/project_json.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
use base_db::{CrateDisplayName, CrateName};
5353
use paths::{AbsPath, AbsPathBuf, Utf8PathBuf};
5454
use rustc_hash::FxHashMap;
55-
use serde::{de, Deserialize};
55+
use serde::{de, Deserialize, Serialize};
5656
use span::Edition;
5757

5858
use crate::cfg_flag::CfgFlag;
@@ -161,14 +161,14 @@ impl ProjectJson {
161161
}
162162
}
163163

164-
#[derive(Deserialize, Debug, Clone)]
164+
#[derive(Serialize, Deserialize, Debug, Clone)]
165165
pub struct ProjectJsonData {
166166
sysroot: Option<Utf8PathBuf>,
167167
sysroot_src: Option<Utf8PathBuf>,
168168
crates: Vec<CrateData>,
169169
}
170170

171-
#[derive(Deserialize, Debug, Clone)]
171+
#[derive(Serialize, Deserialize, Debug, Clone)]
172172
struct CrateData {
173173
display_name: Option<String>,
174174
root_module: Utf8PathBuf,
@@ -190,7 +190,7 @@ struct CrateData {
190190
repository: Option<String>,
191191
}
192192

193-
#[derive(Deserialize, Debug, Clone)]
193+
#[derive(Serialize, Deserialize, Debug, Clone)]
194194
#[serde(rename = "edition")]
195195
enum EditionData {
196196
#[serde(rename = "2015")]
@@ -218,20 +218,21 @@ impl From<EditionData> for Edition {
218218
///
219219
/// This will differ from `CrateId` when multiple `ProjectJson`
220220
/// workspaces are loaded.
221-
#[derive(Deserialize, Debug, Clone, Copy, Eq, PartialEq, Hash)]
221+
#[derive(Serialize, Deserialize, Debug, Clone, Copy, Eq, PartialEq, Hash)]
222222
#[serde(transparent)]
223223
pub struct CrateArrayIdx(pub usize);
224224

225-
#[derive(Deserialize, Debug, Clone, Eq, PartialEq)]
225+
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
226226
pub(crate) struct Dep {
227227
/// Identifies a crate by position in the crates array.
228228
#[serde(rename = "crate")]
229229
pub(crate) krate: CrateArrayIdx,
230+
#[serde(serialize_with = "serialize_crate_name")]
230231
#[serde(deserialize_with = "deserialize_crate_name")]
231232
pub(crate) name: CrateName,
232233
}
233234

234-
#[derive(Deserialize, Debug, Clone)]
235+
#[derive(Serialize, Deserialize, Debug, Clone)]
235236
struct CrateSource {
236237
include_dirs: Vec<Utf8PathBuf>,
237238
exclude_dirs: Vec<Utf8PathBuf>,
@@ -244,3 +245,10 @@ where
244245
let name = String::deserialize(de)?;
245246
CrateName::new(&name).map_err(|err| de::Error::custom(format!("invalid crate name: {err:?}")))
246247
}
248+
249+
fn serialize_crate_name<S>(name: &CrateName, se: S) -> Result<S::Ok, S::Error>
250+
where
251+
S: serde::Serializer,
252+
{
253+
se.serialize_str(name)
254+
}

crates/rust-analyzer/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ tracing.workspace = true
3939
tracing-subscriber.workspace = true
4040
tracing-tree.workspace = true
4141
triomphe.workspace = true
42+
toml = "0.8.8"
4243
nohash-hasher.workspace = true
4344
always-assert = "0.2.0"
4445
walkdir = "2.3.2"
4546
semver.workspace = true
4647
memchr = "2.7.1"
48+
indexmap = { version = "2.0.0", features = ["serde"] }
4749

4850
cfg.workspace = true
4951
flycheck.workspace = true

0 commit comments

Comments
 (0)