leptris 1.6.13

Rust bindings for libleptris — fast XML 1.0 parsing, XPath 1.0, SAX
Documentation
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
//! Idiomatic Rust bindings for [libleptris](https://github.com/leptris/leptris),
//! a pure-C99 XML 1.0 parser with an XPath 1.0 engine and SAX front end.
//!
//! ```no_run
//! use leptris::Document;
//!
//! let doc = Document::parse(b"<root><a id='1'>hello</a></root>").unwrap();
//! let root = doc.root().unwrap();
//! assert_eq!(root.name(), "root");
//!
//! let hits = doc.xpath("//a").unwrap();
//! assert_eq!(hits.len(), 1);
//! assert_eq!(hits.element(0).unwrap().text(), Some("hello"));
//! ```
//!
//! The C library is resolved at link time: set `LEPTRIS_LIB_PATH`
//! (see `build.rs`) or install libleptris on the library path.

mod ffi;

// #1900: the wasm-bindgen face (wasm32 + `wasm` feature only —
// native targets keep the dynamic-link Document API).
#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
pub mod wasmface;

// #1900: the C shim's malloc/free/realloc route through THESE
// wrappers over std::alloc. Two growing allocators in one module
// (rustc's dlmalloc + any C-side bump) corrupt each other — both
// start at __heap_base — so the C side must share the rust allocator.
// Crate-defined wrappers (not direct __rust_alloc externs): extern
// references from C objects never pull std's allocator archive
// member, but these functions are Rust code, so std::alloc links.
#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
#[no_mangle]
pub extern "C" fn leptris_rust_alloc(size: usize, align: usize) -> *mut u8 {
    unsafe {
        std::alloc::alloc(std::alloc::Layout::from_size_align(size, align).unwrap_unchecked())
    }
}

#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
#[no_mangle]
pub extern "C" fn leptris_rust_dealloc(ptr: *mut u8, size: usize, align: usize) {
    unsafe {
        std::alloc::dealloc(
            ptr,
            std::alloc::Layout::from_size_align(size, align).unwrap_unchecked(),
        )
    }
}

#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
#[no_mangle]
pub extern "C" fn leptris_rust_realloc(
    ptr: *mut u8,
    old_size: usize,
    align: usize,
    new_size: usize,
) -> *mut u8 {
    unsafe {
        std::alloc::realloc(
            ptr,
            std::alloc::Layout::from_size_align(old_size, align).unwrap_unchecked(),
            new_size,
        )
    }
}

use std::ffi::{CStr, CString};
use std::fmt;
use std::marker::PhantomData;
use std::os::raw::c_char;

/// Error surfaced by every fallible binding call. Mirrors the
/// `LeptrisStatus` codes from the C API.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Error {
    Memory,
    Parse,
    Xpath,
    NullArg,
    InvalidArg,
    NotFound,
    Io,
    NotImplemented,
    Unknown(i32),
}

impl Error {
    fn from_status(status: i32) -> Error {
        match status {
            ffi::LEPTRIS_ERROR_MEMORY => Error::Memory,
            ffi::LEPTRIS_ERROR_PARSE => Error::Parse,
            ffi::LEPTRIS_ERROR_XPATH => Error::Xpath,
            ffi::LEPTRIS_ERROR_NULL_ARG => Error::NullArg,
            ffi::LEPTRIS_ERROR_INVALID_ARG => Error::InvalidArg,
            ffi::LEPTRIS_ERROR_NOT_FOUND => Error::NotFound,
            ffi::LEPTRIS_ERROR_IO => Error::Io,
            ffi::LEPTRIS_ERROR_NOT_IMPLEMENTED => Error::NotImplemented,
            other => Error::Unknown(other),
        }
    }

