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