sapi_lite/stt/grammar/
mod.rs

1use std::mem::ManuallyDrop;
2use std::ptr::null_mut;
3
4use windows as Windows;
5use Windows::Win32::Media::Speech::{
6    ISpRecoGrammar, SPGRAMMARSTATE, SPGS_DISABLED, SPGS_ENABLED, SPRS_ACTIVE, SPRS_INACTIVE,
7    SPRULESTATE,
8};
9
10use crate::com_util::Intf;
11use crate::Result;
12
13use super::RecognitionPauser;
14
15mod builder;
16mod rule;
17
18pub use builder::GrammarBuilder;
19pub use rule::{RepeatRange, Rule, RuleArena};
20
21/// A set of rules that define phrases that can be recognized.
22pub struct Grammar {
23    intf: ManuallyDrop<Intf<ISpRecoGrammar>>,
24    pauser: RecognitionPauser,
25}
26
27impl Grammar {
28    /// Enables or disables the recognition of all the phrases defined in this grammar.
29    pub fn set_enabled(&self, enabled: bool) -> Result<()> {
30        let _pause = self.pauser.pause()?;
31        unsafe { self.intf.SetGrammarState(grammar_state(enabled)) }
32    }
33
34    /// Enables or disables the recognition of the phrases defined by the rule with the given name.
35    pub fn set_rule_enabled<S: AsRef<str>>(&self, name: S, enabled: bool) -> Result<()> {
36        let _pause = self.pauser.pause()?;
37        unsafe {
38            self.intf
39                .SetRuleState(name.as_ref(), null_mut(), rule_state(enabled))
40        }
41    }
42}
43
44impl Drop for Grammar {
45    fn drop(&mut self) {
46        let _pause = self.pauser.pause();
47        unsafe { ManuallyDrop::drop(&mut self.intf) };
48    }
49}
50
51fn grammar_state(enabled: bool) -> SPGRAMMARSTATE {
52    if enabled {
53        SPGS_ENABLED
54    } else {
55        SPGS_DISABLED
56    }
57}
58
59fn rule_state(enabled: bool) -> SPRULESTATE {
60    if enabled {
61        SPRS_ACTIVE
62    } else {
63        SPRS_INACTIVE
64    }
65}