webserver-base 0.1.23

A Rust library which contains shared logic for all of my webserver projects.
Documentation
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
use super::entity::Entity;
use super::utf16::utf16_len;

/// Room reserved for a `(1/3)` style indicator when a message is split.
const INDICATOR_RESERVE: usize = 12;

/// One message-sized piece of a larger message.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Chunk {
    pub(crate) text: String,
    pub(crate) entities: Vec<Entity>,
}

/// The result of fitting a message into Telegram's length limit.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct SplitOutcome {
    /// The pieces to send, in order.
    pub(crate) chunks: Vec<Chunk>,
    /// How many UTF-16 code units were discarded because the chunk cap was hit.
    pub(crate) dropped_units: usize,
}

/// Returns the byte index of the longest prefix of `text` fitting in `limit`
/// UTF-16 code units, preferring to cut at a natural boundary.
///
/// Preference order is paragraph break, then line break, then space, then a
/// hard cut at the limit. The returned index is always on a `char` boundary,
/// so a surrogate pair can never be severed.
fn find_cut(text: &str, limit: usize) -> usize {
    let mut units: usize = 0;
    let mut paragraph_end: usize = 0;
    let mut line_end: usize = 0;
    let mut space_end: usize = 0;
    let mut hard_end: usize = 0;
    let mut previous_was_newline: bool = false;

    for (byte_index, character) in text.char_indices() {
        let character_units: usize = character.len_utf16();

        if units + character_units > limit {
            break;
        }

        units += character_units;
        let end: usize = byte_index + character.len_utf8();
        hard_end = end;

        if character == '\n' {
            if previous_was_newline {
                paragraph_end = end;
            }
            line_end = end;
            previous_was_newline = true;
        } else {
            if character == ' ' {
                space_end = end;
            }
            previous_was_newline = false;
        }
    }

    // Everything fits.
    if hard_end == text.len() {
        return text.len();
    }

    let cut: usize = if paragraph_end > 0 {
        paragraph_end
    } else if line_end > 0 {
        line_end
    } else if space_end > 0 {
        space_end
    } else {
        hard_end
    };

    if cut > 0 {
        return cut;
    }

    // Defensive: a limit smaller than the first character would otherwise make
    // no progress and loop forever. Overshoot by one character instead.
    text.char_indices()
        .nth(1)
        .map_or_else(|| text.len(), |(index, _)| index)
}

/// Returns the byte index at which `text` reaches `units` UTF-16 code units.
fn byte_index_for_units(text: &str, units: usize) -> usize {
    let mut seen: usize = 0;

    for (byte_index, character) in text.char_indices() {
        if seen + character.len_utf16() > units {
            return byte_index;
        }
        seen += character.len_utf16();
    }

    text.len()
}

/// Restricts `entities` to the absolute UTF-16 range `[start, end)`, clamping
/// any which straddle a boundary and rebasing them onto the new chunk.
fn clamp_entities(entities: &[Entity], start: usize, end: usize) -> Vec<Entity> {
    entities
        .iter()
        .filter_map(|entity: &Entity| {
            let clamped_start: usize = entity.offset.max(start);
            let clamped_end: usize = entity.end().min(end);

            if clamped_start >= clamped_end {
                return None;
            }

            Some(Entity::new(
                entity.kind.clone(),
                clamped_start - start,
                clamped_end - clamped_start,
            ))
        })
        .collect()
}

