use std::borrow::Cow;
use std::sync::LazyLock;
use regex::{Captures, Regex};
struct Kural {
desen: Regex,
yer_tutucu: &'static str,
sonrasi_olmamali: Option<fn(char) -> bool>,
}
static KURALLAR: LazyLock<Vec<Kural>> = LazyLock::new(|| {
vec![
yeni_kural(r#"https?://[^\s)"'<>`]+"#, "<URL>"),
yeni_kural(r"\(View: .*?\.php\)", "(View: <YOL>)"),
yeni_kural(
r#"(^|[\s(\['"=,])[A-Za-z]:[\\/][^\s()'"<>|?*,;:]*"#,
"${1}<YOL>",
),
Kural {
sonrasi_olmamali: Some(yol_suruyor),
..yeni_kural(
r#"(^|[\s(\['"=,])/(?:[^\s/()'"<>|?*,;:]+/)+[^\s/()'"<>|?*,;:]*\.[A-Za-z0-9]+"#,
"${1}<YOL>",
)
},
yeni_kural(r"[\w.+-]+@[\w-]+(?:\.[\w-]+)*\.\p{L}[\w-]*", "<EPOSTA>"),
yeni_kural(
r"(?-u:\b)[0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}(?-u:\b)",
"<UUID>",
),
yeni_kural(
r"(?-u:\b)[0-9]{4}-[0-9]{2}-[0-9]{2}[ T][0-9]{2}:[0-9]{2}:[0-9]{2}(?-u:\b)",
"<ZAMAN>",
),
yeni_kural(r"(?-u:\b)(?:[0-9]{1,3}\.){3}[0-9]{1,3}(?-u:\b)", "<IP>"),
yeni_kural(r"(?-u:\b)[0-9]+(?:\.[0-9]+)*(?-u:\b)", "<SAYI>"),
]
});
fn yeni_kural(desen: &str, yer_tutucu: &'static str) -> Kural {
Kural {
desen: Regex::new(desen).expect("geçersiz düzenli ifade"),
yer_tutucu,
sonrasi_olmamali: None,
}
}
fn yol_suruyor(harf: char) -> bool {
matches!(harf, '/' | '\\' | '_') || harf.is_alphanumeric()
}
pub fn maskele(mesaj: &str) -> Cow<'_, str> {
let mut sonuc = Cow::Borrowed(mesaj);
for kural in KURALLAR.iter() {
let degisen = match kural.sonrasi_olmamali {
None => kural.desen.replace_all(&sonuc, kural.yer_tutucu),
Some(olmamali) => {
let metin: &str = &sonuc;
kural.desen.replace_all(metin, |gruplar: &Captures<'_>| {
let eslesme = gruplar.get(0).expect("0. grup eşleşmenin tamamıdır");
let mut yeni = String::new();
if metin[eslesme.end()..].chars().next().is_some_and(olmamali) {
yeni.push_str(eslesme.as_str());
} else {
gruplar.expand(kural.yer_tutucu, &mut yeni);
}
yeni
})
}
};
if let Cow::Owned(degisen) = degisen {
sonuc = Cow::Owned(degisen);
}
}
sonuc
}
#[cfg(test)]
mod testler {
use super::*;
#[test]
fn sayilar_maskelenir() {
assert_eq!(
maskele("Kullanıcı 381 giriş yaptı"),
"Kullanıcı <SAYI> giriş yaptı"
);
assert_eq!(
maskele("Görev tamamlandı (2.41 sn)"),
"Görev tamamlandı (<SAYI> sn)"
);
assert_eq!(
maskele("Uygulama sürümü 2.4.0 yayına alındı"),
"Uygulama sürümü <SAYI> yayına alındı"
);
}
#[test]
fn kelimenin_icindeki_rakam_maskelenmez() {
assert_eq!(
maskele("oauth2 jetonu yenilendi"),
"oauth2 jetonu yenilendi"
);
assert_eq!(maskele("Sipariş5 hazır"), "Sipariş<SAYI> hazır");
assert_eq!(maskele("4üncü deneme"), "<SAYI>üncü deneme");
assert_eq!(maskele("Ödeme3 ve 3d"), "Ödeme3 ve 3d");
assert_eq!(maskele("Sayı ٣٤ geldi"), "Sayı ٣٤ geldi");
}
#[test]
fn ip_sayilara_bolunmez() {
assert_eq!(
maskele("Connection refused [tcp://10.0.3.12:6379]"),
"Connection refused [tcp://<IP>:<SAYI>]"
);
}
#[test]
fn eposta_url_uuid_ve_zaman_maskelenir() {
assert_eq!(
maskele("Duplicate entry 'ayse@ornek.test'"),
"Duplicate entry '<EPOSTA>'"
);
assert_eq!(
maskele(
"(see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://api.odeme.test/v1/tahsilat"
),
"(see <URL>) for <URL>"
);
assert_eq!(
maskele(
r#"The option "dsn" with value "https://anahtar@o1.ingest.test/2" is invalid."#
),
r#"The option "dsn" with value "<URL>" is invalid."#
);
assert_eq!(
maskele("Webhook 'https://ornek.test/kanca?x=1' yanıt vermedi"),
"Webhook '<URL>' yanıt vermedi"
);
assert_eq!(maskele("<https://ornek.test/a>"), "<<URL>>");
assert_eq!(
maskele(
"Client error: `POST https://api.odeme.test/v1/tahsilat` resulted in a `404 Not Found` response"
),
"Client error: `POST <URL>` resulted in a `<SAYI> Not Found` response"
);
assert_eq!(
maskele("laravel/framework@11.9.2 yüklendi"),
"laravel/framework@<SAYI> yüklendi"
);
assert_eq!(maskele("php@8.3"), "php@<SAYI>");
assert_eq!(
maskele("ayşe@örnek.com.tr ve ali.veli+test@posta2.ornek.test"),
"<EPOSTA> ve <EPOSTA>"
);
assert_eq!(
maskele("Sipariş 3f2b8c1e-9d4a-4e7b-8a6f-1c2d3e4f5a6b oluşturuldu"),
"Sipariş <UUID> oluşturuldu"
);
assert_eq!(
maskele("values (Ayşe, 2026-10-24 09:14:20)"),
"values (Ayşe, <ZAMAN>)"
);
}
#[test]
fn dosya_yollari_maskelenir() {
assert_eq!(
maskele(
r"Route [kampanya.liste] not defined. (View: C:\Users\İrem\Herd\magaza\resources\views\kampanyalar.blade.php)"
),
"Route [kampanya.liste] not defined. (View: <YOL>)"
);
assert_eq!(
maskele(r"include(C:\Users\İrem\Herd\magaza\public\ust.php): Failed to open stream"),
"include(<YOL>): Failed to open stream"
);
assert_eq!(
maskele(
r"Too few arguments to function abort_if(), 1 passed in C:\Users\İrem\Herd\magaza\app\Http\Middleware\YoneticiMi.php on line 18 and at least 2 expected"
),
"Too few arguments to function abort_if(), <SAYI> passed in <YOL> on line <SAYI> and at least <SAYI> expected"
);
assert_eq!(
maskele("Dosya 'C:/Users/İrem/Herd/magaza/.env' okunamadı"),
"Dosya '<YOL>' okunamadı"
);
assert_eq!(
maskele(
r"Undefined variable $urun (View: C:\Users\Ali Veli\Herd\magaza\resources\views\urun.blade.php)"
),
"Undefined variable $urun (View: <YOL>)"
);
assert_eq!(
maskele("x (View: /home/ali veli/magaza/a.blade.php) (View: /srv/b.blade.php)"),
"x (View: <YOL>) (View: <YOL>)"
);
assert_eq!(
maskele(r"C:\a\b.php (View: C:\a\c.blade.php) (View: C:\a\d.blade.php)"),
"<YOL> (View: <YOL>) (View: <YOL>)"
);
assert_eq!(
maskele(r"Hata C:\magaza\app\X.php:12 satırında"),
"Hata <YOL>:<SAYI> satırında"
);
assert_eq!(
maskele(
"Undefined variable $urun (View: /var/www/magaza/resources/views/vitrin.blade.php)"
),
"Undefined variable $urun (View: <YOL>)"
);
assert_eq!(
maskele(
r#"The stream or file "/var/www/magaza/storage/logs/laravel.log" could not be opened"#
),
r#"The stream or file "<YOL>" could not be opened"#
);
assert_eq!(
maskele("Dosya /var/www/magaza/app/X.php:12 içinde"),
"Dosya <YOL>:<SAYI> içinde"
);
assert_eq!(
maskele("GET /api/urunler/15 yavaş"),
"GET /api/urunler/<SAYI> yavaş"
);
assert_eq!(
maskele("Klasör /var/www/magaza/storage yazılamıyor"),
"Klasör /var/www/magaza/storage yazılamıyor"
);
assert_eq!(
maskele("include(ust.php): Failed"),
"include(ust.php): Failed"
);
assert_eq!(maskele("Adım 1/2 tamam"), "Adım <SAYI>/<SAYI> tamam");
assert_eq!(
maskele("GET /api/v1.2/urunler/15 yavaş"),
"GET /api/v1.<SAYI>/urunler/<SAYI> yavaş"
);
assert_eq!(maskele("Klasör /srv/a.b/c boş"), "Klasör /srv/a.b/c boş");
assert_eq!(maskele("/srv/a.php_eski"), "/srv/a.php_eski");
assert_eq!(maskele("/srv/a.phpçok"), "/srv/a.phpçok");
assert_eq!(
maskele("/var/www/a.php /var/www/b.php,/srv/c.log"),
"<YOL> <YOL>,<YOL>"
);
assert_eq!(
maskele("Dosya /var/www/magaza/app/X.php. Sonra /srv/a.log-eski"),
"Dosya <YOL>. Sonra <YOL>-eski"
);
assert_eq!(maskele(r"regex:\d+ hatalı"), r"regex:\d+ hatalı");
assert_eq!(
maskele("Bkz. https://ornek.test/belge/kilavuz.pdf"),
"Bkz. <URL>"
);
}
#[test]
fn degisken_yoksa_kopyalanmaz() {
assert!(matches!(maskele("Önbellek temizlendi"), Cow::Borrowed(_)));
assert!(matches!(
maskele("Kullanıcı 381 giriş yaptı"),
Cow::Owned(_)
));
}
}