    /// The C error message for this status.
    pub fn message(&self) -> &'static str {
        let code = match self {
            Error::Memory => ffi::LEPTRIS_ERROR_MEMORY,
            Error::Parse => ffi::LEPTRIS_ERROR_PARSE,
            Error::Xpath => ffi::LEPTRIS_ERROR_XPATH,
            Error::NullArg => ffi::LEPTRIS_ERROR_NULL_ARG,
            Error::InvalidArg => ffi::LEPTRIS_ERROR_INVALID_ARG,
            Error::NotFound => ffi::LEPTRIS_ERROR_NOT_FOUND,
            Error::Io => ffi::LEPTRIS_ERROR_IO,
            Error::NotImplemented => ffi::LEPTRIS_ERROR_NOT_IMPLEMENTED,
            Error::Unknown(c) => *c,
        };
        unsafe {
            let msg = ffi::leptris_error_message(code);
            if msg.is_null() {
                "unknown error"
            } else {
                CStr::from_ptr(msg).to_str().unwrap_or("unknown error")
            }
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} ({:?})", self.message(), self)
    }
}

impl std::error::Error for Error {}

pub type Result<T> = std::result::Result<T, Error>;

unsafe fn cstr_opt(ptr: *const c_char) -> Option<&'static str> {
    if ptr.is_null() {
        None
    } else {
        Some(CStr::from_ptr(ptr).to_str().unwrap_or(""))
    }
}

/// A parsed XML document. Owns the C document; freed on drop.
pub struct Document {
    raw: *mut ffi::LeptrisDocument,
}

impl Document {
    /// Parse XML from bytes. The bytes must be valid UTF-8 (XML 1.0).
    pub fn parse(xml: &[u8]) -> Result<Document> {
        let mut status: i32 = ffi::LEPTRIS_OK;
        let raw = unsafe {
            ffi::leptris_parse_string(xml.as_ptr() as *const c_char, xml.len(), &mut status)
        };
        if raw.is_null() {
            return Err(Error::from_status(status));
        }
        Ok(Document { raw })
    }

    /// The document element.
    pub fn root(&self) -> Option<Element<'_>> {
        let raw = unsafe { ffi::leptris_document_root(self.raw) };
        if raw.is_null() {
            None
        } else {
            Some(Element {
                raw,
                doc: PhantomData,
            })
        }
    }

    /// Evaluate an XPath 1.0 expression against the whole document.
    pub fn xpath(&self, expr: &str) -> Result<XPathResult<'_>> {
        let c_expr = CString::new(expr).map_err(|_| Error::InvalidArg)?;
        let raw =
            unsafe { ffi::leptris_xpath_eval(self.raw, std::ptr::null_mut(), c_expr.as_ptr()) };
        if raw.is_null() {
            return Err(Error::Xpath);
        }
        Ok(XPathResult {
            raw,
            doc: PhantomData,
        })
    }

    /// Serialize the document back to an owned XML string.
    pub fn serialize(&self) -> Result<String> {
        let raw = unsafe { ffi::leptris_document_serialize(self.raw, std::ptr::null_mut()) };
        if raw.is_null() {
            return Err(Error::Unknown(0));
        }
        let s = unsafe {
            let s = CStr::from_ptr(raw).to_string_lossy().into_owned();
            ffi::leptris_free_string(raw);
            s
        };
        Ok(s)
    }
}

impl Drop for Document {
    fn drop(&mut self) {
        unsafe { ffi::leptris_document_free(self.raw) };
    }
}

/// An element, borrowed from its document for its whole lifetime.
#[derive(Clone, Copy)]
pub struct Element<'doc> {
    raw: *mut ffi::LeptrisElement,
    doc: PhantomData<&'doc Document>,
}

