1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use nom::number::complete::le_i32;
use nom::IResult;

use crate::parsers::nom_utils::NomCustomError;
use crate::semirings::SerializableSemiring;
use crate::{Label, StateId, Tr};

#[inline]
pub(crate) fn parse_start_state(s: i64) -> Option<StateId> {
    if s == -1 {
        None
    } else {
        Some(s as StateId)
    }
}

#[inline]
pub(crate) fn parse_final_weight<W: SerializableSemiring>(weight: W) -> Option<W> {
    // TODO: Avoid this re-allocation
    let zero_weight = W::zero();
    if weight != zero_weight {
        Some(weight)
    } else {
        None
    }
}

pub(crate) fn parse_bin_fst_tr<W: SerializableSemiring>(
    i: &[u8],
) -> IResult<&[u8], Tr<W>, NomCustomError<&[u8]>> {
    let (i, ilabel) = le_i32(i)?;
    let (i, olabel) = le_i32(i)?;
    let (i, weight) = W::parse_binary(i)?;
    let (i, nextstate) = le_i32(i)?;
    Ok((
        i,
        Tr {
            ilabel: ilabel as Label,
            olabel: olabel as Label,
            weight,
            nextstate: nextstate as StateId,
        },
    ))
}