1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
use crate::ChunkValidator;

/// Used for splitting a piece of text into chunks based on the number of
/// characters in each chunk.
///
/// ```
/// use text_splitter::{Characters, TextSplitter};
///
/// let splitter = TextSplitter::new(Characters);
/// ```
#[derive(Debug)]
pub struct Characters;

impl ChunkValidator for Characters {
    /// Determine if the given chunk still fits within the specified max chunk
    /// size, based on characters.
    ///
    /// ```
    /// use text_splitter::{Characters, ChunkValidator};
    ///
    /// assert!(Characters.validate_chunk("hello", 10));
    /// ```
    fn validate_chunk(&self, chunk: &str, chunk_size: usize) -> bool {
        chunk.chars().count() <= chunk_size
    }
}