impl<'doc> Element<'doc> {
    /// The element's name.
    pub fn name(&self) -> &'doc str {
        unsafe { cstr_opt(ffi::leptris_element_name(self.raw)).unwrap_or("") }
    }

    /// The element's text content (direct text children).
    pub fn text(&self) -> Option<&'doc str> {
        unsafe { cstr_opt(ffi::leptris_element_text(self.raw)) }
    }

    /// Number of child elements.
    pub fn child_count(&self) -> usize {
        unsafe { ffi::leptris_element_child_count(self.raw) }
    }

    /// First child element with the given name, if any.
    pub fn child(&self, name: &str) -> Option<Element<'doc>> {
        let c_name = CString::new(name).ok()?;
        let raw = unsafe { ffi::leptris_element_first_child(self.raw, c_name.as_ptr()) };
        if raw.is_null() {
            None
        } else {
            Some(Element {
                raw,
                doc: PhantomData,
            })
        }
    }

    /// First child element of any name.
    pub fn first_child(&self) -> Option<Element<'doc>> {
        let raw = unsafe { ffi::leptris_element_first_child_any(self.raw) };
        if raw.is_null() {
            None
        } else {
            Some(Element {
                raw,
                doc: PhantomData,
            })
        }
    }

    /// Next sibling element of any name.
    pub fn next_sibling(&self) -> Option<Element<'doc>> {
        let raw = unsafe { ffi::leptris_element_next_sibling_any(self.raw) };
        if raw.is_null() {
            None
        } else {
            Some(Element {
                raw,
                doc: PhantomData,
            })
        }
    }

    /// Iterate this element's child elements.
    pub fn children(&self) -> ElementChildren<'doc> {
        ElementChildren {
            next: self.first_child(),
        }
    }

    /// Iterate this element's attributes as (name, value) pairs.
    pub fn attributes(&self) -> Attributes<'doc> {
        Attributes {
            elem: *self,
            next: unsafe {
                let first = ffi::leptris_element_first_attribute(self.raw);
                if first.is_null() {
                    None
                } else {
                    Some(first)
                }
            },
        }
    }

    /// Look up one attribute value by name.
    pub fn attribute(&self, name: &str) -> Option<&'doc str> {
        self.attributes().find(|(n, _)| *n == name).map(|(_, v)| v)
    }
}

/// Iterator over child elements.
pub struct ElementChildren<'doc> {
    next: Option<Element<'doc>>,
}

impl<'doc> Iterator for ElementChildren<'doc> {
    type Item = Element<'doc>;

    fn next(&mut self) -> Option<Element<'doc>> {
        let cur = self.next?;
        self.next = cur.next_sibling();
        Some(cur)
    }
}

/// Iterator over an element's attributes.
pub struct Attributes<'doc> {
    elem: Element<'doc>,
    next: Option<*mut ffi::LeptrisAttribute>,
}

impl<'doc> Iterator for Attributes<'doc> {
    type Item = (&'doc str, &'doc str);

    fn next(&mut self) -> Option<Self::Item> {
        let attr = self.next?;
        unsafe {
            let name = cstr_opt(ffi::leptris_attribute_get_name(attr))?;
            let value = cstr_opt(ffi::leptris_attribute_get_value(self.elem.raw, attr))?;
            self.next = {
                let n = ffi::leptris_attribute_next(attr);
                if n.is_null() {
                    None
                } else {
                    Some(n)
                }
            };
            Some((name, value))
        }
    }
}

/// The XPath node kind reported by [`XPathResult::kind`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum XPathNodeKind {
    Element,
    Attribute,
    Text,
    Other,
}

/// A live XPath result, borrowed from its document; freed on drop.
pub struct XPathResult<'doc> {
    raw: *mut ffi::LeptrisXPathResult,
    doc: PhantomData<&'doc Document>,
}

impl<'doc> XPathResult<'doc> {
    /// Number of nodes in the result (nodesets only).
    pub fn len(&self) -> usize {
        unsafe { ffi::leptris_xpath_result_count(self.raw) }
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// The element at `index` (elements only; other kinds are skipped
    /// by the C API and yield `None`).
    pub fn element(&self, index: usize) -> Option<Element<'doc>> {
        let raw = unsafe { ffi::leptris_xpath_result_get(self.raw, index) };
        if raw.is_null() {
            None
        } else {
            Some(Element {
                raw,
                doc: PhantomData,
            })
        }
    }

    /// The kind of the node at `index` (mixed nodesets).
    pub fn kind(&self, index: usize) -> XPathNodeKind {
        match unsafe { ffi::leptris_xpath_result_node_kind(self.raw, index) } {
            ffi::LEPTRIS_XPATH_NODE_ELEMENT => XPathNodeKind::Element,
            ffi::LEPTRIS_XPATH_NODE_ATTRIBUTE => XPathNodeKind::Attribute,
            ffi::LEPTRIS_XPATH_NODE_TEXT => XPathNodeKind::Text,
            _ => XPathNodeKind::Other,
        }
    }

