summaryrefslogtreecommitdiff
path: root/support/texlab/crates/bibutils/src/lib.rs
blob: f14db68b59cc308cc47979fb1c3fe126467932c4 (plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use bibutils_sys::*;
use std::ffi::CString;
use std::fs;
use std::mem::MaybeUninit;
use std::path::Path;
use tempfile::tempdir;

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum InputFormat {
    Bibtex,
    Biblatex,
    Copac,
    Ebi,
    Endnote,
    EndnoteXml,
    Medline,
    Mods,
    Nbib,
    Ris,
    Word,
}

impl InputFormat {
    fn read_mode(self) -> u32 {
        match self {
            Self::Bibtex => BIBL_BIBTEXIN,
            Self::Biblatex => BIBL_BIBLATEXIN,
            Self::Copac => BIBL_COPACIN,
            Self::Ebi => BIBL_EBIIN,
            Self::Endnote => BIBL_ENDNOTEIN,
            Self::EndnoteXml => BIBL_ENDNOTEXMLIN,
            Self::Medline => BIBL_MEDLINEIN,
            Self::Mods => BIBL_MODSIN,
            Self::Nbib => BIBL_NBIBIN,
            Self::Ris => BIBL_RISIN,
            Self::Word => BIBL_WORDIN,
        }
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum OutputFormat {
    Adsabs,
    Bibtex,
    Endnote,
    Isi,
    Mods,
    Nbib,
    Ris,
    Word2007,
}

impl OutputFormat {
    fn write_mode(self) -> u32 {
        match self {
            Self::Adsabs => BIBL_ADSABSOUT,
            Self::Bibtex => BIBL_BIBTEXOUT,
            Self::Endnote => BIBL_ENDNOTEOUT,
            Self::Isi => BIBL_ISIOUT,
            Self::Mods => BIBL_MODSOUT,
            Self::Nbib => BIBL_NBIBOUT,
            Self::Ris => BIBL_RISOUT,
            Self::Word2007 => BIBL_WORD2007OUT,
        }
    }
}

struct Context {
    inner: MaybeUninit<bibl>,
}

impl Context {
    fn new() -> Self {
        let mut inner = MaybeUninit::zeroed();
        unsafe {
            bibl_init(inner.as_mut_ptr());
        }
        Self { inner }
    }
}

impl Drop for Context {
    fn drop(&mut self) {
        unsafe {
            bibl_free(self.inner.as_mut_ptr());
        }
    }
}

unsafe impl Send for Context {}

struct Params {
    inner: MaybeUninit<param>,
}

impl Params {
    fn new(from: InputFormat, to: OutputFormat) -> Self {
        let program = CString::new("texlab").unwrap();
        let mut inner = MaybeUninit::zeroed();
        unsafe {
            bibl_initparams(
                inner.as_mut_ptr(),
                from.read_mode() as i32,
                to.write_mode() as i32,
                program.as_ptr(),
            );
        }
        Self { inner }
    }
}

impl Drop for Params {
    fn drop(&mut self) {
        unsafe {
            bibl_freeparams(self.inner.as_mut_ptr());
        }
    }
}

unsafe impl Send for Params {}

struct File {
    path: CString,
    handle: *mut FILE,
}

impl File {
    fn new<M: Into<Vec<u8>>>(path: &Path, mode: M) -> Self {
        let path = CString::new(path.to_str().unwrap()).unwrap();
        let mode = CString::new(mode).unwrap();
        let handle = unsafe { fopen(path.as_ptr(), mode.as_ptr()) };
        Self { path, handle }
    }
}

impl Drop for File {
    fn drop(&mut self) {
        unsafe {
            fclose(self.handle);
        }
    }
}

unsafe impl Send for File {}

pub fn convert(input: String, from: InputFormat, to: OutputFormat) -> Option<String> {
    let mut context = Context::new();
    let mut params = Params::new(from, to);
    let dir = tempdir().expect("failed to create a temporary directory");

    let input_path = dir.path().join("input");
    fs::write(&input_path, input).ok()?;
    let input_file = File::new(&input_path, "r");
    unsafe {
        let status = bibl_read(
            context.inner.as_mut_ptr(),
            input_file.handle,
            input_file.path.as_ptr(),
            params.inner.as_mut_ptr(),
        );

        if status != BIBL_OK as i32 {
            return None;
        }
    }

    let output_path = dir.path().join("output");
    let output_file = File::new(&output_path, "w");
    unsafe {
        let status = bibl_write(
            context.inner.as_mut_ptr(),
            output_file.handle,
            params.inner.as_mut_ptr(),
        );

        if status != BIBL_OK as i32 {
            return None;
        }
    }

    // Remove BOM
    let data = fs::read(&output_path).ok()?;
    let text = String::from_utf8_lossy(&data[3..]).into_owned();
    Some(text)
}