complete raw identifier with "r#" prefix

This commit is contained in:
Hongxu Xu 2022-06-26 14:45:30 +08:00
parent 5bb123d970
commit f536766efb
12 changed files with 304 additions and 60 deletions

View file

@ -20,6 +20,9 @@ pub struct ModPath {
segments: Vec<Name>,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct EscapedModPath<'a>(&'a ModPath);
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum PathKind {
Plain,
@ -97,10 +100,12 @@ impl ModPath {
_ => None,
}
}
}
impl Display for ModPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
pub fn escaped(&self) -> EscapedModPath {
EscapedModPath(self)
}
fn _fmt(&self, f: &mut fmt::Formatter<'_>, escaped: bool) -> fmt::Result {
let mut first_segment = true;
let mut add_segment = |s| -> fmt::Result {
if !first_segment {
@ -127,12 +132,28 @@ impl Display for ModPath {
f.write_str("::")?;
}
first_segment = false;
segment.fmt(f)?;
if escaped {
segment.escaped().fmt(f)?
} else {
segment.fmt(f)?
};
}
Ok(())
}
}
impl Display for ModPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self._fmt(f, false)
}
}
impl<'a> Display for EscapedModPath<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0._fmt(f, true)
}
}
impl From<Name> for ModPath {
fn from(name: Name) -> ModPath {
ModPath::from_segments(PathKind::Plain, iter::once(name))