/// Splits text and its entities into pieces which each fit inside `limit`.
///
/// At most `max_chunks` pieces are produced; anything beyond that is reported
/// as `dropped_units` rather than sent, so that a runaway upstream message
/// cannot turn a single notification into an unbounded flood.
fn split_once(text: &str, entities: &[Entity], limit: usize, max_chunks: usize) -> SplitOutcome {
    if utf16_len(text) <= limit {
        return SplitOutcome {
            chunks: vec![Chunk {
                text: text.to_string(),
                entities: entities.to_vec(),
            }],
            dropped_units: 0,
        };
    }

    let mut chunks: Vec<Chunk> = Vec::new();
    let mut remainder: &str = text;
    let mut absolute_offset: usize = 0;

    while !remainder.is_empty() && chunks.len() < max_chunks {
        let cut: usize = find_cut(remainder, limit);
        let piece: &str = &remainder[..cut];
        let piece_units: usize = utf16_len(piece);

        chunks.push(Chunk {
            text: piece.trim_end().to_string(),
            entities: clamp_entities(
                entities,
                absolute_offset,
                absolute_offset + utf16_len(piece.trim_end()),
            ),
        });

        absolute_offset += piece_units;
        remainder = &remainder[cut..];

        // Whitespace at a split point would otherwise open the next chunk with
        // a blank line. Skipping it also skips any entity covering only it.
        let trimmed: &str = remainder.trim_start();
        absolute_offset += utf16_len(remainder) - utf16_len(trimmed);
        remainder = trimmed;
    }

    // A chunk which was nothing but whitespace would be rejected by Telegram.
    chunks.retain(|chunk: &Chunk| !chunk.text.is_empty());

    SplitOutcome {
        chunks,
        dropped_units: utf16_len(remainder),
    }
}

/// Shortens a chunk to `units` UTF-16 code units, clamping its entities.
fn truncate_chunk(chunk: &Chunk, units: usize) -> Chunk {
    if utf16_len(&chunk.text) <= units {
        return chunk.clone();
    }

    let cut: usize = byte_index_for_units(&chunk.text, units);
    let text: String = chunk.text[..cut].trim_end().to_string();
    let entities: Vec<Entity> = clamp_entities(&chunk.entities, 0, utf16_len(&text));

    Chunk { text, entities }
}

/// Prefixes `text` onto a chunk, rebasing its entities past the prefix.
fn prefix_chunk(chunk: &Chunk, prefix: &str) -> Chunk {
    let shift: usize = utf16_len(prefix);

    Chunk {
        text: format!("{prefix}{}", chunk.text),
        entities: chunk
            .entities
            .iter()
            .map(|entity: &Entity| {
                Entity::new(entity.kind.clone(), entity.offset + shift, entity.length)
            })
            .collect(),
    }
}

/// Fits a message into Telegram's length limit, splitting it if necessary.
///
/// When the message splits, each piece is prefixed with a `(1/3)` indicator so
/// that a sequence of arriving messages reads as one message rather than as
/// several unrelated ones. When the chunk cap is reached, the final piece
/// carries a visible marker naming how much was dropped.
pub(crate) fn prepare(
    text: &str,
    entities: &[Entity],
    limit: usize,
    max_chunks: usize,
) -> SplitOutcome {
    let first_pass: SplitOutcome = split_once(text, entities, limit, max_chunks);

    if first_pass.chunks.len() <= 1 {
        return first_pass;
    }

    // Splitting is happening, so every chunk needs room for its indicator.
    let effective_limit: usize = limit.saturating_sub(INDICATOR_RESERVE).max(1);
    let outcome: SplitOutcome = split_once(text, entities, effective_limit, max_chunks);
    let total: usize = outcome.chunks.len();

    let mut chunks: Vec<Chunk> = outcome
        .chunks
        .iter()
        .enumerate()
        .map(|(index, chunk): (usize, &Chunk)| {
            prefix_chunk(chunk, &format!("({}/{total}) ", index + 1))
        })
        .collect();

    if outcome.dropped_units > 0
        && let Some(last) = chunks.last_mut()
    {
        let marker: String = format!(
            "\n… truncated, {} more characters dropped",
            outcome.dropped_units
        );
        let room: usize = limit.saturating_sub(utf16_len(&marker));
        let mut truncated: Chunk = truncate_chunk(last, room);
        truncated.text.push_str(&marker);
        *last = truncated;
    }

    SplitOutcome {
        chunks,
        dropped_units: outcome.dropped_units,
    }
}

#[cfg(test)]
mod tests {
    use super::{Chunk, SplitOutcome, find_cut, prepare, split_once};
    use crate::telegram::entity::{Entity, EntityKind};
    use crate::telegram::utf16::utf16_len;

