use std::fmt;
use std::str::FromStr;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum PositionEncoding {
Utf8,
#[default]
Utf16,
Utf32,
}
impl PositionEncoding {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Utf8 => "utf-8",
Self::Utf16 => "utf-16",
Self::Utf32 => "utf-32",
}
}
}
impl fmt::Display for PositionEncoding {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for PositionEncoding {
type Err = UnknownEncoding;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"utf-8" => Ok(Self::Utf8),
"utf-16" => Ok(Self::Utf16),
"utf-32" => Ok(Self::Utf32),
other => Err(UnknownEncoding(other.to_owned())),
}
}
}
#[derive(Debug, thiserror::Error)]
#[error("unknown position encoding: {0}")]
pub struct UnknownEncoding(pub String);
#[must_use]
pub fn negotiate(client_supported: &[PositionEncoding]) -> PositionEncoding {
client_supported
.first()
.copied()
.unwrap_or(PositionEncoding::Utf16)
}