polars_utils/
io.rs

1use std::fs::File;
2use std::io;
3use std::path::Path;
4
5use polars_error::*;
6
7use crate::config::verbose;
8
9pub fn _limit_path_len_io_err(path: &Path, err: io::Error) -> PolarsError {
10    let path = path.to_string_lossy();
11    let msg = if path.len() > 88 && !verbose() {
12        let truncated_path: String = path.chars().skip(path.len() - 88).collect();
13        format!("{err}: ...{truncated_path}")
14    } else {
15        format!("{err}: {path}")
16    };
17    io::Error::new(err.kind(), msg).into()
18}
19
20pub fn open_file(path: &Path) -> PolarsResult<File> {
21    File::open(path).map_err(|err| _limit_path_len_io_err(path, err))
22}
23
24pub fn open_file_write(path: &Path) -> PolarsResult<File> {
25    std::fs::OpenOptions::new()
26        .write(true)
27        .create(true)
28        .truncate(true)
29        .open(path)
30        .map_err(|err| _limit_path_len_io_err(path, err))
31}
32
33pub fn create_file(path: &Path) -> PolarsResult<File> {
34    File::create(path).map_err(|err| _limit_path_len_io_err(path, err))
35}