abin/string/
str_segment.rs1use crate::{AnyBin, AnyStr, BinSegment, Segment};
2
3#[derive(Debug, Clone)]
17pub enum StrSegment<'a, TBin: AnyBin> {
18 Slice(&'a str),
19 Static(&'static str),
20 Str(AnyStr<TBin>),
21 GivenString(String),
22 Char(char),
23 Empty,
24}
25
26impl<'a, TBin: AnyBin> Into<BinSegment<'a, TBin>> for StrSegment<'a, TBin> {
27 fn into(self) -> BinSegment<'a, TBin> {
28 match self {
29 StrSegment::Slice(slice) => BinSegment::Slice(slice.as_bytes()),
30 StrSegment::Static(slice) => BinSegment::Static(slice.as_bytes()),
31 StrSegment::Str(string) => BinSegment::Bin(string.into_bin()),
32 StrSegment::GivenString(string) => BinSegment::GivenVec(string.into_bytes()),
33 StrSegment::Char(chr) => BinSegment::Bytes128(chr.into()),
34 StrSegment::Empty => BinSegment::Empty,
35 }
36 }
37}
38
39impl<'a, TBin: AnyBin> From<&'static str> for StrSegment<'a, TBin> {
40 fn from(string: &'static str) -> Self {
41 Self::Static(string)
42 }
43}
44
45impl<'a, TBin: AnyBin> From<AnyStr<TBin>> for StrSegment<'a, TBin> {
46 fn from(any_str: AnyStr<TBin>) -> Self {
47 Self::Str(any_str)
48 }
49}
50
51impl<'a, TBin: AnyBin> From<String> for StrSegment<'a, TBin> {
52 fn from(string: String) -> Self {
53 Self::GivenString(string)
54 }
55}
56
57impl<'a, TBin: AnyBin> From<char> for StrSegment<'a, TBin> {
58 fn from(chr: char) -> Self {
59 Self::Char(chr)
60 }
61}
62
63impl<'a, TBin: AnyBin> Segment for StrSegment<'a, TBin> {
64 #[inline]
65 fn number_of_bytes(&self) -> usize {
66 match self {
67 StrSegment::Slice(slice) => slice.len(),
68 StrSegment::Static(slice) => slice.len(),
69 StrSegment::Str(string) => string.len(),
70 StrSegment::GivenString(string) => string.len(),
71 StrSegment::Empty => 0,
72 StrSegment::Char(char) => char.len_utf8(),
73 }
74 }
75
76 #[inline]
77 fn empty() -> Self {
78 Self::Empty
79 }
80}