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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use crate::{wrap, CLabel, CStateId, RUSTFST_FFI_RESULT};

use ffi_convert::*;
use rustfst::prelude::{StateId, Tr};
use rustfst::semirings::TropicalWeight;
use rustfst::Semiring;

#[derive(Debug)]
#[repr(C)]
#[derive(CReprOf, AsRust, CDrop, RawPointerConverter)]
#[target_type(Tr::<TropicalWeight>)]
pub struct CTr {
    /// Input label.
    pub ilabel: CLabel,
    /// Output label.
    pub olabel: CLabel,
    /// Weight.
    pub weight: CTropicalWeight,
    /// ID of the next state.
    pub nextstate: CStateId,
}

#[derive(Debug)]
#[repr(C)]
#[derive(CDrop, RawPointerConverter)]
pub struct CTropicalWeight {
    value: libc::c_float,
}

impl CReprOf<TropicalWeight> for CTropicalWeight {
    fn c_repr_of(input: TropicalWeight) -> Result<Self, CReprOfError> {
        Ok(Self {
            value: input.take_value(),
        })
    }
}

impl AsRust<TropicalWeight> for CTropicalWeight {
    fn as_rust(&self) -> Result<TropicalWeight, AsRustError> {
        Ok(self.value.into())
    }
}

#[no_mangle]
pub extern "C" fn tr_new(
    ilabel: CLabel,
    olabel: CLabel,
    weight: libc::c_float,
    nextstate: CStateId,
    new_struct: *mut *const CTr,
) -> RUSTFST_FFI_RESULT {
    wrap(|| {
        let tr = CTr {
            ilabel,
            olabel,
            weight: CTropicalWeight { value: weight },
            nextstate,
        };
        let raw_pointer: *mut CTr = Box::into_raw(Box::new(tr));
        unsafe { *new_struct = raw_pointer };
        Ok(())
    })
}

#[no_mangle]
pub extern "C" fn tr_ilabel(tr: *const CTr, ilabel: *mut CLabel) -> RUSTFST_FFI_RESULT {
    wrap(|| {
        let tr = unsafe { <CTr as ffi_convert::RawBorrow<CTr>>::raw_borrow(tr)? };
        let ilabel_val = tr.ilabel;
        unsafe { *ilabel = ilabel_val }
        Ok(())
    })
}

#[no_mangle]
pub extern "C" fn tr_set_ilabel(tr: *mut CTr, ilabel: *const CLabel) -> RUSTFST_FFI_RESULT {
    wrap(|| {
        let tr = &mut unsafe { <CTr as ffi_convert::RawBorrowMut<CTr>>::raw_borrow_mut(tr)? };
        tr.ilabel = ilabel as CLabel;
        Ok(())
    })
}

#[no_mangle]
pub extern "C" fn tr_olabel(tr: *const CTr, olabel: *mut CLabel) -> RUSTFST_FFI_RESULT {
    wrap(|| {
        let tr = unsafe { <CTr as ffi_convert::RawBorrow<CTr>>::raw_borrow(tr)? };
        let olabel_val = tr.olabel;
        unsafe { *olabel = olabel_val }
        Ok(())
    })
}

#[no_mangle]
pub extern "C" fn tr_set_olabel(tr: *mut CTr, olabel: *const CLabel) -> RUSTFST_FFI_RESULT {
    wrap(|| {
        let tr = &mut unsafe { <CTr as ffi_convert::RawBorrowMut<CTr>>::raw_borrow_mut(tr)? };
        tr.olabel = olabel as CLabel;
        Ok(())
    })
}

#[no_mangle]
pub extern "C" fn tr_weight(tr: *const CTr, weight: *mut libc::c_float) -> RUSTFST_FFI_RESULT {
    wrap(|| {
        let tr = unsafe { <CTr as ffi_convert::RawBorrow<CTr>>::raw_borrow(tr)? };
        let weight_val = *tr.weight.as_rust()?.value();
        unsafe { *weight = weight_val }
        Ok(())
    })
}

#[no_mangle]
pub extern "C" fn tr_set_weight(tr: *mut CTr, weight: libc::c_float) -> RUSTFST_FFI_RESULT {
    wrap(|| {
        let tr = &mut unsafe { <CTr as ffi_convert::RawBorrowMut<CTr>>::raw_borrow_mut(tr)? };
        let tropical_weight = TropicalWeight::new(weight as f32);
        tr.weight = CTropicalWeight::c_repr_of(tropical_weight)?;
        Ok(())
    })
}

#[no_mangle]
pub extern "C" fn tr_next_state(tr: *const CTr, next_state: *mut CStateId) -> RUSTFST_FFI_RESULT {
    wrap(|| {
        let tr = unsafe { <CTr as ffi_convert::RawBorrow<CTr>>::raw_borrow(tr)? };
        let next_state_val = tr.nextstate;
        unsafe { *next_state = next_state_val }
        Ok(())
    })
}

#[no_mangle]
pub extern "C" fn tr_set_next_state(
    tr: *mut CTr,
    next_state: *const CStateId,
) -> RUSTFST_FFI_RESULT {
    wrap(|| {
        let tr = &mut unsafe { <CTr as ffi_convert::RawBorrowMut<CTr>>::raw_borrow_mut(tr)? };
        tr.nextstate = next_state as StateId;
        Ok(())
    })
}

#[no_mangle]
pub extern "C" fn tr_delete(tr_ptr: *mut CTr) -> RUSTFST_FFI_RESULT {
    wrap(|| {
        if tr_ptr.is_null() {
            return Ok(());
        }

        unsafe { drop(Box::from_raw(tr_ptr)) }
        Ok(())
    })
}