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
use web_sys::AbortSignal;

use super::sys;

/// Options for [`pipe_to_with_options`](super::ReadableStream::pipe_to_with_options).
#[derive(Clone, Debug, Default)]
pub struct PipeOptions {
    prevent_close: bool,
    prevent_cancel: bool,
    prevent_abort: bool,
    signal: Option<AbortSignal>,
}

impl PipeOptions {
    /// Creates a blank new set of pipe options.
    ///
    /// Equivalent to [`PipeOptions::default`](Default::default).
    pub fn new() -> Self {
        Default::default()
    }

    /// Creates a set of pipe options from a raw [`PipeOptions`](sys::PipeOptions) object.
    pub fn from_raw(raw: sys::PipeOptions) -> Self {
        Self {
            prevent_close: raw.prevent_close(),
            prevent_cancel: raw.prevent_cancel(),
            prevent_abort: raw.prevent_abort(),
            signal: raw.signal(),
        }
    }

    /// Convert this to a raw [`PipeOptions`](sys::PipeOptions) object.
    pub fn into_raw(self) -> sys::PipeOptions {
        sys::PipeOptions::new(
            self.prevent_close,
            self.prevent_cancel,
            self.prevent_abort,
            self.signal,
        )
    }

    /// Sets whether the destination writable stream should be closed
    /// when the source readable stream closes.
    pub fn prevent_close(&mut self, prevent_close: bool) -> &mut Self {
        self.prevent_close = prevent_close;
        self
    }

    /// Sets whether the source readable stream should be [canceled](https://streams.spec.whatwg.org/#cancel-a-readable-stream)
    /// when the destination writable stream errors.
    pub fn prevent_cancel(&mut self, prevent_cancel: bool) -> &mut Self {
        self.prevent_cancel = prevent_cancel;
        self
    }

    /// Sets whether the destination writable stream should be [aborted](https://streams.spec.whatwg.org/#abort-a-writable-stream)
    /// when the source readable stream errors.
    pub fn prevent_abort(&mut self, prevent_abort: bool) -> &mut Self {
        self.prevent_abort = prevent_abort;
        self
    }

    /// Sets an abort signal to abort the ongoing pipe operation.
    /// When the signal is aborted, the source readable stream will be canceled
    /// and the destination writable stream will be aborted
    /// unless the respective options [`prevent_cancel`](Self::prevent_cancel)
    /// or [`prevent_abort`](Self::prevent_abort) are set.
    pub fn signal(&mut self, signal: AbortSignal) -> &mut Self {
        self.signal = Some(signal);
        self
    }
}