f5ed284f8c 2025-06-21 1: use crate::Cursor;
f5ed284f8c 2025-06-21 2:
85fa6bddaa 2026-01-18 3: use std::borrow::Cow;
85fa6bddaa 2026-01-18 4:
85fa6bddaa 2026-01-18 5: use html_escape::encode_text;
f5ed284f8c 2025-06-21 6: use lazy_static::lazy_static;
f5ed284f8c 2025-06-21 7: use regex::Regex;
0f47e23e21 2026-01-12 8: use stacked_errors::{
0f47e23e21 2026-01-12 9: bail,
0f47e23e21 2026-01-12 10: Result,
0f47e23e21 2026-01-12 11: };
f5ed284f8c 2025-06-21 12:
f5ed284f8c 2025-06-21 13: lazy_static! {
f5ed284f8c 2025-06-21 14: pub static ref RE_DOMAIN: Regex = Regex::new(r"^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$").unwrap();
dafeec0481 2026-01-18 15: pub static ref RE_CLOSING: Regex = Regex::new(r"</[ \t]*(pre|code)[ \t]*>").unwrap();
f5ed284f8c 2025-06-21 16: }
f5ed284f8c 2025-06-21 17:
f5ed284f8c 2025-06-21 18: /// `Attachment` object to store number attachment data and corresponding file name
f5ed284f8c 2025-06-21 19: #[derive(Debug)]
f5ed284f8c 2025-06-21 20: pub struct Attachment {
f5ed284f8c 2025-06-21 21: pub data: Cursor<Vec<u8>>,
f5ed284f8c 2025-06-21 22: pub name: String,
0f47e23e21 2026-01-12 23: }
0f47e23e21 2026-01-12 24:
dafeec0481 2026-01-18 25: /// Pass any text here to be validated as not breaking from Telegram preformatted blocks
85fa6bddaa 2026-01-18 26: /// escape all HTML chars afterwards
85fa6bddaa 2026-01-18 27: pub fn validate (text: &str) -> Result<Cow<'_, str>> {
dafeec0481 2026-01-18 28: if RE_CLOSING.is_match(text) {
dafeec0481 2026-01-18 29: bail!("Telegram closing tag found.");
dafeec0481 2026-01-18 30: } else {
85fa6bddaa 2026-01-18 31: Ok(encode_text(text))
dafeec0481 2026-01-18 32: }
f5ed284f8c 2025-06-21 33: }