Skip to main content

v_common_onto/
parser.rs

1use crate::cbor2individual::*;
2use crate::individual::*;
3use crate::msgpack2individual::*;
4
5#[derive(PartialEq, Debug)]
6pub enum RawType {
7    Cbor,
8    Json,
9    Msgpack,
10    Unknown,
11}
12
13pub fn parse_to_predicate(expect_predicate: &str, iraw: &mut Individual) -> bool {
14    if iraw.raw.raw_type == RawType::Msgpack {
15        if let Err(e) = parse_msgpack_to_predicate(expect_predicate, iraw) {
16            if !e.is_empty() {
17                error!("parse for [{}], err={}", expect_predicate, e);
18            }
19            return false;
20        }
21        return true;
22    } else if iraw.raw.raw_type == RawType::Cbor {
23        return parse_cbor_to_predicate(expect_predicate, iraw);
24    }
25
26    false
27}
28
29const MSGPACK_MAGIC_HEADER: u8 = 146;
30
31pub fn parse_raw(iraw: &mut Individual) -> Result<(), i8> {
32    if iraw.raw.data.is_empty() {
33        return Ok(());
34    }
35
36    let traw: &[u8] = iraw.raw.data.as_slice();
37
38    if traw[0] == MSGPACK_MAGIC_HEADER {
39        iraw.raw.raw_type = RawType::Msgpack;
40    } else {
41        iraw.raw.raw_type = RawType::Cbor;
42    }
43
44    let res = if iraw.raw.raw_type == RawType::Msgpack {
45        parse_msgpack(&mut iraw.raw)
46    } else if iraw.raw.raw_type == RawType::Cbor {
47        parse_cbor(&mut iraw.raw)
48    } else {
49        Err(-1)
50    };
51
52    if let Ok(uri) = res {
53        iraw.obj.uri = uri;
54        return Ok(());
55    }
56
57    Err(-1)
58}