Update SourceFile::path to return the FileName struct
diff --git a/src/lib.rs b/src/lib.rs
index 25f61f7..d0660d4 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -106,13 +106,16 @@
}
}
+// Returned by reference, so we can't easily wrap it.
+pub use imp::FileName;
+
#[derive(Clone, PartialEq, Eq)]
pub struct SourceFile(imp::SourceFile);
impl SourceFile {
/// Get the path to this source file as a string.
- pub fn as_str(&self) -> &str {
- self.0.as_str()
+ pub fn path(&self) -> &FileName {
+ self.0.path()
}
pub fn is_real(&self) -> bool {
@@ -120,15 +123,9 @@
}
}
-impl AsRef<str> for SourceFile {
- fn as_ref(&self) -> &str {
- self.0.as_ref()
- }
-}
-
-impl PartialEq<str> for SourceFile {
- fn eq(&self, other: &str) -> bool {
- self.0.eq(other)
+impl AsRef<FileName> for SourceFile {
+ fn as_ref(&self) -> &FileName {
+ self.0.path()
}
}
diff --git a/src/stable.rs b/src/stable.rs
index 8b951e5..4bcaf6c 100644
--- a/src/stable.rs
+++ b/src/stable.rs
@@ -150,14 +150,23 @@
}
}
+#[derive(Clone, PartialEq, Eq, Debug)]
+pub struct FileName(String);
+
+impl fmt::Display for FileName {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ self.0.fmt(f)
+ }
+}
+
#[derive(Clone, PartialEq, Eq)]
pub struct SourceFile {
- name: String,
+ name: FileName,
}
impl SourceFile {
/// Get the path to this source file as a string.
- pub fn as_str(&self) -> &str {
+ pub fn path(&self) -> &FileName {
&self.name
}
@@ -167,22 +176,16 @@
}
}
-impl AsRef<str> for SourceFile {
- fn as_ref(&self) -> &str {
- self.as_str()
- }
-}
-
-impl PartialEq<str> for SourceFile {
- fn eq(&self, other: &str) -> bool {
- self.as_ref() == other
+impl AsRef<FileName> for SourceFile {
+ fn as_ref(&self) -> &FileName {
+ self.path()
}
}
impl fmt::Debug for SourceFile {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("SourceFile")
- .field("path", &self.as_str())
+ .field("path", &self.path())
.field("is_real", &self.is_real())
.finish()
}
@@ -299,7 +302,7 @@
let cm = cm.borrow();
let fi = cm.fileinfo(*self);
SourceFile {
- name: fi.name.clone(),
+ name: FileName(fi.name.clone()),
}
})
}
diff --git a/src/unstable.rs b/src/unstable.rs
index 3f4ef63..f4d1a41 100644
--- a/src/unstable.rs
+++ b/src/unstable.rs
@@ -160,12 +160,28 @@
}
#[derive(Clone, PartialEq, Eq)]
-pub struct SourceFile(proc_macro::SourceFile);
+pub struct FileName(String);
+
+impl fmt::Display for FileName {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ self.0.fmt(f)
+ }
+}
+
+// NOTE: We have to generate our own filename object here because we can't wrap
+// the one provided by proc_macro.
+#[derive(Clone, PartialEq, Eq)]
+pub struct SourceFile(proc_macro::SourceFile, FileName);
impl SourceFile {
+ fn new(sf: proc_macro::SourceFile) -> Self {
+ let filename = FileName(sf.path().to_string());
+ SourceFile(sf, filename)
+ }
+
/// Get the path to this source file as a string.
- pub fn as_str(&self) -> &str {
- self.0.as_str()
+ pub fn path(&self) -> &FileName {
+ &self.1
}
pub fn is_real(&self) -> bool {
@@ -173,15 +189,9 @@
}
}
-impl AsRef<str> for SourceFile {
- fn as_ref(&self) -> &str {
- self.0.as_ref()
- }
-}
-
-impl PartialEq<str> for SourceFile {
- fn eq(&self, other: &str) -> bool {
- self.0.eq(other)
+impl AsRef<FileName> for SourceFile {
+ fn as_ref(&self) -> &FileName {
+ self.path()
}
}
@@ -209,7 +219,7 @@
}
pub fn source_file(&self) -> SourceFile {
- SourceFile(self.0.source_file())
+ SourceFile::new(self.0.source_file())
}
pub fn start(&self) -> LineColumn {