eric_sdk/
response.rs

1use crate::error_code::ErrorCode;
2use eric_bindings::{
3    EricReturnBufferApi, EricRueckgabepufferErzeugen, EricRueckgabepufferFreigeben,
4    EricRueckgabepufferInhalt,
5};
6use std::ffi::CStr;
7
8/// A structure which summarizes the response from the Eric instance.
9#[derive(Debug)]
10pub struct EricResponse {
11    /// The error code returned by the Eric instance.
12    pub error_code: i32,
13    /// The response when validating an XML file.
14    pub validation_response: String,
15    /// The response when an XML file is send to the tax authorities.
16    pub server_response: String,
17}
18
19impl EricResponse {
20    pub fn new(error_code: i32, validation_response: String, server_response: String) -> Self {
21        Self {
22            error_code,
23            validation_response,
24            server_response,
25        }
26    }
27}
28
29pub struct ResponseBuffer {
30    ctx: *mut EricReturnBufferApi,
31}
32
33impl ResponseBuffer {
34    pub fn new() -> Result<Self, anyhow::Error> {
35        let response_buffer = unsafe { EricRueckgabepufferErzeugen() };
36
37        Ok(ResponseBuffer {
38            ctx: response_buffer,
39        })
40    }
41
42    pub fn as_ptr(&self) -> *mut EricReturnBufferApi {
43        self.ctx
44    }
45
46    pub fn read(&self) -> Result<&str, anyhow::Error> {
47        let buffer = unsafe {
48            let ptr = EricRueckgabepufferInhalt(self.ctx);
49            CStr::from_ptr(ptr)
50        };
51
52        Ok(buffer.to_str()?)
53    }
54}
55
56impl Drop for ResponseBuffer {
57    fn drop(&mut self) {
58        println!("Cleaning up response buffer");
59
60        let error_code = unsafe { EricRueckgabepufferFreigeben(self.ctx) };
61
62        match error_code {
63            x if x == ErrorCode::ERIC_OK as i32 => (),
64            error_code => panic!("Can't drop reponse buffer: {}", error_code),
65        }
66    }
67}