]> git.lizzy.rs Git - rust.git/blob - crates/rust-analyzer/src/lib.rs
internal: prepare to track changes to mem_docs
[rust.git] / crates / rust-analyzer / src / 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 #![recursion_limit = "512"]
12
13 pub mod cli;
14
15 #[allow(unused)]
16 macro_rules! eprintln {
17     ($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
18 }
19
20 mod global_state;
21 mod reload;
22 mod main_loop;
23 mod dispatch;
24 mod handlers;
25 mod caps;
26 mod cargo_target_spec;
27 mod to_proto;
28 mod from_proto;
29 mod semantic_tokens;
30 mod markdown;
31 mod diagnostics;
32 mod line_index;
33 mod request_metrics;
34 mod lsp_utils;
35 mod thread_pool;
36 mod mem_docs;
37 mod diff;
38 mod op_queue;
39 pub mod lsp_ext;
40 pub mod config;
41
42 #[cfg(test)]
43 mod integrated_benchmarks;
44
45 use serde::de::DeserializeOwned;
46 use std::fmt;
47
48 pub use crate::{caps::server_capabilities, main_loop::main_loop};
49
50 pub type Error = Box<dyn std::error::Error + Send + Sync>;
51 pub type Result<T, E = Error> = std::result::Result<T, E>;
52
53 pub fn from_json<T: DeserializeOwned>(what: &'static str, json: serde_json::Value) -> Result<T> {
54     let res = serde_path_to_error::deserialize(&json)
55         .map_err(|e| format!("Failed to deserialize {}: {}; {}", what, e, json))?;
56     Ok(res)
57 }
58
59 #[derive(Debug)]
60 struct LspError {
61     code: i32,
62     message: String,
63 }
64
65 impl LspError {
66     fn new(code: i32, message: String) -> LspError {
67         LspError { code, message }
68     }
69 }
70
71 impl fmt::Display for LspError {
72     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
73         write!(f, "Language Server request failed with {}. ({})", self.code, self.message)
74     }
75 }
76
77 impl std::error::Error for LspError {}