f5ed284f8c 2025-06-21 1: use crate::Cursor;
f5ed284f8c 2025-06-21 2:
f5ed284f8c 2025-06-21 3: use lazy_static::lazy_static;
f5ed284f8c 2025-06-21 4: use regex::Regex;
0f47e23e21 2026-01-12 5: use scraper::Html;
0f47e23e21 2026-01-12 6: use stacked_errors::{
0f47e23e21 2026-01-12 7: bail,
0f47e23e21 2026-01-12 8: Result,
0f47e23e21 2026-01-12 9: };
f5ed284f8c 2025-06-21 10:
f5ed284f8c 2025-06-21 11: lazy_static! {
f5ed284f8c 2025-06-21 12: 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();
f5ed284f8c 2025-06-21 13: }
f5ed284f8c 2025-06-21 14:
f5ed284f8c 2025-06-21 15: /// `Attachment` object to store number attachment data and corresponding file name
f5ed284f8c 2025-06-21 16: #[derive(Debug)]
f5ed284f8c 2025-06-21 17: pub struct Attachment {
f5ed284f8c 2025-06-21 18: pub data: Cursor<Vec<u8>>,
f5ed284f8c 2025-06-21 19: pub name: String,
0f47e23e21 2026-01-12 20: }
0f47e23e21 2026-01-12 21:
0f47e23e21 2026-01-12 22: /// Pass any text here to be validated as HTML, breaks on validation errors
0f47e23e21 2026-01-12 23: pub fn validate (text: &str) -> Result<&str> {
6ce625a569 2026-01-12 24: // Technically full validation is not needed nor required here, all text after validation
6ce625a569 2026-01-12 25: // is used in Telegram messages as RAW text enclosed in `pre`/`code` tags, so the only reason
6ce625a569 2026-01-12 26: // for this check is to make sure there's no dangling closing tags in the text that might
6ce625a569 2026-01-12 27: // break Telegram message formatting
0f47e23e21 2026-01-12 28: let fragment = Html::parse_fragment(text);
0f47e23e21 2026-01-12 29: if !fragment.errors.is_empty() {
0f47e23e21 2026-01-12 30: bail!(fragment.errors.join("\n"));
0f47e23e21 2026-01-12 31: }
c996f5c871 2026-01-12 32: Ok(text)
f5ed284f8c 2025-06-21 33: }