    #[test]
    fn short_text_is_a_single_chunk() {
        let outcome: SplitOutcome = split_once("hello", &[], 4096, 5);

        let expected: usize = 1;
        let actual: usize = outcome.chunks.len();
        assert_eq!(expected, actual);

        let expected_text: String = String::from("hello");
        let actual_text: String = outcome.chunks[0].text.clone();
        assert_eq!(expected_text, actual_text);

        let expected_dropped: usize = 0;
        let actual_dropped: usize = outcome.dropped_units;
        assert_eq!(expected_dropped, actual_dropped);
    }

    #[test]
    fn entities_survive_an_unsplit_message() {
        let entities: Vec<Entity> = vec![Entity::new(EntityKind::Bold, 0, 5)];
        let outcome: SplitOutcome = split_once("hello", &entities, 4096, 5);

        let expected: Vec<Entity> = entities.clone();
        let actual: Vec<Entity> = outcome.chunks[0].entities.clone();
        assert_eq!(expected, actual);
    }

    #[test]
    fn long_text_splits_into_multiple_chunks() {
        let text: String = "a".repeat(10_000);
        let outcome: SplitOutcome = split_once(&text, &[], 4096, 5);

        let expected: usize = 3;
        let actual: usize = outcome.chunks.len();
        assert_eq!(expected, actual);

        let expected_dropped: usize = 0;
        let actual_dropped: usize = outcome.dropped_units;
        assert_eq!(expected_dropped, actual_dropped);
    }

    #[test]
    fn no_chunk_exceeds_the_limit() {
        let text: String = "lorem ipsum dolor sit amet ".repeat(500);
        let outcome: SplitOutcome = split_once(&text, &[], 100, 50);

        for chunk in &outcome.chunks {
            assert!(utf16_len(&chunk.text) <= 100);
        }
    }

    #[test]
    fn splitting_prefers_line_boundaries() {
        let text: String = format!("{}\n{}", "a".repeat(50), "b".repeat(50));
        let outcome: SplitOutcome = split_once(&text, &[], 60, 5);

        let expected_first: String = "a".repeat(50);
        let actual_first: String = outcome.chunks[0].text.clone();
        assert_eq!(expected_first, actual_first);

        let expected_second: String = "b".repeat(50);
        let actual_second: String = outcome.chunks[1].text.clone();
        assert_eq!(expected_second, actual_second);
    }

    #[test]
    fn splitting_never_severs_a_surrogate_pair() {
        // Each 🎨 is 2 UTF-16 code units; an odd limit forces a cut which a
        // naive implementation would place inside a pair.
        let text: String = "🎨".repeat(100);
        let outcome: SplitOutcome = split_once(&text, &[], 51, 20);

        for chunk in &outcome.chunks {
            // A severed pair would not round-trip through char boundaries.
            let expected: String = chunk.text.clone();
            let actual: String = chunk.text.chars().collect::<String>();
            assert_eq!(expected, actual);
            assert!(utf16_len(&chunk.text) <= 51);
        }

        let expected_total: usize = 100;
        let actual_total: usize = outcome
            .chunks
            .iter()
            .map(|chunk: &Chunk| chunk.text.chars().count())
            .sum();
        assert_eq!(expected_total, actual_total);
    }

    #[test]
    fn entities_straddling_a_boundary_are_clamped_into_both_chunks() {
        // 100 'a's, with a bold run covering offsets 40..60, split at 50.
        let text: String = "a".repeat(100);
        let entities: Vec<Entity> = vec![Entity::new(EntityKind::Bold, 40, 20)];
        let outcome: SplitOutcome = split_once(&text, &entities, 50, 5);

        let expected_first: Vec<Entity> = vec![Entity::new(EntityKind::Bold, 40, 10)];
        let actual_first: Vec<Entity> = outcome.chunks[0].entities.clone();
        assert_eq!(expected_first, actual_first);

        let expected_second: Vec<Entity> = vec![Entity::new(EntityKind::Bold, 0, 10)];
        let actual_second: Vec<Entity> = outcome.chunks[1].entities.clone();
        assert_eq!(expected_second, actual_second);
    }

