Add functions for byte characters and doc comments
Likely to be tweaked when the actual PR lands!
diff --git a/src/lib.rs b/src/lib.rs
index d7891cb..e1305eb 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -191,9 +191,17 @@
}
impl Literal {
+ pub fn bytechar(b: u8) -> Literal {
+ Literal(imp::Literal::bytechar(b))
+ }
+
pub fn bytestring(s: &[u8]) -> Literal {
Literal(imp::Literal::bytestring(s))
}
+
+ pub fn doccomment(s: &str) -> Literal {
+ Literal(imp::Literal::doccomment(s))
+ }
}
macro_rules! tys {
diff --git a/src/stable.rs b/src/stable.rs
index 1b18b8c..d30e5e0 100644
--- a/src/stable.rs
+++ b/src/stable.rs
@@ -1,5 +1,6 @@
extern crate unicode_xid;
+use std::ascii;
use std::borrow::Borrow;
use std::cell::RefCell;
use std::collections::HashMap;
@@ -213,6 +214,19 @@
pub struct Literal(String);
impl Literal {
+ pub fn bytechar(byte: u8) -> Literal {
+ match byte {
+ 0 => Literal(format!("b'\\0'")),
+ b'\"' => Literal(format!("b'\"'")),
+ n => {
+ let mut escaped = "b'".to_string();
+ escaped.extend(ascii::escape_default(n).map(|c| c as char));
+ escaped.push('\'');
+ Literal(escaped)
+ }
+ }
+ }
+
pub fn bytestring(bytes: &[u8]) -> Literal {
let mut escaped = "b\"".to_string();
for b in bytes {
@@ -230,6 +244,10 @@
escaped.push('"');
Literal(escaped)
}
+
+ pub fn doccomment(s: &str) -> Literal {
+ Literal(s.to_string())
+ }
}
impl fmt::Display for Literal {