reductionml_core/
parsers.rs

1mod text_parser;
2
3pub use text_parser::*;
4mod vw_text_parser;
5pub use vw_text_parser::*;
6mod dsjson_parser;
7pub use dsjson_parser::*;
8mod json_parser;
9pub use json_parser::*;
10
11use crate::{hash::hash_bytes, FeatureHash, NamespaceHash};
12
13pub enum ParsedFeature<'a> {
14    Simple { name: &'a str },
15    SimpleWithStringValue { name: &'a str, value: &'a str },
16    Anonymous { offset: u32 },
17}
18
19impl<'a> ParsedFeature<'a> {
20    pub fn hash(&self, namespace_hash: NamespaceHash) -> FeatureHash {
21        match &self {
22            ParsedFeature::Simple { name } => {
23                FeatureHash::from(hash_bytes(name.as_bytes(), *namespace_hash))
24            }
25            ParsedFeature::SimpleWithStringValue { name, value } => {
26                let name_key_hash = hash_bytes(name.as_bytes(), *namespace_hash);
27                FeatureHash::from(hash_bytes(value.as_bytes(), name_key_hash))
28            }
29            ParsedFeature::Anonymous { offset } => (*namespace_hash + offset).into(),
30        }
31    }
32}