1 use std::fmt; 2 3 use crate::gen::rust::component::RustPathComponent; 4 use crate::gen::rust::ident::RustIdent; 5 use crate::gen::rust::path::RustPath; 6 7 #[derive(Eq, PartialEq, Debug, Clone)] 8 pub(crate) struct RustIdentWithPath { 9 pub path: RustPath, 10 pub ident: RustIdent, 11 } 12 13 impl RustIdentWithPath { new(s: String) -> RustIdentWithPath14 pub fn new(s: String) -> RustIdentWithPath { 15 let mut path = RustPath::from(s); 16 let ident = match path.path.path.pop() { 17 None => panic!("empty path"), 18 Some(RustPathComponent::Ident(ident)) => ident, 19 Some(RustPathComponent::Keyword(kw)) => { 20 panic!("last path component is a keyword: {}", kw) 21 } 22 }; 23 RustIdentWithPath { path, ident } 24 } 25 prepend_ident(&mut self, ident: RustIdent)26 pub fn prepend_ident(&mut self, ident: RustIdent) { 27 self.path.prepend_ident(ident) 28 } 29 to_path(&self) -> RustPath30 pub fn to_path(&self) -> RustPath { 31 self.path.clone().append_ident(self.ident.clone()) 32 } 33 } 34 35 impl<S: Into<String>> From<S> for RustIdentWithPath { from(s: S) -> Self36 fn from(s: S) -> Self { 37 RustIdentWithPath::new(s.into()) 38 } 39 } 40 41 impl fmt::Display for RustIdentWithPath { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result42 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 43 fmt::Display::fmt(&self.to_path(), f) 44 } 45 } 46