Skip to main content

flynt/
lib.rs

1// src/lib.rs
2
3//! Lint Fluent translation keys against their use in Rust code and Askama templates.
4//!
5//! Keys used in templates `{{ "key" | t(&lang) }}` and in Rust `loc("key", &lang)` are collected
6//! and compared against the `.ftl` files. A typo then fails the build instead of shipping the key
7//! name as user-facing text.
8//!
9//! flynt infers what to scan from the target repository. Workspace members come from its
10//! `Cargo.toml`. Locales come from the subdirectories of its locales directory. Every default can
11//! be overridden, on the command line or in a `.flynt.toml`.
12//!
13//! # Checks
14//!
15//! - coverage: a used key is defined in every locale.
16//! - consistency: every locale defines the same set of keys.
17//! - duplicates: no locale defines a key twice.
18//! - unused: every defined key is referenced somewhere. This is a warning by default.
19//! - syntax: every `.ftl` file parses.
20//!
21//! # Example
22//!
23//! ```no_run
24//! use flynt::config::{self, PartialConfig};
25//!
26//! let cli = PartialConfig {
27//!     root: Some("../my-project".into()),
28//!     ..PartialConfig::default()
29//! };
30//! let config = config::load(&cli)?;
31//! let report = flynt::check(&config)?;
32//!
33//! for finding in &report.missing_keys {
34//!     println!("{} is missing in {}", finding.key, finding.missing_in.join(", "));
35//! }
36//! assert_eq!(report.exit_code(), 0);
37//! # Ok::<(), anyhow::Error>(())
38//! ```
39
40#![deny(unsafe_code)]
41#![warn(clippy::pedantic, missing_docs)]
42
43mod check;
44pub mod config;
45mod extract;
46pub mod model;
47mod parse;
48pub mod report;
49
50pub use config::{ColorChoice, Config, OutputFormat, PartialConfig, Severity};
51pub use model::{
52	DuplicateKey, InconsistentKey, KeyDefinition, KeyUsage, Location, MissingKey, ParseError,
53	Report, SCHEMA_VERSION, Summary, UnusedKey, UsageType,
54};
55
56/// Runs every check and returns the report. Performs no output.
57///
58/// Rendering is [`report::render`]'s job. This lets a caller embedding flynt act on the findings
59/// instead of parsing text.
60///
61/// # Errors
62///
63/// Returns an error for tool-level problems only. This includes: a directory that cannot be walked,
64/// a file that is not valid UTF-8, an invalid glob, a missing locales directory (unless
65/// `require_locales` is off), or a tree with nothing at all to check. Lint findings are carried in
66/// the [`Report`], not returned as errors.
67pub fn check(config: &Config) -> anyhow::Result<Report> {
68	check::run(config)
69}