rust_analyzer/
lib.rs

1//! Implementation of the LSP for rust-analyzer.
2//!
3//! This crate takes Rust-specific analysis results from ide and translates
4//! into LSP types.
5//!
6//! It also is the root of all state. `world` module defines the bulk of the
7//! state, and `main_loop` module defines the rules for modifying it.
8//!
9//! The `cli` submodule implements some batch-processing analysis, primarily as
10//! a debugging aid.
11
12/// Any toolchain less than this version will likely not work with rust-analyzer built from this revision.
13pub const MINIMUM_SUPPORTED_TOOLCHAIN_VERSION: semver::Version = semver::Version {
14    major: 1,
15    minor: 78,
16    patch: 0,
17    pre: semver::Prerelease::EMPTY,
18    build: semver::BuildMetadata::EMPTY,
19};
20
21pub mod cli;
22
23mod command;
24mod diagnostics;
25mod discover;
26mod flycheck;
27mod line_index;
28mod main_loop;
29mod mem_docs;
30mod op_queue;
31mod reload;
32mod target_spec;
33mod task_pool;
34mod test_runner;
35mod version;
36
37mod handlers {
38    pub(crate) mod dispatch;
39    pub(crate) mod notification;
40    pub(crate) mod request;
41}
42
43pub mod tracing {
44    pub mod config;
45    pub mod json;
46    pub use config::Config;
47    pub mod hprof;
48}
49
50pub mod config;
51mod global_state;
52pub mod lsp;
53use self::lsp::ext as lsp_ext;
54
55#[cfg(test)]
56mod integrated_benchmarks;
57
58use serde::de::DeserializeOwned;
59
60pub use crate::{
61    lsp::capabilities::server_capabilities, main_loop::main_loop, reload::ws_to_crate_graph,
62    version::version,
63};
64
65pub fn from_json<T: DeserializeOwned>(
66    what: &'static str,
67    json: &serde_json::Value,
68) -> anyhow::Result<T> {
69    serde_json::from_value(json.clone())
70        .map_err(|e| anyhow::format_err!("Failed to deserialize {what}: {e}; {json}"))
71}
72
73#[doc(hidden)]
74macro_rules! try_default_ {
75    ($it:expr $(,)?) => {
76        match $it {
77            Some(it) => it,
78            None => return Ok(Default::default()),
79        }
80    };
81}
82pub(crate) use try_default_ as try_default;