f5ed284f8c 2025-06-21 1: use crate::Cursor;
f5ed284f8c 2025-06-21 2:
997fb1cff4 2026-08-01 3: use std::{
997fb1cff4 2026-08-01 4: borrow::Cow,
997fb1cff4 2026-08-01 5: sync::LazyLock,
997fb1cff4 2026-08-01 6: };
85fa6bddaa 2026-01-18 7:
85fa6bddaa 2026-01-18 8: use html_escape::encode_text;
bf99298edf 2026-07-31 9: use regex::{
bf99298edf 2026-07-31 10: Regex,
bf99298edf 2026-07-31 11: RegexBuilder,
bf99298edf 2026-07-31 12: };
0f47e23e21 2026-01-12 13: use stacked_errors::{
0f47e23e21 2026-01-12 14: bail,
0f47e23e21 2026-01-12 15: Result,
0f47e23e21 2026-01-12 16: };
0f47e23e21 2026-01-12 17:
997fb1cff4 2026-08-01 18: pub static RE_DOMAIN: LazyLock<Regex> = LazyLock::new(|| {
997fb1cff4 2026-08-01 19: Regex::new(r"^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$").expect("Invalid domain regex")
997fb1cff4 2026-08-01 20: });
997fb1cff4 2026-08-01 21: pub static RE_CLOSING: LazyLock<Regex> = LazyLock::new(|| {
997fb1cff4 2026-08-01 22: RegexBuilder::new(r"</[ \t]*(pre|code)[ \t]*>")
997fb1cff4 2026-08-01 23: .case_insensitive(true).build().expect("Invalid closing tag regex")
997fb1cff4 2026-08-01 24: });
0f47e23e21 2026-01-12 25:
3c858ed7c4 2026-07-31 26: /// Stores binary attachment data and metadata for Telegram messages.
3c858ed7c4 2026-07-31 27: /// The data is wrapped in a `Cursor<Vec<u8>>` for efficient streaming,
3c858ed7c4 2026-07-31 28: /// while `name` holds the filename or display name of the attachment.
f5ed284f8c 2025-06-21 29: #[derive(Debug)]
f5ed284f8c 2025-06-21 30: pub struct Attachment {
f5ed284f8c 2025-06-21 31: pub data: Cursor<Vec<u8>>,
f5ed284f8c 2025-06-21 32: pub name: String,
0f47e23e21 2026-01-12 33: }
0f47e23e21 2026-01-12 34:
dafeec0481 2026-01-18 35: /// Pass any text here to be validated as not breaking from Telegram preformatted blocks
85fa6bddaa 2026-01-18 36: /// escape all HTML chars afterwards
3c858ed7c4 2026-07-31 37: pub fn validate <'a>(text: &'a str) -> Result<Cow<'a, str>> {
dafeec0481 2026-01-18 38: if RE_CLOSING.is_match(text) {
dafeec0481 2026-01-18 39: bail!("Telegram closing tag found.");
dafeec0481 2026-01-18 40: } else {
85fa6bddaa 2026-01-18 41: Ok(encode_text(text))
dafeec0481 2026-01-18 42: }
f5ed284f8c 2025-06-21 43: }