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
use crate::error::Error;
use crate::taskdef::{FromTaskArg, IntoTaskArg, TaskArg};
use std::fs;
use std::io::{self, Read, Seek, Write};
use std::path::{Path, PathBuf};
pub struct Blob(PathBuf);
pub struct Output(pub(crate) PathBuf);
impl Blob {
pub fn from_output(output: Output) -> Self {
Blob(output.0)
}
pub fn open(&self) -> io::Result<impl Read + Seek> {
let path = em_canonicalize(&self.0)?;
fs::OpenOptions::new().read(true).open(&path)
}
}
impl Output {
pub fn open(&self) -> io::Result<impl Write + Seek> {
let path = em_canonicalize(&self.0)?;
fs::OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(&path)
}
#[inline]
pub fn into_blob(self) -> Blob {
Blob::from_output(self)
}
pub fn file(self, path: &Path) -> Result<Blob, Error> {
let mut inf = fs::OpenOptions::new().read(true).open(path)?;
let mut outf = self.open()?;
io::copy(&mut inf, &mut outf)?;
Ok(Blob::from_output(self))
}
pub fn bytes(self, data: impl AsRef<[u8]>) -> Result<Blob, Error> {
let data = data.as_ref();
self.open()?.write_all(data)?;
Ok(Blob::from_output(self))
}
}
impl IntoTaskArg for Blob {
fn into_arg(self, base: &Path) -> Result<TaskArg, Error> {
let cpath = em_canonicalize(&self.0)?;
let path = cpath.strip_prefix(base)?;
path.to_str()
.ok_or_else(|| Error::invalid_path(&self.0))
.map(|v| TaskArg::Blob(v.replace('\\', "/")))
}
}
#[cfg(target_arch = "wasm32")]
fn em_canonicalize(path: &Path) -> io::Result<PathBuf> {
use std::path::Component;
let mut out_path = PathBuf::new();
for c in path.components() {
match c {
Component::ParentDir => {
let _ = out_path.pop();
}
Component::Normal(v) => out_path.push(v),
Component::RootDir => out_path.push("/"),
_ => (),
}
}
Ok(out_path)
}
#[cfg(not(target_arch = "wasm32"))]
fn em_canonicalize(path: &Path) -> io::Result<PathBuf> {
path.canonicalize()
}
impl FromTaskArg for Blob {
fn from_arg(arg: TaskArg, base: &Path) -> Result<Self, Error> {
match arg {
TaskArg::Blob(path) => Ok(Blob(base.join(path))),
_ => Err(Error::BlobExpected),
}
}
}
impl IntoTaskArg for Output {
fn into_arg(self, base: &Path) -> Result<TaskArg, Error> {
let path = self.0.strip_prefix(base)?;
path.to_str()
.ok_or_else(|| Error::invalid_path(&self.0))
.map(|v| TaskArg::Output(v.into()))
}
}
impl FromTaskArg for Output {
fn from_arg(arg: TaskArg, base: &Path) -> Result<Self, Error> {
Ok(match arg {
TaskArg::Output(path) => Output(PathBuf::from(&base.join(path))),
_ => return Err(Error::OutputExpected),
})
}
}