1use std::fmt;
8use std::str::FromStr;
9
10#[non_exhaustive]
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
13pub enum PositionEncoding {
14 Utf8,
16 #[default]
18 Utf16,
19 Utf32,
21}
22
23impl PositionEncoding {
24 #[must_use]
26 pub const fn as_str(self) -> &'static str {
27 match self {
28 Self::Utf8 => "utf-8",
29 Self::Utf16 => "utf-16",
30 Self::Utf32 => "utf-32",
31 }
32 }
33}
34
35impl fmt::Display for PositionEncoding {
36 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37 f.write_str(self.as_str())
38 }
39}
40
41impl FromStr for PositionEncoding {
42 type Err = UnknownEncoding;
43
44 fn from_str(s: &str) -> Result<Self, Self::Err> {
45 match s {
46 "utf-8" => Ok(Self::Utf8),
47 "utf-16" => Ok(Self::Utf16),
48 "utf-32" => Ok(Self::Utf32),
49 other => Err(UnknownEncoding(other.to_owned())),
50 }
51 }
52}
53
54#[derive(Debug, thiserror::Error)]
56#[error("unknown position encoding: {0}")]
57pub struct UnknownEncoding(pub String);
58
59#[must_use]
65pub fn negotiate(client_supported: &[PositionEncoding]) -> PositionEncoding {
66 client_supported
67 .first()
68 .copied()
69 .unwrap_or(PositionEncoding::Utf16)
70}