Make a Char trait that provides some handy methods for Object::Char
This commit is contained in:
		
							parent
							
								
									fd5df91e27
								
							
						
					
					
						commit
						1430bdad29
					
				
					 1 changed files with 25 additions and 5 deletions
				
			
		| 
						 | 
					@ -7,6 +7,11 @@ mod names;
 | 
				
			||||||
use object::Object;
 | 
					use object::Object;
 | 
				
			||||||
use predicates::IsChar;
 | 
					use predicates::IsChar;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub trait Char {
 | 
				
			||||||
 | 
					    fn from_char(c: char) -> Object;
 | 
				
			||||||
 | 
					    fn from_char_named(name: &str) -> Option<Object>;
 | 
				
			||||||
 | 
					    fn char_name(&self) -> Option<String>;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl IsChar for Object {
 | 
					impl IsChar for Object {
 | 
				
			||||||
    fn is_char(&self) -> bool {
 | 
					    fn is_char(&self) -> bool {
 | 
				
			||||||
| 
						 | 
					@ -17,20 +22,28 @@ impl IsChar for Object {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl Object {
 | 
					impl Char for Object {
 | 
				
			||||||
    pub fn from_char(c: char) -> Object {
 | 
					    fn from_char(c: char) -> Object {
 | 
				
			||||||
        Object::Char(c)
 | 
					        Object::Char(c)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub fn from_char_name(name: &str) -> Option<Object> {
 | 
					    fn from_char_named(name: &str) -> Option<Object> {
 | 
				
			||||||
        names::char_for(name).map(|c| Object::from_char(c))
 | 
					        names::char_for(name).map(|c| Object::from_char(c))
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn char_name(&self) -> Option<String> {
 | 
				
			||||||
 | 
					        match *self {
 | 
				
			||||||
 | 
					            Object::Char(c) => Some(names::name_of(c).map_or(c.to_string(), String::from)),
 | 
				
			||||||
 | 
					            _ => None,
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[cfg(test)]
 | 
					#[cfg(test)]
 | 
				
			||||||
mod tests {
 | 
					mod tests {
 | 
				
			||||||
    use object::Object;
 | 
					    use object::Object;
 | 
				
			||||||
    use predicates::{IsBool, IsChar};
 | 
					    use predicates::{IsBool, IsChar};
 | 
				
			||||||
 | 
					    use super::Char;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    #[test]
 | 
					    #[test]
 | 
				
			||||||
    fn chars_are_chars() {
 | 
					    fn chars_are_chars() {
 | 
				
			||||||
| 
						 | 
					@ -45,7 +58,14 @@ mod tests {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    #[test]
 | 
					    #[test]
 | 
				
			||||||
    fn named_chars_are_created() {
 | 
					    fn named_chars_are_created() {
 | 
				
			||||||
        assert_eq!(Object::from_char_name("newline"), Some(Object::from_char('\n')));
 | 
					        assert_eq!(Object::from_char_named("newline"), Some(Object::from_char('\n')));
 | 
				
			||||||
        assert_eq!(Object::from_char_name("asdf"), None);
 | 
					        assert_eq!(Object::from_char_named("asdf"), None);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    #[test]
 | 
				
			||||||
 | 
					    fn chars_have_names() {
 | 
				
			||||||
 | 
					        assert_eq!(Object::from_char('a').char_name(), Some(String::from("a")));
 | 
				
			||||||
 | 
					        assert_eq!(Object::from_char('\n').char_name(), Some(String::from("newline")));
 | 
				
			||||||
 | 
					        assert_eq!(Object::Bool(true).char_name(), None);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue