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