#[allow(dead_code)]
pub mod jsonrpc_codes {
pub const PARSE_ERROR: i64 = -32700;
pub const INVALID_REQUEST: i64 = -32600;
pub const METHOD_NOT_FOUND: i64 = -32601;
pub const INVALID_PARAMS: i64 = -32602;
pub const INTERNAL_ERROR: i64 = -32603;
pub const SERVER_NOT_INITIALIZED: i64 = -32002;
}
#[derive(Debug)]
pub struct ProtocolError {
pub code: i64,
pub message: String,
}
#[allow(dead_code)] impl ProtocolError {
pub fn new(code: i64, message: impl Into<String>) -> Self {
Self {
code,
message: message.into(),
}
}
pub fn parse_error(message: impl Into<String>) -> Self {
Self::new(jsonrpc_codes::PARSE_ERROR, message)
}
pub fn invalid_request(message: impl Into<String>) -> Self {
Self::new(jsonrpc_codes::INVALID_REQUEST, message)
}
pub fn method_not_found(method: &str) -> Self {
Self::new(
jsonrpc_codes::METHOD_NOT_FOUND,
format!("method not found: {method}"),
)
}
pub fn invalid_params(message: impl Into<String>) -> Self {
Self::new(jsonrpc_codes::INVALID_PARAMS, message)
}
pub fn internal_error(message: impl Into<String>) -> Self {
Self::new(jsonrpc_codes::INTERNAL_ERROR, message)
}
pub fn server_not_initialized(message: impl Into<String>) -> Self {
Self::new(jsonrpc_codes::SERVER_NOT_INITIALIZED, message)
}
}
impl std::fmt::Display for ProtocolError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "[{}] {}", self.code, self.message)
}
}
impl std::error::Error for ProtocolError {}
#[derive(Debug)]
pub struct ToolError(pub String);
impl ToolError {
pub fn new(message: impl Into<String>) -> Self {
Self(message.into())
}
}
impl std::fmt::Display for ToolError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
impl std::error::Error for ToolError {}
impl From<sqlrite::SQLRiteError> for ToolError {
fn from(err: sqlrite::SQLRiteError) -> Self {
Self::new(err.to_string())
}
}