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
#[allow(unused_imports)]
use serde::{Serialize, Deserialize};
#[allow(unused_imports)]
use bincode;
#[allow(unused_imports)]
use serde_json::Value as JsonValue;

#[derive(Clone)]
#[derive(Serialize, Deserialize)]
pub struct BufferObject {
    pub path: String,
    pub result: String,
    string: String,
    json: JsonValue,
    typ: String,
}

impl BufferObject {

    pub fn new(path: String) -> Self {
        let content = std::fs::read_to_string(&path).unwrap();
        BufferObject {
            path: path.clone(),
            result: String::new(),
            string: String::from(content),
            json: JsonValue::default(), // Hinzugefügt, um das Feld `json` zu initialisieren
            typ: "base".to_string(),
        }
    }

    pub fn from_json(path: String) -> Self {
        let content = std::fs::read_to_string(&path).unwrap();
        let json: JsonValue = serde_json::from_str(&content).unwrap();
        BufferObject {
            path: path.clone(),
            result: String::new(),
            string: String::from(content),
            json: json, // Korrigiert, um das Feld `json` direkt zuzuweisen
            typ: "json".to_string(),
        }
    }

    pub fn to_json(self) -> JsonValue {
        let json = if self.typ == "json" {
            self.json.clone()
        } else {
            let _string = self.string.clone();
            let _json = serde_json::from_str(&_string).unwrap();
            _json
        };
        json
    }

    pub fn to_string(self) -> String {
        let string = if self.typ == "base" {
            self.string.clone()
        } else {
            let _json = self.json.clone();
            let _string = serde_json::to_string(&_json).unwrap();
            _string
        };
        string
    }

    pub fn to_cstring(self) -> std::ffi::CString {
        let string = self.to_string();
        let cstring = std::ffi::CString::new(string).unwrap();
        cstring
    }

    pub fn get_serialized(self) -> Vec<u8> {
        let seri = bincode::serialize(&self.to_string()).unwrap();
        seri
    }
}