use std::borrow::Cow;
use std::sync::LazyLock;
use regex::Regex;
struct Kural {
desen: Regex,
yer_tutucu: &'static str,
}
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>",
),
yeni_kural(
r#"(^|[\s(\['"=,])/(?:[^\s/()'"<>|?*,;:]+/)+[^\s/()'"<>|?*,;:]*\.[A-Za-z0-9]+\b"#,
"${1}<YOL>",
),
yeni_kural(r"[\w.+-]+@[\w-]+(?:\.[\w-]+)+", "<EPOSTA>"),
yeni_kural(
r"\b[0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}\b",
"<UUID>",
),
yeni_kural(r"\b\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}\b", "<ZAMAN>"),
yeni_kural(r"\b(?:\d{1,3}\.){3}\d{1,3}\b", "<IP>"),
yeni_kural(r"\b\d+(?:\.\d+)*\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,
}
}
pub fn maskele(mesaj: &str) -> Cow<'_, str> {
let mut sonuc = Cow::Borrowed(mesaj);
for kural in KURALLAR.iter() {
if let Cow::Owned(degisen) = kural.desen.replace_all(&sonuc, kural.yer_tutucu) {
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"
);
}
#[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("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(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(_)
));
}
}