}
pub fn new(s: &str, encoding: &'static Encoding) -> Result<Identifier, Error> {
Self::is_plausible(s)?;
+ let identifier = Identifier(s.into());
+ identifier.check_encoding(encoding)?;
+ Ok(identifier)
+ }
+ /// Checks whether this is a valid identifier in the given `encoding`. An
+ /// identifier that is valid in one encoding might be invalid in another
+ /// because some characters are unencodable or because it is too long.
+ pub fn check_encoding(&self, encoding: &'static Encoding) -> Result<(), Error> {
+ let s = self.0.as_str();
let (encoded, _, unencodable) = encoding.encode(s);
if unencodable {
let mut encoder = encoding.new_encoder();
max: Self::MAX_LEN,
});
}
- Ok(Identifier(s.into()))
+ Ok(())
}
pub fn is_plausible(s: &str) -> Result<(), Error> {
if s.is_empty() {