Update SourceFile::path to return the FileName struct

This commit is contained in:
Nika Layzell 2017-12-30 14:34:35 -05:00
parent 1ecb6ce8c3
commit b35a9a3d49
3 changed files with 47 additions and 37 deletions

View File

@ -106,13 +106,16 @@ impl TokenStream {
}
}
// 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 SourceFile {
}
}
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()
}
}

View File

@ -150,14 +150,23 @@ impl IntoIterator for TokenStream {
}
}
#[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 SourceFile {
}
}
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 @@ impl Span {
let cm = cm.borrow();
let fi = cm.fileinfo(*self);
SourceFile {
name: fi.name.clone(),
name: FileName(fi.name.clone()),
}
})
}

View File

@ -160,12 +160,28 @@ impl fmt::Debug for TokenTreeIter {
}
#[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 SourceFile {
}
}
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 @@ impl Span {
}
pub fn source_file(&self) -> SourceFile {
SourceFile(self.0.source_file())
SourceFile::new(self.0.source_file())
}
pub fn start(&self) -> LineColumn {