From 9085cb14d156e07ac9a93c5711fb750e876e7ff7 Mon Sep 17 00:00:00 2001 From: gibbz00 Date: Fri, 6 Oct 2023 20:42:15 +0200 Subject: [PATCH] Model `PositionEncodingKind` as an enum. --- src/lib.rs | 49 ++++++++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4253c76..18ae52e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -306,44 +306,23 @@ pub struct LocationLink { /// /// @since 3.17.0 #[derive(Debug, Eq, PartialEq, Hash, PartialOrd, Clone, Deserialize, Serialize)] -pub struct PositionEncodingKind(std::borrow::Cow<'static, str>); - -impl PositionEncodingKind { +pub enum PositionEncodingKind { /// Character offsets count UTF-8 code units. - pub const UTF8: PositionEncodingKind = PositionEncodingKind::new("utf-8"); - + #[serde(rename = "utf-8")] + Utf8, /// Character offsets count UTF-16 code units. /// /// This is the default and must always be supported /// by servers - pub const UTF16: PositionEncodingKind = PositionEncodingKind::new("utf-16"); - + #[serde(rename = "utf-16")] + Utf16, /// Character offsets count UTF-32 code units. /// /// Implementation note: these are the same as Unicode code points, /// so this `PositionEncodingKind` may also be used for an /// encoding-agnostic representation of character offsets. - pub const UTF32: PositionEncodingKind = PositionEncodingKind::new("utf-32"); - - pub const fn new(tag: &'static str) -> Self { - PositionEncodingKind(std::borrow::Cow::Borrowed(tag)) - } - - pub fn as_str(&self) -> &str { - &self.0 - } -} - -impl From for PositionEncodingKind { - fn from(from: String) -> Self { - PositionEncodingKind(std::borrow::Cow::from(from)) - } -} - -impl From<&'static str> for PositionEncodingKind { - fn from(from: &'static str) -> Self { - PositionEncodingKind::new(from) - } + #[serde(rename = "utf-32")] + Utf32, } /// Represents a diagnostic, such as a compiler error or warning. @@ -2846,7 +2825,7 @@ mod tests { #[test] fn test_resource_operation_kind() { test_serialization( - &vec![ + &[ ResourceOperationKind::Create, ResourceOperationKind::Rename, ResourceOperationKind::Delete, @@ -2854,4 +2833,16 @@ mod tests { r#"["create","rename","delete"]"#, ); } + + #[test] + fn serializes_position_encoding_kind() { + test_serialization( + &[ + PositionEncodingKind::Utf8, + PositionEncodingKind::Utf16, + PositionEncodingKind::Utf32, + ], + r#"["utf-8","utf-16","utf-32"]"#, + ) + } }