Skip to main content

LanguageHint

Struct LanguageHint 

Source
#[non_exhaustive]
pub struct LanguageHint { pub lang: String, pub source: LangHintSource, pub confidence: f32, }
Expand description

v0.10 Sprint 2 — typed lang hint wrapper(Decision A-prime)。

设计意图:替代裸 Option<&str> 给 SDK consumer / 未来 firewall lang-aware 路径用;强制 provenance + confidence,low-conf 一律 fail-closed 退化。

典型用法(SDK consumer):

use vigil_redaction::lang_hint::{LanguageHint, LangHintSource};

// caller 知道用户 locale → 高信任
let hint = LanguageHint::caller_provided("de");

// 启发式 detect → advisory,low-conf 自动 fail-closed
let hint = LanguageHint::heuristic("de", 0.4);  // confidence 太低,into_lang_str → None

SemVer:#[non_exhaustive] — 未来加字段(如 audit_id / provider_chain) 不破;pub fields 可读,struct literal 构造仅 crate 内可用。

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§lang: String

ISO 639-1 lowercase("en" / "de" / "it" / "fr" / …);与 fixture lang 字段对齐

§source: LangHintSource

信息来源(用于 audit trail + 可信度决策)

§confidence: f32

置信度 [0.0, 1.0];low-conf(< 0.5)into_lang_str 一律返 None(fail-closed 退化 baseline path,避免误降 policy)

Implementations§

Source§

impl LanguageHint

Source

pub fn caller_provided(lang: impl Into<String>) -> LanguageHint

caller 显式传入(CallerProvided source,confidence = 1.0)。 适用:用户 locale 设置 / 业务上下文 / 已校验输入。

Source

pub fn fixture(lang: impl Into<String>) -> LanguageHint

fixture / 测试场景(FixtureExperimental source,confidence = 1.0)。 仅非 production;典型 release-gate / spike-gate / fixture lang 字段透传。

Source

pub fn heuristic(lang: impl Into<String>, confidence: f32) -> LanguageHint

启发式 detect(Heuristic source,caller 给 confidence)。 决策不可信:即使 confidence = 1.0,is_trusted 返 false(由 LangHintSource 静态判定);仅 advisory(diagnose / suggestion UI)。 confidence < 0.5 时 into_lang_str 也返 None。

Source

pub fn detect(text: &str) -> LanguageHint

v0.10 Sprint 6 — 启发式 lang detect(advisory only)。

调用 detect_lang_heuristic 内部启发式(unicode 字符集 + 关键词),返 LanguageHint with LangHintSource::Heuristic始终返 Heuristiclang_str() 永返 None,无法触发 firewall 决策路径。

适用场景:

  • fixture lang 字段标注辅助(P1.0 启发式同口径,但用 Rust 端版本)
  • SDK consumer 诊断 UI / 建议性显示
  • 永不作 production 决策权威(feedback_lang_review_authoritative 约束)
Source

pub fn lang_str(&self) -> Option<&str>

fail-closed 转换:返 Option<&str>(走 scan_text_with_engine_with_lang)。

决策规则:

  • confidence < LANG_HINT_TRUSTED_CONFIDENCE(0.5)→ None(退化 baseline)
  • LangHintSource::HeuristicNone(无论 confidence,启发式不可信)
  • 其他(CallerProvided / FixtureExperimental + confidence ≥ 0.5)→ Some(&lang)

这是 D=C 决议下的 SDK 边界 — caller 即使传 Heuristic,也不会触发 lang-conditional threshold(防 production 误降 policy)。

Trait Implementations§

Source§

impl Clone for LanguageHint

Source§

fn clone(&self) -> LanguageHint

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for LanguageHint

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.