auditmysite 1.2.0

Resource-efficient WCAG 2.1 Accessibility Checker in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! SERP pass — aggregates existing SEO signals into a search-result-page readiness check.
//!
//! No live search calls, no ML. Pure heuristics over already-collected data.

use serde::{Deserialize, Serialize};

use crate::seo::SeoAnalysis;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum SerpSignalStatus {
    Ok,
    Warning,
    Fail,
}

impl SerpSignalStatus {
    pub fn label(&self, en: bool) -> &'static str {
        match self {
            SerpSignalStatus::Ok => "OK",
            SerpSignalStatus::Warning => {
                if en {
                    "Warning"
                } else {
                    "Warnung"
                }
            }
            SerpSignalStatus::Fail => {
                if en {
                    "Error"
                } else {
                    "Fehler"
                }
            }
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SerpSignal {
    pub category: String,
    pub label: String,
    pub status: SerpSignalStatus,
    pub detail: String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SerpAnalysis {
    pub signals: Vec<SerpSignal>,
    pub score: u32,
    pub pass_count: u32,
    pub warning_count: u32,
    pub fail_count: u32,
    /// Rich-result-related schema types detected; this is not an eligibility claim.
    pub rich_result_types: Vec<String>,
}

pub fn build_serp_analysis(seo: &SeoAnalysis, url: &str, locale: &str) -> SerpAnalysis {
    let en = locale == "en";
    let mut signals = Vec::new();

    // --- robots: noindex / nosnippet ---
    if let Some(robots_meta) = &seo.technical.robots_meta {
        let lower = robots_meta.to_ascii_lowercase();
        if lower.contains("noindex") {
            signals.push(sig(
                if en { "Indexing" } else { "Indexierung" },
                "robots noindex",
                SerpSignalStatus::Fail,
                if en {
                    format!(
                        "Page is marked as noindex and is not indexed ({})",
                        robots_meta
                    )
                } else {
                    format!(
                        "Seite ist als noindex markiert und wird nicht indexiert ({})",
                        robots_meta
                    )
                },
            ));
        }
        if lower.contains("nosnippet") {
            signals.push(sig(
                if en { "Indexing" } else { "Indexierung" },
                "robots nosnippet",
                SerpSignalStatus::Warning,
                if en {
                    "nosnippet prevents the meta description from appearing in the SERP entry."
                } else {
                    "nosnippet verhindert die Meta-Description im SERP-Eintrag."
                },
            ));
        }
    }

    // --- Title ---
    match &seo.meta.title {
        None => signals.push(sig(
            if en { "Title" } else { "Titel" },
            if en {
                "Title present"
            } else {
                "Titel vorhanden"
            },
            SerpSignalStatus::Fail,
            if en {
                "No <title> tag found — required field for Google listings."
            } else {
                "Kein <title>-Tag gefunden — Pflichtfeld für Google-Listeneinträge."
            },
        )),
        Some(title) => {
            let len = title.len();
            let status = if !(30..=60).contains(&len) {
                SerpSignalStatus::Warning
            } else {
                SerpSignalStatus::Ok
            };
            signals.push(sig(
                if en { "Title" } else { "Titel" },
                if en { "Title length" } else { "Titellänge" },
                status,
                if en {
                    format!("{} characters (recommended 30–60)", len)
                } else {
                    format!("{} Zeichen (empfohlen 30–60)", len)
                },
            ));
            if len > 55 {
                signals.push(sig(
                    if en { "Title" } else { "Titel" },
                    if en {
                        "Truncation risk"
                    } else {
                        "Abschneidungsrisiko"
                    },
                    SerpSignalStatus::Warning,
                    if en {
                        format!(
                            "{} characters — title gets truncated in narrow SERPs (about 55 characters is safe).",
                            len
                        )
                    } else {
                        format!(
                            "{} Zeichen — Titel wird in schmalen SERPs abgeschnitten (ca. 55 Zeichen sicher).",
                            len
                        )
                    },
                ));
            }
        }
    }

    // --- Meta Description ---
    match &seo.meta.description {
        None => signals.push(sig(
            "Description",
            if en {
                "Description present"
            } else {
                "Description vorhanden"
            },
            SerpSignalStatus::Fail,
            if en {
                "No meta description — Google shows uncontrolled page text."
            } else {
                "Keine Meta-Description — Google zeigt unkontrollierten Seitentext."
            },
        )),
        Some(desc) => {
            let len = desc.len();
            let status = if !(120..=160).contains(&len) {
                SerpSignalStatus::Warning
            } else {
                SerpSignalStatus::Ok
            };
            signals.push(sig(
                "Description",
                if en {
                    "Description length"
                } else {
                    "Description-Länge"
                },
                status,
                if en {
                    format!("{} characters (recommended 120–160)", len)
                } else {
                    format!("{} Zeichen (empfohlen 120–160)", len)
                },
            ));
        }
    }

    // --- Canonical ---
    signals.push(sig(
        "Canonical",
        if en { "Canonical tag" } else { "Canonical-Tag" },
        if seo.technical.has_canonical {
            SerpSignalStatus::Ok
        } else {
            SerpSignalStatus::Warning
        },
        if seo.technical.has_canonical {
            seo.technical.canonical_url.clone().unwrap_or_default()
        } else if en {
            "No canonical tag — Google picks the canonical URL itself.".to_string()
        } else {
            "Kein Canonical-Tag — Google wählt selbst die kanonische URL.".to_string()
        },
    ));

    if let Some(canon) = &seo.technical.canonical_url {
        let canon_clean = canon.trim_end_matches('/');
        let url_clean = url.trim_end_matches('/');
        if !canon_clean.is_empty() && !canon_clean.eq_ignore_ascii_case(url_clean) {
            signals.push(sig(
                "Canonical",
                if en {
                    "Canonical self-reference"
                } else {
                    "Canonical-Selbstreferenz"
                },
                SerpSignalStatus::Warning,
                if en {
                    format!("Canonical points to a different URL: {}", canon)
                } else {
                    format!("Canonical zeigt auf eine andere URL: {}", canon)
                },
            ));
        }
    }

    // --- Favicon ---
    signals.push(sig(
        if en { "Appearance" } else { "Erscheinungsbild" },
        "Favicon",
        if seo.technical.has_favicon {
            SerpSignalStatus::Ok
        } else {
            SerpSignalStatus::Warning
        },
        if seo.technical.has_favicon {
            if en {
                "Favicon present (appears in mobile SERPs and browser tabs)."
            } else {
                "Favicon vorhanden (erscheint in mobilen SERPs und Browser-Tabs)."
            }
        } else if en {
            "No favicon found — Google shows a generic icon in mobile SERPs."
        } else {
            "Kein Favicon gefunden — Google zeigt in mobilen SERPs ein generisches Icon."
        },
    ));

    // --- Hreflang x-default ---
    if seo.technical.has_hreflang {
        let has_x_default = seo.technical.hreflang.iter().any(|h| h.lang == "x-default");
        signals.push(sig(
            if en {
                "Internationalisation"
            } else {
                "Internationalisierung"
            },
            "hreflang x-default",
            if has_x_default {
                SerpSignalStatus::Ok
            } else {
                SerpSignalStatus::Warning
            },
            if has_x_default {
                if en {
                    format!(
                        "{} hreflang tags incl. x-default",
                        seo.technical.hreflang.len()
                    )
                } else {
                    format!(
                        "{} hreflang-Tags inkl. x-default",
                        seo.technical.hreflang.len()
                    )
                }
            } else if en {
                format!(
                    "{} hreflang tags, no x-default — Google recommendation for multilingual pages.",
                    seo.technical.hreflang.len()
                )
            } else {
                format!(
                    "{} hreflang-Tags, kein x-default — Google-Empfehlung für mehrsprachige Seiten.",
                    seo.technical.hreflang.len()
                )
            },
        ));
    }

    // --- Breadcrumb Schema ---
    {
        use crate::seo::SchemaType;
        let has_breadcrumb = seo
            .structured_data
            .types
            .contains(&SchemaType::BreadcrumbList);
        signals.push(sig(
            "Rich Result",
            if en {
                "Breadcrumb schema"
            } else {
                "Breadcrumb-Schema"
            },
            if has_breadcrumb {
                SerpSignalStatus::Ok
            } else {
                SerpSignalStatus::Warning
            },
            if has_breadcrumb {
                if en {
                    "BreadcrumbList detected — Google shows the path in the listing."
                } else {
                    "BreadcrumbList erkannt — Google zeigt Pfadangabe im Listeneintrag."
                }
            } else if en {
                "No BreadcrumbList schema — path display in the SERP entry not possible."
            } else {
                "Kein BreadcrumbList-Schema — Pfadangabe im SERP-Eintrag nicht möglich."
            },
        ));
    }

    // --- Score ---
    let pass_count = signals
        .iter()
        .filter(|s| s.status == SerpSignalStatus::Ok)
        .count() as u32;
    let warning_count = signals
        .iter()
        .filter(|s| s.status == SerpSignalStatus::Warning)
        .count() as u32;
    let fail_count = signals
        .iter()
        .filter(|s| s.status == SerpSignalStatus::Fail)
        .count() as u32;
    let total = signals.len() as u32;
    let score = (pass_count * 100 + warning_count * 50)
        .checked_div(total)
        .unwrap_or(100)
        .min(100);

    SerpAnalysis {
        signals,
        score,
        pass_count,
        warning_count,
        fail_count,
        rich_result_types: seo.structured_data.rich_snippets_potential.clone(),
    }
}

fn sig(
    category: impl Into<String>,
    label: impl Into<String>,
    status: SerpSignalStatus,
    detail: impl Into<String>,
) -> SerpSignal {
    SerpSignal {
        category: category.into(),
        label: label.into(),
        status,
        detail: detail.into(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::seo::technical::HreflangTag;
    use crate::seo::SchemaType;

    fn has_german_chars(s: &str) -> bool {
        s.chars().any(|c| "äöüÄÖÜß".contains(c))
    }

    #[test]
    fn test_serp_signals_have_no_german_chars_in_en() {
        // Populate technical + meta so every conditional branch that produces a
        // visible label/detail string is exercised.
        let mut seo = SeoAnalysis::default();
        seo.meta.title =
            Some("A title long enough to trip the truncation risk branch here".to_string());
        seo.meta.description = Some("short".to_string());
        seo.technical.robots_meta = Some("noindex, nosnippet".to_string());
        seo.technical.has_canonical = true;
        seo.technical.canonical_url = Some("https://example.com/other".to_string());
        seo.technical.has_favicon = false;
        seo.technical.has_hreflang = true;
        seo.technical.hreflang = vec![HreflangTag {
            lang: "de".to_string(),
            url: "https://example.com/de".to_string(),
        }];
        seo.structured_data.types = vec![SchemaType::Article];

        let analysis = build_serp_analysis(&seo, "https://example.com/", "en");
        for signal in &analysis.signals {
            assert!(
                !has_german_chars(&signal.category),
                "German chars in category: {}",
                signal.category
            );
            assert!(
                !has_german_chars(&signal.label),
                "German chars in label: {}",
                signal.label
            );
            assert!(
                !has_german_chars(&signal.detail),
                "German chars in detail: {}",
                signal.detail
            );
        }
    }
}