Skip to main content

legit_hasher/
lib.rs

1//! One implementation of the [`legit_hashes`] definitions for callers in any language: requests in
2//! and results out as JSON lines, from a command line or, with the `serve` feature, Redis Streams.
3//!
4//! The `legit-hasher` binary is a thin shell around this library. The core is [`hash`], one
5//! [`Request`] in and one [`Outcome`] out, which never panics and turns every failure into an
6//! error result. [`run`] drives a batch of requests on a pool of threads with bounded memory,
7//! skipping those an earlier run's output already answers ([`Finished`]). [`distances`] compares
8//! stored hashes and signatures, and [`Info`] reports the definitions, the limits and the CPU.
9//!
10//! ```
11//! use legit_hasher::{Request, Settings, Status, hash};
12//!
13//! let line = br#"{"id": "greeting", "text": "hello there, world",
14//!     "options": {"format": 1, "definitions": [{"id": "minhash-v1", "shingle": 5, "band_keys": 16}]}}"#;
15//! let request = Request::from_json(line).map_err(|outcome| outcome.to_json())?;
16//! let outcome = hash(&request, Settings::default());
17//! assert_eq!(outcome.status, Status::Success);
18//! let result = &outcome.hashes.as_ref().expect("a successful result has hashes")[0];
19//! assert!(result.value.as_deref().is_some_and(|value| value.starts_with("minhash-v1:")));
20//! assert_eq!(result.band_keys.as_ref().map(Vec::len), Some(16));
21//! # Ok::<(), String>(())
22//! ```
23//!
24//! A definition's output is the same on every machine. The runner detects the CPU only to report
25//! it; nothing it detects changes a hash.
26
27#![forbid(unsafe_code)]
28
29mod base64;
30mod definition;
31mod distance;
32mod error;
33mod hasher;
34mod info;
35mod inspect;
36mod orient;
37mod outcome;
38mod request;
39mod resume;
40mod run;
41#[cfg(feature = "serve")]
42pub mod serve;
43
44pub use definition::{Definition, DefinitionFailure, Input, MAX_SHINGLE, Plan, Tokens};
45pub use distance::{Comparison, DistanceError, compare, distances};
46pub use error::{Error, Result};
47pub use hasher::{MAX_METADATA_BYTES, Settings, hash};
48pub use info::{Cpu, DefinitionInfo, Info, Limits, MALLOC_VARIABLE, Malloc, PEAK_BYTES_PER_PIXEL, ServeInfo};
49pub use outcome::{
50    DecodeFacts, DefinitionError, HashOutcome, Outcome, RUNNER, RequestError, Runner, SourceFacts, Status, Timings,
51};
52pub use request::{Entry, Id, MAX_DEFINITIONS, MAX_OPTIONS_BYTES, Options, Orientation, Request, Source};
53pub use resume::Finished;
54pub use run::{DEFAULT_MAX_LINE_BYTES, RunConfig, Summary, run};
55
56/// The version of the request and result formats: `1`. Every result line and `info` carry it,
57/// and a request's `options` must name it. A breaking change to either format raises it.
58pub const FORMAT: u32 = 1;
59
60/// The README's examples, compiled and run as doctests.
61#[cfg(doctest)]
62#[doc = include_str!("../README.md")]
63struct ReadmeDoctests;