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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
pub trait StringEncoding {
    fn new() -> Self;

    fn get_encoded_length(string: &str) -> usize;

    fn encoded_length_to_byte_count(string: &str, length: usize) -> usize;
}

#[derive(Debug, PartialEq, Eq)]
pub struct ByteWiseEncoding;

impl StringEncoding for ByteWiseEncoding {
    fn new() -> Self {
        Self
    }

    fn get_encoded_length(string: &str) -> usize {
        string.len()
    }

    fn encoded_length_to_byte_count(_string: &str, length: usize) -> usize {
        length
    }
}

#[derive(Debug, PartialEq, Eq)]
pub struct Utf8;

impl StringEncoding for Utf8 {
    fn new() -> Self {
        Self
    }

    fn get_encoded_length(string: &str) -> usize {
        string.chars().count()
    }

    fn encoded_length_to_byte_count(string: &str, length: usize) -> usize {
        string.chars().take(length).map(char::len_utf8).count()
    }
}