    #[test]
    fn entities_entirely_outside_a_chunk_are_dropped() {
        let text: String = "a".repeat(100);
        let entities: Vec<Entity> = vec![Entity::new(EntityKind::Bold, 60, 10)];
        let outcome: SplitOutcome = split_once(&text, &entities, 50, 5);

        let expected_first: Vec<Entity> = Vec::new();
        let actual_first: Vec<Entity> = outcome.chunks[0].entities.clone();
        assert_eq!(expected_first, actual_first);

        let expected_second: Vec<Entity> = vec![Entity::new(EntityKind::Bold, 10, 10)];
        let actual_second: Vec<Entity> = outcome.chunks[1].entities.clone();
        assert_eq!(expected_second, actual_second);
    }

    #[test]
    fn hitting_the_chunk_cap_reports_dropped_units() {
        let text: String = "a".repeat(1000);
        let outcome: SplitOutcome = split_once(&text, &[], 100, 5);

        let expected_chunks: usize = 5;
        let actual_chunks: usize = outcome.chunks.len();
        assert_eq!(expected_chunks, actual_chunks);

        let expected_dropped: usize = 500;
        let actual_dropped: usize = outcome.dropped_units;
        assert_eq!(expected_dropped, actual_dropped);
    }

    #[test]
    fn find_cut_returns_whole_string_when_it_fits() {
        let expected: usize = 5;
        let actual: usize = find_cut("hello", 10);
        assert_eq!(expected, actual);
    }

    #[test]
    fn find_cut_always_makes_progress() {
        // A limit below the width of the first character must still advance.
        let actual: usize = find_cut("🎨🎨", 1);
        assert!(actual > 0);
    }

    #[test]
    fn prepare_adds_indicators_when_splitting() {
        let text: String = "a".repeat(300);
        let outcome: SplitOutcome = prepare(&text, &[], 100, 5);

        assert!(outcome.chunks[0].text.starts_with("(1/"));
        assert!(outcome.chunks[1].text.starts_with("(2/"));

        for chunk in &outcome.chunks {
            assert!(utf16_len(&chunk.text) <= 100);
        }
    }

    #[test]
    fn prepare_adds_no_indicator_to_a_single_chunk() {
        let outcome: SplitOutcome = prepare("hello", &[], 100, 5);

        let expected: String = String::from("hello");
        let actual: String = outcome.chunks[0].text.clone();
        assert_eq!(expected, actual);
    }

    #[test]
    fn prepare_shifts_entities_past_the_indicator() {
        let text: String = "a".repeat(300);
        let entities: Vec<Entity> = vec![Entity::new(EntityKind::Bold, 0, 10)];
        let outcome: SplitOutcome = prepare(&text, &entities, 100, 5);

        // "(1/4) " is 6 UTF-16 code units.
        let expected: Vec<Entity> = vec![Entity::new(EntityKind::Bold, 6, 10)];
        let actual: Vec<Entity> = outcome.chunks[0].entities.clone();
        assert_eq!(expected, actual);
    }

    #[test]
    fn prepare_marks_truncation_in_the_final_chunk() {
        let text: String = "a".repeat(10_000);
        let outcome: SplitOutcome = prepare(&text, &[], 100, 3);

        assert!(outcome.dropped_units > 0);

        let last: String = outcome
            .chunks
            .last()
            .expect("there should be chunks")
            .text
            .clone();
        assert!(last.contains("truncated"));
        assert!(last.contains("more characters dropped"));

        for chunk in &outcome.chunks {
            assert!(utf16_len(&chunk.text) <= 100);
        }
    }

    #[test]
    fn prepare_respects_the_caption_limit() {
        let text: String = "a".repeat(5000);
        let outcome: SplitOutcome = prepare(&text, &[], 1024, 5);

        for chunk in &outcome.chunks {
            assert!(utf16_len(&chunk.text) <= 1024);
        }
    }
}