Move Uri type to protocol crate

This commit is contained in:
Patrick Förster 2019-12-11 21:06:31 +01:00
parent 2825c1cd9d
commit 088615fff2
11 changed files with 16 additions and 20 deletions

View file

@ -3,12 +3,14 @@ mod client;
mod codec;
mod options;
mod range;
mod uri;
pub use self::capabilities::ClientCapabilitiesExt;
pub use self::client::{LatexLspClient, LspClient};
pub use self::codec::LspCodec;
pub use self::options::*;
pub use self::range::RangeExt;
pub use self::uri::{AsUri, Uri};
pub use lsp_types::*;
use serde::{Deserialize, Serialize};

View file

@ -0,0 +1,73 @@
use lsp_types::{TextDocumentIdentifier, TextDocumentPositionParams, Url};
use serde::{Deserialize, Serialize};
use std::fmt;
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::path::Path;
#[derive(Debug, Eq, Clone, Serialize, Deserialize)]
pub struct Uri(Url);
impl Uri {
pub fn from_file_path<P: AsRef<Path>>(path: P) -> Result<Self, ()> {
Url::from_file_path(path).map(|url| url.into())
}
}
impl PartialEq for Uri {
fn eq(&self, other: &Self) -> bool {
if cfg!(windows) {
self.as_str().to_lowercase() == other.as_str().to_lowercase()
} else {
self.as_str() == other.as_str()
}
}
}
impl Hash for Uri {
fn hash<H: Hasher>(&self, state: &mut H) {
self.as_str().to_lowercase().hash(state);
}
}
impl Deref for Uri {
type Target = Url;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<Url> for Uri {
fn from(url: Url) -> Self {
Uri(url)
}
}
impl Into<Url> for Uri {
fn into(self) -> Url {
self.0
}
}
impl fmt::Display for Uri {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
pub trait AsUri {
fn as_uri(&self) -> Uri;
}
impl AsUri for TextDocumentIdentifier {
fn as_uri(&self) -> Uri {
self.uri.clone().into()
}
}
impl AsUri for TextDocumentPositionParams {
fn as_uri(&self) -> Uri {
self.text_document.as_uri()
}
}