Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit 2d18d3d

Browse files
committed
Auto merge of #1394 - jabagawee:rls-analysis-serde, r=Xanewok
Add serde implementation to complement rustc-serialize We want to transition off of rustc-serialize and onto serde, so this commit sets up both to co-exist, gated by feature flags. Due to the fact that they implement the same functions, only one feature flag can be active at a time. To compile rls-analysis with serde, it is necessary to run cargo build with the `--no-default-features` flag and the `--features "serialize-serde"` flag. The tests in this commit will still work with the default feature flags but will need to be changed before the serde implementation can pass tests. Addresses #1354
2 parents 8eb4be1 + e4ac381 commit 2d18d3d

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

rls-analysis/Cargo.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,22 @@ exclude = [
1111
]
1212

1313
[dependencies]
14-
rustc-serialize = "0.3"
14+
rustc-serialize = { version = "0.3", optional = true }
1515
log = "0.4"
1616
rls-data = "= 0.18.2"
1717
rls-span = "0.4"
1818
derive-new = "0.5"
1919
fst = { version = "0.3", default-features = false }
2020
itertools = "0.7.3"
2121
json = "0.11.13"
22+
serde = { version = "1.0", optional = true }
23+
serde_json = { version = "1.0", optional = true }
2224

2325
[dev-dependencies]
2426
lazy_static = "1"
2527
env_logger = "0.5"
28+
29+
[features]
30+
default = ["serialize-rustc"]
31+
serialize-rustc = ["rustc-serialize", "rls-data/serialize-rustc", "rls-span/serialize-rustc"]
32+
serialize-serde = ["serde", "serde_json", "rls-data/serialize-serde", "rls-span/serialize-serde"]

rls-analysis/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ extern crate itertools;
1515
extern crate json;
1616
extern crate rls_data as data;
1717
extern crate rls_span as span;
18+
#[cfg(feature = "serialize-rustc")]
1819
extern crate rustc_serialize;
20+
#[cfg(feature = "serialize-serde")]
21+
extern crate serde;
22+
#[cfg(feature = "serialize-serde")]
23+
extern crate serde_json;
1924

2025
mod analysis;
2126
mod listings;

rls-analysis/src/raw.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ pub use data::{
1212
CratePreludeData, Def, DefKind, GlobalCrateId as CrateId, Import, Ref, Relation, RelationKind,
1313
SigElement, Signature, SpanData,
1414
};
15-
use json;
1615
use listings::{DirectoryListing, ListingKind};
1716
use {AnalysisLoader, Blacklist};
1817

@@ -123,7 +122,7 @@ fn read_crate_data(path: &Path) -> Option<Analysis> {
123122
Err(err)
124123
})
125124
.ok()?;
126-
let s = ::rustc_serialize::json::decode(&buf)
125+
let s = decode_buf(&buf)
127126
.or_else(|err| {
128127
warn!("deserialisation error: {:?}", err);
129128
json::parse(&buf)
@@ -157,6 +156,28 @@ fn read_crate_data(path: &Path) -> Option<Analysis> {
157156
s
158157
}
159158

159+
#[cfg(all(feature = "serialize-rustc", not(feature = "serialize-serde")))]
160+
fn decode_buf(buf: &str) -> Result<Option<Analysis>, rustc_serialize::json::DecoderError> {
161+
::rustc_serialize::json::decode(buf)
162+
}
163+
164+
#[cfg(all(feature = "serialize-serde", not(feature = "serialize-rustc")))]
165+
fn decode_buf(buf: &str) -> Result<Option<Analysis>, serde_json::Error> {
166+
::serde_json::from_str(buf)
167+
}
168+
169+
#[cfg(all(feature = "serialize-rustc", feature = "serialize-serde"))]
170+
fn decode_buf(buf: &str) -> Result<Option<Analysis>, Box<dyn std::error::Error>> {
171+
compile_error!("Features \"serialize-serde\" and \"serialize-rustc\" cannot both be enabled for this crate.")
172+
}
173+
174+
#[cfg(not(any(feature = "serialize-serde", feature = "serialize-rustc")))]
175+
fn decode_buf(buf: &str) -> Result<Option<Analysis>, Box<dyn std::error::Error>> {
176+
compile_error!(
177+
"Either feature \"serialize-serde\" or \"serialize-rustc\" must be enabled for this crate."
178+
)
179+
}
180+
160181
pub fn name_space_for_def_kind(dk: DefKind) -> char {
161182
match dk {
162183
DefKind::Enum

0 commit comments

Comments
 (0)