[][src]Function lexpr::datum::from_reader_custom

pub fn from_reader_custom(rdr: impl Read, options: Options) -> Result<Datum>

Parse a datum from an IO stream containing a single S-expression.

The content of the IO stream is parsed directly from the stream without being buffered in memory.

When reading from a source against which short reads are not efficient, such as a File, you will want to apply your own buffering, e.g. using std::io::BufReader.

use std::error::Error;
use std::fs::File;
use std::io::BufReader;
use std::path::Path;

fn read_datum_from_file<P: AsRef<Path>>(path: P) -> Result<lexpr::Datum, Box<dyn Error>> {
    // Open the file in read-only mode with buffer.
    let file = File::open(path)?;
    let reader = BufReader::new(file);

    // Read an arbitrary S-expression, using parser options suitable for Emacs Lisp.
    let datum = lexpr::datum::from_reader_custom(reader, lexpr::parse::Options::elisp())?;

    // Return the datum.
    Ok(datum)
}

let datum = read_datum_from_file("test.el").unwrap();
println!("{:?}", datum);