    /// The name of the node at `index` (elements and attributes).
    pub fn node_name(&self, index: usize) -> Option<&'doc str> {
        unsafe { cstr_opt(ffi::leptris_xpath_result_node_name(self.raw, index)) }
    }

    /// The string value of the node at `index` (text, comment, CDATA,
    /// attributes).
    pub fn node_value(&self, index: usize) -> Option<&'doc str> {
        unsafe { cstr_opt(ffi::leptris_xpath_result_node_value(self.raw, index)) }
    }

    /// Numeric result value.
    pub fn number(&self) -> f64 {
        unsafe { ffi::leptris_xpath_result_number(self.raw) }
    }

    /// String result value (owned copy).
    pub fn to_string(&self) -> String {
        unsafe {
            let raw = ffi::leptris_xpath_result_string(self.raw);
            if raw.is_null() {
                return String::new();
            }
            let s = CStr::from_ptr(raw).to_string_lossy().into_owned();
            ffi::leptris_free_string(raw);
            s
        }
    }
}

impl Drop for XPathResult<'_> {
    fn drop(&mut self) {
        unsafe { ffi::leptris_xpath_result_free(self.raw) };
    }
}

pub mod sax {
    //! Event-driven parsing with closures — a Rust-flavored veneer
    //! over `leptris_sax_parse`.

    use super::ffi;
    use std::ffi::CStr;
    use std::os::raw::{c_char, c_int};

