Sha256: 4f7ab2bd68c68dbad3b3f7ba0640edb29b5654a13bdaf5aa34cca3ca9eb9601a

Contents?: true

Size: 1 KB

Versions: 5

Compression:

Stored size: 1 KB

Contents

use magnus::{exception, Error, RString, Value};
use polars::io::mmap::MmapBytesReader;
use std::fs::File;
use std::io::Cursor;
use std::path::PathBuf;

use crate::RbResult;

pub fn get_file_like(f: Value, truncate: bool) -> RbResult<File> {
    let str_slice = f.try_convert::<PathBuf>()?;
    let f = if truncate {
        File::create(str_slice)
            .map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?
    } else {
        File::open(str_slice).map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?
    };
    Ok(f)
}

pub fn get_mmap_bytes_reader(rb_f: Value) -> RbResult<Box<dyn MmapBytesReader>> {
    if let Ok(bytes) = rb_f.funcall::<_, _, RString>("read", ()) {
        let bytes = unsafe { bytes.as_slice() };
        // TODO avoid copy
        Ok(Box::new(Cursor::new(bytes.to_vec())))
    } else {
        let p = rb_f.try_convert::<PathBuf>()?;
        let f = File::open(p).map_err(|e| Error::new(exception::runtime_error(), e.to_string()))?;
        Ok(Box::new(f))
    }
}

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
polars-df-0.6.0 ext/polars/src/file.rs
polars-df-0.5.0 ext/polars/src/file.rs
polars-df-0.4.0 ext/polars/src/file.rs
polars-df-0.3.1 ext/polars/src/file.rs
polars-df-0.3.0 ext/polars/src/file.rs