use crate::error::{Error, Result};
fn reject_control(identifier: &str) -> Result<()> {
if identifier.bytes().any(|b| b < 0x20 || b == 0x7f) {
return Err(Error::Validation("Identifier contains control character".into()));
}
Ok(())
}
fn push_wrapped(out: &mut String, segment: &str, wrap: char) {
out.push(wrap);
for c in segment.chars() {
if c == wrap {
out.push(wrap);
}
out.push(c);
}
out.push(wrap);
}
pub fn quote(identifier: &str, wrap: char) -> Result<String> {
if identifier == "*" {
return Ok("*".into());
}
reject_control(identifier)?;
let mut out = String::with_capacity(identifier.len() + 4);
if !identifier.contains('.') {
push_wrapped(&mut out, identifier, wrap);
return Ok(out);
}
let count = identifier.split('.').count();
for (index, segment) in identifier.split('.').enumerate() {
if index > 0 {
out.push('.');
}
if segment == "*" && index == count - 1 {
out.push('*');
} else {
push_wrapped(&mut out, segment, wrap);
}
}
Ok(out)
}
pub fn quote_literal(identifier: &str, wrap: char) -> Result<String> {
if identifier == "*" {
return Ok("*".into());
}
reject_control(identifier)?;
let mut out = String::with_capacity(identifier.len() + 2);
push_wrapped(&mut out, identifier, wrap);
Ok(out)
}