cleanup
[pspp] / rust / src / identifier.rs
index 0553b41b119e2e8e5eb74d81f380400aef63dd25..8727bf1ea360454bdc15a911dc7ec1a0cd563f49 100644 (file)
@@ -1,3 +1,5 @@
+use std::fmt::{Display, Formatter, Result as FmtResult};
+
 use encoding_rs::{EncoderResult, Encoding};
 use finl_unicode::categories::{CharacterCategories, MajorCategory};
 use thiserror::Error as ThisError;
@@ -28,9 +30,6 @@ impl IdentifierChar for char {
     }
 }
 
-#[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.")]
@@ -72,6 +71,9 @@ fn is_reserved_word(s: &str) -> bool {
     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.
@@ -82,18 +84,30 @@ impl Identifier {
         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()))
     }
@@ -118,3 +132,9 @@ impl Identifier {
         Ok(())
     }
 }
+
+impl Display for Identifier {
+    fn fmt(&self, f: &mut Formatter) -> FmtResult {
+        write!(f, "{}", self.0)
+    }
+}