Skip to main content

Segment

Enum Segment 

Source
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>).

If you don’t need strong typing, you can use the type_erased method to drop the type information (producing char and &str). There is a typedef associated with this type for convenience (StrSegment).

You have two main things you can control about how segmentation happens; whether or not a contiguous chunk is broken into it’s largest “words” (runs of characters with no natural boundaries as defined by B), and whether or not you are returned the byte offset to the segment from the original data.

Normally, you’ll want chunks broken into their largest words. So we’ve named the variant that breaks chunks into their largest words simply segments.

See the respective functions for more details.

Variants§

§

Chunk(C)

A chunk (run of non-delimiter characters) of an identifier.

§

Delim(D)

A single delimiter.

Implementations§

Source§

impl<D> Segment<D, &str>

Source

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>

Source

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>

Source

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>>

Source

pub fn is_empty(&self) -> bool

Returns true if the contents of this segment is an empty chunk.

§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);
Source

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);
Source

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"));
Source

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"));
Source

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>

Source

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);
Source

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);
Source

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>>

Source

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);
Source

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);
Source

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>

Source

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);
Source

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>

Source

pub fn as_str(&self) -> &str
where D: AsRef<str>, C: AsRef<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");
Source

pub const fn is_delim(&self) -> bool

Returns true if the segment contains a delimiter, false if not.

§Examples
let delim = CamelSegment::Delim(LowLine);
assert!(delim.is_delim());

let chunk = CamelSegment::Chunk(CamelChunk::new("chunk")?);
assert!(!chunk.is_delim());
Source

pub fn is_delim_and(&self, pred: impl FnOnce(&D) -> bool) -> bool

Returns true if the segment contains a delimiter, and if the provided predicate passes. Returns false if not.

§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 == '_'));
Source

pub fn is_delim_or(&self, pred: impl FnOnce(&C) -> bool) -> bool

Returns true if the segment contains a delimiter, or if the segment contained a chunk, and provided predicate passes. Returns false otherwise.

§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"));
Source

pub const fn is_chunk(&self) -> bool

Returns true if the segment contains a chunk, false if not.

§Examples
let delim = CamelSegment::Delim(LowLine);
assert!(!delim.is_chunk());

let chunk = CamelSegment::Chunk(CamelChunk::new("chunk")?);
assert!(chunk.is_chunk());
Source

pub fn is_chunk_and(&self, pred: impl FnOnce(&C) -> bool) -> bool

Returns true if the segment contains a chunk, and if the provided predicate passes. Returns false otherwise.

§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"));
Source

pub fn is_chunk_or(&self, pred: impl FnOnce(&D) -> bool) -> bool

Returns true if the segment contains a chunk, or if the segment contained a delimiter, and provided predicate passes. Returns false otherwise.

§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 == '_'));
Source

pub fn delim(self) -> Option<D>

Returns Some if the segment is a delim, otherwise returns None.

let delim = CamelSegment::Delim(LowLine);
assert_eq!(delim.delim(), Some(LowLine));

let chunk = CamelSegment::Chunk(CamelChunk::new("chunk")?);
assert_eq!(chunk.delim(), None);
Source

pub fn chunk(self) -> Option<C>

Returns Some if the segment is a chunk, otherwise returns None.

let delim = CamelSegment::Delim(LowLine);
assert_eq!(delim.chunk(), None);

let chunk = CamelSegment::Chunk(CamelChunk::new("chunk")?);
assert_eq!(chunk.chunk(), Some(CamelChunk::new("chunk")?));

Trait Implementations§

Source§

impl<D: AsRef<str>, C: AsRef<str>> AsRef<str> for Segment<D, C>

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<D: Clone, C: Clone> Clone for Segment<D, C>

Source§

fn clone(&self) -> Segment<D, C>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<D: Copy, C: Copy> Copy for Segment<D, C>

Source§

impl<D: Debug, C: Debug> Debug for Segment<D, C>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<D: Display, C: Display> Display for Segment<D, C>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<D: Eq, C: Eq> Eq for Segment<D, C>

Source§

impl<'a, B, D, P> From<&'a Chunk<B, D, P>> for Segment<D, &'a Chunk<B, D, P>>

Source§

fn from(orig: &'a Chunk<B, D, P>) -> Self

Converts to this type from the input type.
Source§

impl<D: Delimiter, C> From<D> for Segment<D, C>

Source§

fn from(orig: D) -> Self

Converts to this type from the input type.
Source§

impl<D: Hash, C: Hash> Hash for Segment<D, C>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<D: Ord, C: Ord> Ord for Segment<D, C>

Source§

fn cmp(&self, other: &Segment<D, C>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl<D1, D2: PartialEq<D1>, C1, C2: PartialEq<C1>> PartialEq<Segment<D1, C1>> for Segment<D2, C2>

Source§

fn eq(&self, rhs: &Segment<D1, C1>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<D1, D2: PartialOrd<D1>, C1, C2: PartialOrd<C1>> PartialOrd<Segment<D1, C1>> for Segment<D2, C2>

Source§

fn partial_cmp(&self, rhs: &Segment<D1, C1>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a, B: Boundary, D: Delimiter, P: Profile> TryFrom<&'a str> for Segment<D, &'a Chunk<B, D, P>>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(orig: &'a str) -> Result<Self, Error>

Performs the conversion.

Auto Trait Implementations§

§

impl<D, C> Freeze for Segment<D, C>
where C: Freeze, D: Freeze,

§

impl<D, C> RefUnwindSafe for Segment<D, C>

§

impl<D, C> Send for Segment<D, C>
where C: Send, D: Send,

§

impl<D, C> Sync for Segment<D, C>
where C: Sync, D: Sync,

§

impl<D, C> Unpin for Segment<D, C>
where C: Unpin, D: Unpin,

§

impl<D, C> UnsafeUnpin for Segment<D, C>
where C: UnsafeUnpin, D: UnsafeUnpin,

§

impl<D, C> UnwindSafe for Segment<D, C>
where C: UnwindSafe, D: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.