From c002322bde06a73c8cfa02cd1cbe33cf225da47b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 15 Jun 2020 13:29:07 +0200 Subject: New VFS API --- crates/vfs/src/vfs_path.rs | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 crates/vfs/src/vfs_path.rs (limited to 'crates/vfs/src/vfs_path.rs') diff --git a/crates/vfs/src/vfs_path.rs b/crates/vfs/src/vfs_path.rs new file mode 100644 index 000000000..de5dc0bf3 --- /dev/null +++ b/crates/vfs/src/vfs_path.rs @@ -0,0 +1,49 @@ +//! Abstract-ish representation of paths for VFS. +use std::fmt; + +use paths::{AbsPath, AbsPathBuf}; + +/// Long-term, we want to support files which do not reside in the file-system, +/// so we treat VfsPaths as opaque identifiers. +#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)] +pub struct VfsPath(VfsPathRepr); + +impl VfsPath { + pub fn as_path(&self) -> Option<&AbsPath> { + match &self.0 { + VfsPathRepr::PathBuf(it) => Some(it.as_path()), + } + } + pub fn join(&self, path: &str) -> VfsPath { + match &self.0 { + VfsPathRepr::PathBuf(it) => { + let res = it.join(path).normalize(); + VfsPath(VfsPathRepr::PathBuf(res)) + } + } + } + pub fn pop(&mut self) -> bool { + match &mut self.0 { + VfsPathRepr::PathBuf(it) => it.pop(), + } + } +} + +#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)] +enum VfsPathRepr { + PathBuf(AbsPathBuf), +} + +impl From for VfsPath { + fn from(v: AbsPathBuf) -> Self { + VfsPath(VfsPathRepr::PathBuf(v)) + } +} + +impl fmt::Display for VfsPath { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match &self.0 { + VfsPathRepr::PathBuf(it) => fmt::Display::fmt(&it.display(), f), + } + } +} -- cgit v1.2.3