+use std::fmt::{Display, Formatter, Result as FmtResult};
+
use encoding_rs::{EncoderResult, Encoding};
use finl_unicode::categories::{CharacterCategories, MajorCategory};
use thiserror::Error as ThisError;
}
}
-#[derive(Clone, PartialEq, Eq, Debug, Hash)]
-pub struct Identifier(pub UniCase<String>);
-
#[derive(Clone, Debug, ThisError)]
pub enum Error {
#[error("Identifier cannot be empty string.")]
false
}
+#[derive(Clone, PartialEq, Eq, Debug, Hash)]
+pub struct Identifier(pub UniCase<String>);
+
impl Identifier {
/// Maximum length of an identifier, in bytes. The limit applies in the
/// encoding used by the dictionary, not in UTF-8.
let (encoded, _, unencodable) = encoding.encode(s);
if unencodable {
let mut encoder = encoding.new_encoder();
- let mut buf =
- Vec::with_capacity(encoder.max_buffer_length_from_utf8_without_replacement(s.len()).unwrap());
+ let mut buf = Vec::with_capacity(
+ encoder
+ .max_buffer_length_from_utf8_without_replacement(s.len())
+ .unwrap(),
+ );
let EncoderResult::Unmappable(c) = encoder
.encode_from_utf8_to_vec_without_replacement(s, &mut buf, true)
.0
else {
unreachable!();
};
- return Err(Error::NotEncodable { id: s.into(), encoding: encoding.name(), c });
+ return Err(Error::NotEncodable {
+ id: s.into(),
+ encoding: encoding.name(),
+ c,
+ });
}
if encoded.len() > Self::MAX_LEN {
- return Err(Error::TooLong { id: s.into(), length: encoded.len(), encoding: encoding.name(), max: Self::MAX_LEN });
+ return Err(Error::TooLong {
+ id: s.into(),
+ length: encoded.len(),
+ encoding: encoding.name(),
+ max: Self::MAX_LEN,
+ });
}
Ok(Identifier(s.into()))
}
Ok(())
}
}
+
+impl Display for Identifier {
+ fn fmt(&self, f: &mut Formatter) -> FmtResult {
+ write!(f, "{}", self.0)
+ }
+}