Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ jobs:
- name: Check formatting
run: cargo fmt --all -- --check

- name: Check every source file is a declared module
run: bash scripts/check_modules_reachable.sh

- name: Clippy (lint)
run: cargo clippy --all-targets --all-features -- -D warnings

Expand Down
10 changes: 10 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,13 @@ Follow existing conventions. Write tests for new features.
## Reporting Issues

Include: steps to reproduce, expected behavior, environment info.

## Rust formatting

CI runs `cargo fmt --all -- --check` for `contract/`. Enable the repo hook once so it is checked before every commit:

```bash
git config core.hooksPath .githooks
```

and run `cd contract && cargo fmt --all` if it complains.
38 changes: 38 additions & 0 deletions contract/scripts/check_modules_reachable.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env bash
# Fail if a .rs file under src/ is not reachable from lib.rs / main.rs.
# A file nobody declares as a module is never compiled, so its tests never run.
set -euo pipefail
cd "$(dirname "$0")/.."

fail=0
while IFS= read -r f; do
rel="${f#src/}"
case "$rel" in lib.rs|main.rs) continue ;; esac
dir="$(dirname "$rel")"
base="$(basename "$rel" .rs)"
if [ "$base" = "mod" ]; then
# src/a/b/mod.rs is declared as `mod b;` in src/a/mod.rs (or lib.rs for src/b/mod.rs)
name="$(basename "$dir")"
parent="$(dirname "$dir")"
else
name="$base"
parent="$dir"
fi
if [ "$parent" = "." ]; then
candidates="src/lib.rs src/main.rs"
else
candidates="src/$parent/mod.rs src/$parent.rs"
fi
found=0
for c in $candidates; do
if [ -f "$c" ] && grep -Eq "^[[:space:]]*(pub(\([a-z]+\))?[[:space:]]+)?mod[[:space:]]+${name}[[:space:]]*;" "$c"; then
found=1
fi
done
if [ "$found" -eq 0 ]; then
echo "ERROR: $f is not declared as a module (it is never compiled)"
fail=1
fi
done < <(find src -name '*.rs' | sort)

exit "$fail"
8 changes: 5 additions & 3 deletions contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//!
//! | Module | Why it is public |
//! |---|---|
//! | `app` | The Axum `Router` constructor. This is the entry point for embedding the service. |
//! | *(root)* `app` | Not a module: `pub fn app(state)` at the crate root builds the Axum `Router` (it delegates to `routes::app`). This is the entry point for embedding the service. |
//! | `cache` | `CacheBackend` / `InMemoryCache` / `RedisCache` are named in `AppState`, which integration tests construct. |
//! | `config` | `AppConfig::from_env` and `ConfigError` are how a deployment is configured. |
//! | `hash_validator` | `HashValidator` and `ValidationError` are the contract for callers accepting document hashes. |
Expand Down Expand Up @@ -42,7 +42,6 @@
//! Until then the rule is: **do not add new `pub` items to these modules**, and
//! prefer `pub(crate)` for anything new that does not have to be reachable.

pub mod app;
pub mod cache;
pub mod config;
pub mod error;
Expand All @@ -59,6 +58,9 @@ pub mod types;
pub mod webhook;
pub mod smalda_helpers;

#[cfg(test)]
mod tests;

use axum::{
body::Body,
extract::{Path, State},
Expand Down Expand Up @@ -1419,7 +1421,7 @@ pub fn find_duplicates(documents: &[&str], threshold: f64) -> Vec<(usize, usize,
}

#[cfg(test)]
mod tests {
mod unit_tests {
use super::*;

#[test]
Expand Down
5 changes: 4 additions & 1 deletion contract/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
pub mod integration;
mod cache_key_namespace;
mod error_response_no_leak;
mod integration;
mod stellar_retry_classification;
1 change: 0 additions & 1 deletion contract/tests/public_api_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ fn declared_modules(source: &str) -> Vec<&str> {
/// Grouped the way the doc comment groups them, so the two lists cannot drift
/// apart silently.
const DELIBERATELY_PUBLIC: &[&str] = &[
"app",
"cache",
"config",
"hash_validator",
Expand Down
8 changes: 5 additions & 3 deletions frontend/app/[locale]/(protected)/documents/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import React, { useCallback, useEffect, useRef, useState } from "react";
import { useParams } from "next/navigation";
import { useTranslations } from "next-intl";
import { useLocale, useTranslations } from "next-intl";
import { Link, useRouter } from "@/i18n/navigation";
import { useDateFormatting } from "@/i18n/formatting";
import { ApiError, request } from "@/lib/api-client";
import { riskUrl } from "@/lib/risk-url";
import { apiUrl } from "@/lib/api-config";
import {
Dialog,
Expand Down Expand Up @@ -262,6 +263,7 @@ function ShareModal({ docId }: { docId: string }) {

export default function DocumentDetailPage() {
const t = useTranslations("documentDetail");
const locale = useLocale();
const { formatDateTime } = useDateFormatting();
const params = useParams<{ id: string }>();
const router = useRouter();
Expand Down Expand Up @@ -315,7 +317,7 @@ export default function DocumentDetailPage() {
const [docData, riskData] = await Promise.all([
request<DocumentDetail>(apiUrl(path)),
// Risk data is optional; a missing endpoint must not fail the page.
request<RiskData>(apiUrl(`${path}/risk`)).catch(() => null),
request<RiskData>(riskUrl(path, locale)).catch(() => null),
]);
setDoc(docData);
setLatitudeInput(docData.latitude == null ? "" : String(docData.latitude));
Expand All @@ -330,7 +332,7 @@ export default function DocumentDetailPage() {
} finally {
setLoading(false);
}
}, [docId, t]);
}, [docId, t, locale]);

useEffect(() => {
fetchData();
Expand Down
9 changes: 9 additions & 0 deletions frontend/lib/__tests__/risk-url.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { riskUrl } from "../risk-url";

describe("riskUrl", () => {
it.each(["en", "fr", "es"])("sends the %s locale as ?lang=", (locale) => {
const url = riskUrl("/documents/abc", locale);
expect(url).toContain("/documents/abc/risk");
expect(new URL(url).searchParams.get("lang")).toBe(locale);
});
});
10 changes: 10 additions & 0 deletions frontend/lib/risk-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { apiUrl } from "@/lib/api-config";

/**
* URL of a document's risk assessment, in the user's locale. The backend
* (`GET /documents/:id/risk?lang=`) returns the localised flag descriptions
* for `en`, `fr` and `es`; without `lang` they are always English.
*/
export function riskUrl(documentPath: string, locale: string): string {
return apiUrl(`${documentPath}/risk`, { lang: locale });
}
Loading