Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-utils/src/io.rs
6939 views
1
use std::fs::File;
2
use std::io;
3
use std::path::Path;
4
5
use polars_error::*;
6
7
use crate::config::verbose;
8
9
pub 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} (set POLARS_VERBOSE=1 to see full path)")
14
} else {
15
format!("{err}: {path}")
16
};
17
io::Error::new(err.kind(), msg).into()
18
}
19
20
pub fn open_file(path: &Path) -> PolarsResult<File> {
21
File::open(path).map_err(|err| _limit_path_len_io_err(path, err))
22
}
23
24
pub 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
33
pub fn create_file(path: &Path) -> PolarsResult<File> {
34
File::create(path).map_err(|err| _limit_path_len_io_err(path, err))
35
}
36
37