    /// Callbacks for SAX events; every field is optional. The
    /// lifetime lets closures borrow local state for the duration of
    /// the parse.
    #[derive(Default)]
    pub struct Handler<'a> {
        pub start_document: Option<Box<dyn FnMut() + 'a>>,
        pub end_document: Option<Box<dyn FnMut() + 'a>>,
        pub start_element: Option<Box<dyn FnMut(&str, &[(String, String)]) + 'a>>,
        pub end_element: Option<Box<dyn FnMut(&str) + 'a>>,
        pub characters: Option<Box<dyn FnMut(&str) + 'a>>,
        pub comment: Option<Box<dyn FnMut(&str) + 'a>>,
        pub cdata: Option<Box<dyn FnMut(&str) + 'a>>,
        pub processing_instruction: Option<Box<dyn FnMut(&str, &str) + 'a>>,
        pub start_prefix_mapping: Option<Box<dyn FnMut(&str, &str) + 'a>>,
        pub end_prefix_mapping: Option<Box<dyn FnMut(&str) + 'a>>,
        pub error: Option<Box<dyn FnMut(&str, i32, i32) + 'a>>,
    }

    /* Trampolines: user_data is a *mut Handler. */
    unsafe fn ud<'h>(data: *mut core::ffi::c_void) -> &'h mut Handler<'h> {
        &mut *(data as *mut Handler)
    }
    fn s<'a>(p: *const c_char) -> &'a str {
        if p.is_null() {
            ""
        } else {
            unsafe { CStr::from_ptr(p).to_str().unwrap_or("") }
        }
    }

    unsafe extern "C" fn tr_start_document(data: *mut core::ffi::c_void) {
        if let Some(f) = ud(data).start_document.as_mut() {
            f()
        }
    }
    unsafe extern "C" fn tr_end_document(data: *mut core::ffi::c_void) {
        if let Some(f) = ud(data).end_document.as_mut() {
            f()
        }
    }
    unsafe extern "C" fn tr_start_element(
        data: *mut core::ffi::c_void,
        name: *const c_char,
        attrs: *const *const c_char,
    ) {
        let mut pairs = Vec::new();
        let mut p = attrs;
        while !(*p).is_null() {
            let k = CStr::from_ptr(*p).to_string_lossy().into_owned();
            let v = CStr::from_ptr(*p.add(1)).to_string_lossy().into_owned();
            pairs.push((k, v));
            p = p.add(2);
        }
        if let Some(f) = ud(data).start_element.as_mut() {
            f(s(name), &pairs)
        }
    }
    unsafe extern "C" fn tr_end_element(data: *mut core::ffi::c_void, name: *const c_char) {
        if let Some(f) = ud(data).end_element.as_mut() {
            f(s(name))
        }
    }
    unsafe extern "C" fn tr_characters(
        data: *mut core::ffi::c_void,
        text: *const c_char,
        len: usize,
    ) {
        if let Some(f) = ud(data).characters.as_mut() {
            let slice = std::slice::from_raw_parts(text as *const u8, len);
            f(&String::from_utf8_lossy(slice))
        }
    }
    unsafe extern "C" fn tr_comment(data: *mut core::ffi::c_void, comment: *const c_char) {
        if let Some(f) = ud(data).comment.as_mut() {
            f(s(comment))
        }
    }
    unsafe extern "C" fn tr_cdata(data: *mut core::ffi::c_void, cdata: *const c_char) {
        if let Some(f) = ud(data).cdata.as_mut() {
            f(s(cdata))
        }
    }
    unsafe extern "C" fn tr_pi(
        data: *mut core::ffi::c_void,
        target: *const c_char,
        d: *const c_char,
    ) {
        if let Some(f) = ud(data).processing_instruction.as_mut() {
            f(s(target), s(d))
        }
    }
    unsafe extern "C" fn tr_start_ns(
        data: *mut core::ffi::c_void,
        prefix: *const c_char,
        uri: *const c_char,
    ) {
        if let Some(f) = ud(data).start_prefix_mapping.as_mut() {
            f(s(prefix), s(uri))
        }
    }
    unsafe extern "C" fn tr_end_ns(data: *mut core::ffi::c_void, prefix: *const c_char) {
        if let Some(f) = ud(data).end_prefix_mapping.as_mut() {
            f(s(prefix))
        }
    }
    unsafe extern "C" fn tr_error(
        data: *mut core::ffi::c_void,
        message: *const c_char,
        line: c_int,
        column: c_int,
    ) {
        if let Some(f) = ud(data).error.as_mut() {
            f(s(message), line, column)
        }
    }

    /// Parse `xml` with the given callbacks. Returns `Err` on a
    /// parse failure (the `error` callback, when set, also fires).
    pub fn parse(xml: &[u8], handler: &mut Handler<'_>) -> super::Result<()> {
        let c_handler = ffi::LeptrisSAXHandler {
            start_document: if handler.start_document.is_some() {
                Some(tr_start_document)
            } else {
                None
            },
            end_document: if handler.end_document.is_some() {
                Some(tr_end_document)
            } else {
                None
            },
            start_element: if handler.start_element.is_some() {
                Some(tr_start_element)
            } else {
                None
            },
            end_element: if handler.end_element.is_some() {
                Some(tr_end_element)
            } else {
                None
            },
            characters: if handler.characters.is_some() {
                Some(tr_characters)
            } else {
                None
            },
            comment: if handler.comment.is_some() {
                Some(tr_comment)
            } else {
                None
            },
            cdata: if handler.cdata.is_some() {
                Some(tr_cdata)
            } else {
                None
            },
            processing_instruction: if handler.processing_instruction.is_some() {
                Some(tr_pi)
            } else {
                None
            },
            start_prefix_mapping: if handler.start_prefix_mapping.is_some() {
                Some(tr_start_ns)
            } else {
                None
            },
            end_prefix_mapping: if handler.end_prefix_mapping.is_some() {
                Some(tr_end_ns)
            } else {
                None
            },
            error: if handler.error.is_some() {
                Some(tr_error)
            } else {
                None
            },
        };
        let rc = unsafe {
            ffi::leptris_sax_parse(
                xml.as_ptr() as *const c_char,
                xml.len(),
                &c_handler as *const ffi::LeptrisSAXHandler as *mut ffi::LeptrisSAXHandler,
                handler as *mut Handler<'_> as *mut core::ffi::c_void,
            )
        };
        if rc == 0 {
            Ok(())
        } else {
            Err(super::Error::Parse)
        }
    }
}