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