use std::borrow::Cow;
use std::fmt;
#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct TmuxText {
bytes: Vec<u8>,
}
impl TmuxText {
pub fn from_bytes(bytes: impl Into<Vec<u8>>) -> Self {
Self {
bytes: bytes.into(),
}
}
#[must_use]
pub fn as_bytes(&self) -> &[u8] {
&self.bytes
}
pub fn as_str(&self) -> Result<&str, std::str::Utf8Error> {
std::str::from_utf8(&self.bytes)
}
#[must_use]
pub fn to_string_lossy(&self) -> Cow<'_, str> {
String::from_utf8_lossy(&self.bytes)
}
#[must_use]
pub fn as_flag(&self) -> Option<bool> {
match self.as_bytes() {
b"on" | b"yes" | b"1" => Some(true),
b"off" | b"no" | b"0" => Some(false),
_ => None,
}
}
#[must_use]
pub fn parse<T: std::str::FromStr>(&self) -> Option<T> {
self.as_str().ok()?.parse().ok()
}
}
impl From<&str> for TmuxText {
fn from(value: &str) -> Self {
Self::from_bytes(value.as_bytes())
}
}
impl From<String> for TmuxText {
fn from(value: String) -> Self {
Self::from_bytes(value.into_bytes())
}
}
impl PartialEq<str> for TmuxText {
fn eq(&self, other: &str) -> bool {
self.as_bytes() == other.as_bytes()
}
}
impl PartialEq<&str> for TmuxText {
fn eq(&self, other: &&str) -> bool {
self == *other
}
}
impl PartialEq<[u8]> for TmuxText {
fn eq(&self, other: &[u8]) -> bool {
self.as_bytes() == other
}
}
impl PartialEq<&[u8]> for TmuxText {
fn eq(&self, other: &&[u8]) -> bool {
self == *other
}
}
impl PartialEq<TmuxText> for [u8] {
fn eq(&self, other: &TmuxText) -> bool {
other == self
}
}
impl PartialEq<TmuxText> for str {
fn eq(&self, other: &TmuxText) -> bool {
other == self
}
}
impl PartialEq<TmuxText> for &str {
fn eq(&self, other: &TmuxText) -> bool {
other == *self
}
}
impl From<Vec<u8>> for TmuxText {
fn from(value: Vec<u8>) -> Self {
Self::from_bytes(value)
}
}
impl fmt::Debug for TmuxText {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("TmuxText(<redacted>)")
}
}