sapi_lite/stt/semantics/
tree.rs

1use std::ffi::OsString;
2use std::ops::{Deref, DerefMut};
3
4use windows as Windows;
5use Windows::Win32::Media::Speech::SPPHRASEPROPERTY;
6
7use super::SemanticValue;
8
9/// A tree of values that forms part of the semantic information for a recognized phrase.
10#[derive(Debug, PartialEq, Clone)]
11pub struct SemanticTree {
12    /// The value at the root of this tree.
13    pub value: SemanticValue<OsString>,
14    /// The sub-trees that form this tree.
15    pub children: Vec<SemanticTree>,
16}
17
18impl SemanticTree {
19    pub(crate) fn from_sapi(sapi_prop: Option<&SPPHRASEPROPERTY>) -> Vec<Self> {
20        let mut result = Vec::new();
21        let mut next_prop = sapi_prop;
22        while let Some(prop) = next_prop {
23            if let Ok(value) = SemanticValue::from_sapi(prop) {
24                result.push(SemanticTree {
25                    value,
26                    children: SemanticTree::from_sapi(unsafe { prop.pFirstChild.as_ref() }),
27                });
28            }
29            next_prop = unsafe { prop.pNextSibling.as_ref() };
30        }
31        result
32    }
33}
34
35impl Deref for SemanticTree {
36    type Target = Vec<SemanticTree>;
37
38    fn deref(&self) -> &Self::Target {
39        &self.children
40    }
41}
42
43impl DerefMut for SemanticTree {
44    fn deref_mut(&mut self) -> &mut Self::Target {
45        &mut self.children
46    }
47}