Skip to main content

typed_ident/alloc/
segment.rs

1// =============================================================================
2// USES
3// =============================================================================
4
5// -----------------------------------------------------------------------------
6use crate::Segment;
7use crate::syntax::Delimiter;
8use std_alloc::string::String;
9
10// =============================================================================
11// IMPLS
12// =============================================================================
13
14// -----------------------------------------------------------------------------
15impl<D> Segment<D, &str> {
16    /// Converts a type-erased segment into an owned representation.
17    ///
18    /// # Examples
19    ///
20    /// ```
21    /// # use typed_ident::*;
22    /// # use typed_ident::syntax::delimiter::*;
23    /// # use typed_ident::presets::unicode::camel::*;
24    /// let segment = CamelSegment::Delim(LowLine)
25    ///     .type_erased_chunk()
26    ///     .into_owned();
27    /// assert_eq!(segment, CamelSegment::Delim(LowLine).type_erased_chunk());
28    ///
29    /// let segment = CamelSegment::Chunk(CamelChunk::new("Upper")?)
30    ///     .type_erased_chunk()
31    ///     .into_owned();
32    /// assert_eq!(segment, StringSegment::Chunk(String::from("Upper")));
33    /// # Ok::<(), typed_ident::Error>(())
34    /// ```
35    #[must_use]
36    #[inline]
37    pub fn into_owned(self) -> Segment<D, String> {
38        match self {
39            Segment::Chunk(chunk) => Segment::Chunk(String::from(chunk)),
40            Segment::Delim(delim) => Segment::Delim(delim),
41        }
42    }
43}
44
45// -----------------------------------------------------------------------------
46impl<D: Delimiter> Segment<D, String> {
47    /// Returns the byte length of the string representation of `self`.
48    ///
49    /// For delimiters, this will first cast as a character, then return the
50    /// UTF-8 length of the character. For chunks, the length of the chunk is
51    /// used directly.
52    ///
53    /// # Examples
54    ///
55    /// ```
56    /// # use typed_ident::syntax::delimiter::*;
57    /// # use typed_ident::presets::unicode::camel::*;
58    /// let segment = CamelSegment::Delim(LowLine)
59    ///     .type_erased_chunk()
60    ///     .into_owned();
61    /// assert_eq!(segment.len(), 1);
62    ///
63    /// let segment = CamelSegment::Chunk(CamelChunk::new("Upper")?)
64    ///     .type_erased_chunk()
65    ///     .into_owned();
66    /// assert_eq!(segment.len(), 5);
67    /// # Ok::<(), typed_ident::Error>(())
68    /// ```
69    pub fn len(&self) -> usize {
70        match self {
71            Self::Delim(delim) => delim.as_char().len_utf8(),
72            Self::Chunk(chunk) => chunk.len(),
73        }
74    }
75}
76
77// -----------------------------------------------------------------------------
78impl Segment<char, String> {
79    /// Returns the byte length of the string representation of `self`.
80    ///
81    /// For delimiters, this will first cast as a character, then return the
82    /// UTF-8 length of the character. For chunks, the length of the chunk is
83    /// used directly.
84    ///
85    /// # Examples
86    ///
87    /// ```
88    /// # use typed_ident::syntax::delimiter::*;
89    /// # use typed_ident::presets::unicode::camel::*;
90    /// let segment = CamelSegment::Delim(LowLine)
91    ///     .type_erased()
92    ///     .into_owned();
93    /// assert_eq!(segment.len(), 1);
94    ///
95    /// let segment = CamelSegment::Chunk(CamelChunk::new("Upper")?)
96    ///     .type_erased()
97    ///     .into_owned();
98    /// assert_eq!(segment.len(), 5);
99    /// # Ok::<(), typed_ident::Error>(())
100    /// ```
101    pub fn len(&self) -> usize {
102        match self {
103            Self::Delim(delim) => delim.len_utf8(),
104            Self::Chunk(chunk) => chunk.len(),
105        }
106    }
107}