pub enum Segment<D, C> {
Chunk(C),
Delim(D),
}Expand description
A part of an identifier returned during segmentation.
The default type contents of a segment are:
D: The delimiter from the fragment/identifier.C: A chunk formed from the type parameters (&Chunk<B, D, P>).
You have two main things you can control about how segmentation happens;
- Whether or not indices are returned along with the segments.
- Whether or not we return the entire chunk, versus further splitting the
chunk into “words” based on the identifier’s defined
Boundary.
| No Indices | Indices | |
|---|---|---|
| Chunks | chunked_segments | chunked_segment_indices |
| Words | segments | segment_indices |
Normally, you’ll want chunks broken into words. So we’ve named the variant
that breaks chunks into words simply segments.
See the respective functions for more details.
§Type Erasure
If you don’t need strong typing, you can use the type_erased methods to
drop the type information (producing char and &str). There is a typedef
associated with this type for convenience (StrSegment).
There are different levels of type-erasure, depending on which type information you want to drop.
| Method | Delimiter Becomes… | Chunk Becomes… |
|---|---|---|
type_erased | char | &str |
type_erased_chunk | D (Unchanged) | &str |
type_erased_delim | char | C (Unchanged) |
Variants§
Implementations§
Source§impl<D> Segment<D, &str>
impl<D> Segment<D, &str>
Sourcepub fn into_owned(self) -> Segment<D, String>
pub fn into_owned(self) -> Segment<D, String>
Converts a type-erased segment into an owned representation.
§Examples
let segment = CamelSegment::Delim(LowLine)
.type_erased_chunk()
.into_owned();
assert_eq!(segment, CamelSegment::Delim(LowLine).type_erased_chunk());
let segment = CamelSegment::Chunk(CamelChunk::new("Upper")?)
.type_erased_chunk()
.into_owned();
assert_eq!(segment, StringSegment::Chunk(String::from("Upper")));Source§impl<D: Delimiter> Segment<D, String>
impl<D: Delimiter> Segment<D, String>
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the byte length of the string representation of self.
For delimiters, this will first cast as a character, then return the UTF-8 length of the character. For chunks, the length of the chunk is used directly.
§Examples
let segment = CamelSegment::Delim(LowLine)
.type_erased_chunk()
.into_owned();
assert_eq!(segment.len(), 1);
let segment = CamelSegment::Chunk(CamelChunk::new("Upper")?)
.type_erased_chunk()
.into_owned();
assert_eq!(segment.len(), 5);Source§impl Segment<char, String>
impl Segment<char, String>
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the byte length of the string representation of self.
For delimiters, this will first cast as a character, then return the UTF-8 length of the character. For chunks, the length of the chunk is used directly.
§Examples
let segment = CamelSegment::Delim(LowLine)
.type_erased()
.into_owned();
assert_eq!(segment.len(), 1);
let segment = CamelSegment::Chunk(CamelChunk::new("Upper")?)
.type_erased()
.into_owned();
assert_eq!(segment.len(), 5);Source§impl<'a, B, D: Delimiter, P> Segment<D, &'a Chunk<B, D, P>>
impl<'a, B, D: Delimiter, P> Segment<D, &'a Chunk<B, D, P>>
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the contents of this segment is an empty chunk.
Since a delimiter can never be empty, if the segment is a delimiter,
this will return false.
§Examples
let segment = CamelSegment::Delim(LowLine);
assert_eq!(segment.is_empty(), false);
let segment = CamelSegment::Chunk(CamelChunk::new("Upper")?);
assert_eq!(segment.is_empty(), false);
let segment = CamelSegment::Chunk(CamelChunk::new("")?);
assert_eq!(segment.is_empty(), true);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the byte length of the string representation of self.
For delimiters, this will first cast as a character, then return the UTF-8 length of the character. For chunks, the length of the chunk is used directly.
§Examples
let segment = CamelSegment::Delim(LowLine);
assert_eq!(segment.len(), 1);
let segment = CamelSegment::Chunk(CamelChunk::new("Upper")?);
assert_eq!(segment.len(), 5);Sourcepub fn type_erased(self) -> Segment<char, &'a str>
pub fn type_erased(self) -> Segment<char, &'a str>
Converts the segment to a type-erased representation.
The Delim variant will be converted to a char, and the Chunk
variant will be converted to a &str.
§Examples
let segment = CamelSegment::Delim(LowLine).type_erased();
assert!(segment.is_delim_and(|c| *c == '_'));
let segment = CamelSegment::Chunk(CamelChunk::new("Upper")?).type_erased();
assert!(segment.is_chunk_and(|c| *c == "Upper"));Sourcepub fn type_erased_delim(self) -> Segment<char, &'a Chunk<B, D, P>>
pub fn type_erased_delim(self) -> Segment<char, &'a Chunk<B, D, P>>
Converts a segment’s delimiter to a type-erased representation.
The Delim variant will be converted to a char, but the chunk
will remain strongly-typed.
§Examples
let segment = CamelSegment::Delim(LowLine).type_erased_delim();
assert!(segment.is_delim_and(|c| *c == '_'));
let segment = CamelSegment::Chunk(CamelChunk::new("Upper")?).type_erased_delim();
assert!(segment.is_chunk_and(|c| *c == "Upper"));Sourcepub fn type_erased_chunk(self) -> Segment<D, &'a str>
pub fn type_erased_chunk(self) -> Segment<D, &'a str>
Converts a segment’s chunk to a type-erased representation.
The Chunk variant will be converted to a &str, but the delimiter
will remain strongly-typed.
§Examples
let segment = CamelSegment::Delim(LowLine).type_erased_chunk();
assert!(segment.is_delim_and(|c| *c == '_'));
let segment = CamelSegment::Chunk(CamelChunk::new("Upper")?).type_erased_chunk();
assert!(segment.is_chunk_and(|c| *c == "Upper"));Source§impl<'a, D: Delimiter> Segment<D, &'a str>
impl<'a, D: Delimiter> Segment<D, &'a str>
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the contents of this segment is an empty chunk.
§Examples
let segment = CamelSegment::Delim(LowLine)
.type_erased_chunk();
assert_eq!(segment.is_empty(), false);
let segment = CamelSegment::Chunk(CamelChunk::new("Upper")?)
.type_erased_chunk();
assert_eq!(segment.is_empty(), false);
let segment = CamelSegment::Chunk(CamelChunk::new("")?)
.type_erased_chunk();
assert_eq!(segment.is_empty(), true);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the byte length of the string representation of self.
For delimiters, this will first cast as a character, then return the UTF-8 length of the character. For chunks, the length of the chunk is used directly.
§Examples
let segment = CamelSegment::Delim(LowLine)
.type_erased_chunk();
assert_eq!(segment.len(), 1);
let segment = CamelSegment::Chunk(CamelChunk::new("Upper")?)
.type_erased_chunk();
assert_eq!(segment.len(), 5);Sourcepub fn type_erased(self) -> Segment<char, &'a str>
pub fn type_erased(self) -> Segment<char, &'a str>
Converts the segment to a type-erased representation.
The Delim variant will be converted to a char, and the Chunk
variant will be converted to a &str.
§Examples
let segment = CamelSegment::Delim(LowLine)
.type_erased_chunk().type_erased();
assert!(segment.is_delim_and(|c| *c == '_'));
let segment = CamelSegment::Chunk(CamelChunk::new("Upper")?)
.type_erased_chunk().type_erased();
assert!(segment.is_chunk_and(|c| *c == "Upper"));Source§impl<'a, B, D, P> Segment<char, &'a Chunk<B, D, P>>
impl<'a, B, D, P> Segment<char, &'a Chunk<B, D, P>>
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the contents of this segment is an empty chunk.
§Examples
let segment = CamelSegment::Delim(LowLine)
.type_erased_delim();
assert_eq!(segment.is_empty(), false);
let segment = CamelSegment::Chunk(CamelChunk::new("Upper")?)
.type_erased_delim();
assert_eq!(segment.is_empty(), false);
let segment = CamelSegment::Chunk(CamelChunk::new("")?)
.type_erased_delim();
assert_eq!(segment.is_empty(), true);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the byte length of the string representation of self.
For character delimiters, this will return the UTF-8 length of the character. For chunks, the length of the chunk is used directly.
§Examples
let segment = CamelSegment::Delim(LowLine)
.type_erased_delim();
assert_eq!(segment.len(), 1);
let segment = CamelSegment::Chunk(CamelChunk::new("Upper")?)
.type_erased_delim();
assert_eq!(segment.len(), 5);Sourcepub fn type_erased(self) -> Segment<char, &'a str>
pub fn type_erased(self) -> Segment<char, &'a str>
Converts the segment to a type-erased representation.
The Chunk variant will be converted to a &str.
§Examples
let segment = CamelSegment::Delim(LowLine)
.type_erased_delim().type_erased();
assert!(segment.is_delim_and(|c| *c == '_'));
let segment = CamelSegment::Chunk(CamelChunk::new("Upper")?)
.type_erased_delim().type_erased();
assert!(segment.is_chunk_and(|c| *c == "Upper"));Source§impl Segment<char, &str>
impl Segment<char, &str>
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the contents of this segment is an empty chunk.
§Examples
let segment = CamelSegment::Delim(LowLine)
.type_erased();
assert_eq!(segment.is_empty(), false);
let segment = CamelSegment::Chunk(CamelChunk::new("Upper")?)
.type_erased();
assert_eq!(segment.is_empty(), false);
let segment = CamelSegment::Chunk(CamelChunk::new("")?)
.type_erased();
assert_eq!(segment.is_empty(), true);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the byte length of the string representation of self.
For character delimiters, this will return the UTF-8 length of the character. For chunks, the length of the chunk is used directly.
§Examples
let segment = CamelSegment::Delim(LowLine)
.type_erased();
assert_eq!(segment.len(), 1);
let segment = CamelSegment::Chunk(CamelChunk::new("Upper")?)
.type_erased();
assert_eq!(segment.len(), 5);Source§impl<D, C> Segment<D, C>
impl<D, C> Segment<D, C>
Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Returns the original string representation of this segment.
§Examples
let delim = CamelSegment::Delim(LowLine);
assert_eq!(delim.as_str(), "_");
let chunk = CamelSegment::Chunk(CamelChunk::new("chunk")?);
assert_eq!(chunk.as_str(), "chunk");Sourcepub const fn is_delim(&self) -> bool
pub const fn is_delim(&self) -> bool
Returns true for every delimiter.
§Examples
let delim = CamelSegment::Delim(LowLine);
assert!(delim.is_delim());
let chunk = CamelSegment::Chunk(CamelChunk::new("chunk")?);
assert!(!chunk.is_delim());Sourcepub fn is_delim_and(&self, pred: impl FnOnce(&D) -> bool) -> bool
pub fn is_delim_and(&self, pred: impl FnOnce(&D) -> bool) -> bool
Returns true if a delimiter and the predicate passes.
§Examples
let delim = CamelSegment::Delim(LowLine);
assert!(delim.is_delim_and(|d| *d == '_'));
assert!(!delim.is_delim_and(|d| *d == '-'));
let chunk = CamelSegment::Chunk(CamelChunk::new("chunk")?);
assert!(!chunk.is_delim_and(|d| *d == '_'));Sourcepub fn is_delim_or(&self, pred: impl FnOnce(&C) -> bool) -> bool
pub fn is_delim_or(&self, pred: impl FnOnce(&C) -> bool) -> bool
Returns true for every delimiter. For a chunk, returns the result of
the predicate.
§Examples
let delim = CamelSegment::Delim(LowLine);
assert!(delim.is_delim_or(|c| *c == "chunk"));
let chunk = CamelSegment::Chunk(CamelChunk::new("chunk")?);
assert!(chunk.is_delim_or(|c| *c == "chunk"));
assert!(!chunk.is_delim_or(|c| *c == "text"));Sourcepub const fn is_chunk(&self) -> bool
pub const fn is_chunk(&self) -> bool
Returns true for every chunk.
§Examples
let delim = CamelSegment::Delim(LowLine);
assert!(!delim.is_chunk());
let chunk = CamelSegment::Chunk(CamelChunk::new("chunk")?);
assert!(chunk.is_chunk());Sourcepub fn is_chunk_and(&self, pred: impl FnOnce(&C) -> bool) -> bool
pub fn is_chunk_and(&self, pred: impl FnOnce(&C) -> bool) -> bool
Returns true if a chunk and the predicate passes.
§Examples
let delim = CamelSegment::Delim(LowLine);
assert!(!delim.is_chunk_and(|c| *c == "chunk"));
let chunk = CamelSegment::Chunk(CamelChunk::new("chunk")?);
assert!(chunk.is_chunk_and(|c| *c == "chunk"));
assert!(!chunk.is_chunk_and(|c| *c == "text"));Sourcepub fn is_chunk_or(&self, pred: impl FnOnce(&D) -> bool) -> bool
pub fn is_chunk_or(&self, pred: impl FnOnce(&D) -> bool) -> bool
Returns true if a chunk. For a delimiter, returns the result of
the predicate.
§Examples
let delim = CamelSegment::Delim(LowLine);
assert!(delim.is_chunk_or(|d| *d == '_'));
assert!(!delim.is_chunk_or(|d| *d == '-'));
let chunk = CamelSegment::Chunk(CamelChunk::new("chunk")?);
assert!(chunk.is_chunk_or(|d| *d == '_'));