use serde::{Deserialize, Serialize};
use crate::protocol::{Request, Notification};
use crate::protocol::messages::MessageResult;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Root {
pub uri: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ListRootsRequest {
pub method: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub params: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ListRootsResult {
pub roots: Vec<Root>,
#[serde(rename = "_meta", skip_serializing_if = "Option::is_none")]
pub meta: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct RootsListChangedNotification {
pub method: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub params: Option<serde_json::Value>,
}
impl Request for ListRootsRequest {
const METHOD: &'static str = "roots/list";
fn method(&self) -> &str {
&self.method
}
fn params(&self) -> Option<&serde_json::Value> {
None
}
}
impl Notification for RootsListChangedNotification {
const METHOD: &'static str = "notifications/roots/list_changed";
fn method(&self) -> &str {
&self.method
}
fn params(&self) -> Option<&serde_json::Value> {
None
}
}
impl MessageResult for ListRootsResult {}
impl Root {
pub fn new(uri: impl Into<String>) -> Self {
Self {
uri: uri.into(),
name: None,
}
}
pub fn with_name(uri: impl Into<String>, name: impl Into<String>) -> Self {
Self {
uri: uri.into(),
name: Some(name.into()),
}
}
pub fn is_valid(&self) -> bool {
self.uri.starts_with("file://")
}
}
impl ListRootsRequest {
pub fn new() -> Self {
Self {
method: Self::METHOD.to_string(),
params: None,
}
}
}
impl ListRootsResult {
pub fn new(roots: Vec<Root>) -> Self {
Self {
roots,
meta: None,
}
}
pub fn with_single_root(uri: impl Into<String>) -> Self {
Self {
roots: vec![Root::new(uri)],
meta: None,
}
}
}
impl RootsListChangedNotification {
pub fn new() -> Self {
Self {
method: Self::METHOD.to_string(),
params: None,
}
}
}