]> git.lizzy.rs Git - rust.git/blob - crates/rust-analyzer/src/lib.rs
Merge #4945
[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 ra_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_endings;
33 mod request_metrics;
34 mod lsp_utils;
35 mod thread_pool;
36 pub mod lsp_ext;
37 pub mod config;
38
39 use serde::de::DeserializeOwned;
40
41 pub type Result<T, E = Box<dyn std::error::Error + Send + Sync>> = std::result::Result<T, E>;
42 pub use crate::{caps::server_capabilities, main_loop::main_loop};
43 use std::fmt;
44
45 pub fn from_json<T: DeserializeOwned>(what: &'static str, json: serde_json::Value) -> Result<T> {
46     let res = T::deserialize(&json)
47         .map_err(|e| format!("Failed to deserialize {}: {}; {}", what, e, json))?;
48     Ok(res)
49 }
50
51 #[derive(Debug)]
52 struct LspError {
53     code: i32,
54     message: String,
55 }
56
57 impl LspError {
58     fn new(code: i32, message: String) -> LspError {
59         LspError { code, message }
60     }
61 }
62
63 impl fmt::Display for LspError {
64     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
65         write!(f, "Language Server request failed with {}. ({})", self.code, self.message)
66     }
67 }
68
69 impl std::error::Error for LspError {}