| 1 | // Copyright (c) 2021 Harry [Majored] [[email protected]] |
| 2 | // MIT License (https://github.com/Majored/rs-async-zip/blob/main/LICENSE) |
| 3 | |
| 4 | use crate::base::write::io::offset::AsyncOffsetWriter; |
| 5 | use crate::spec::Compression; |
| 6 | |
| 7 | use std::io::Error; |
| 8 | use std::pin::Pin; |
| 9 | use std::task::{Context, Poll}; |
| 10 | |
| 11 | #[cfg(any(feature = "deflate", feature = "bzip2", feature = "zstd", feature = "lzma", feature = "xz"))] |
| 12 | use async_compression::futures::write; |
| 13 | use futures_lite::io::AsyncWrite; |
| 14 | |
| 15 | pub enum CompressedAsyncWriter<'b, W: AsyncWrite + Unpin> { |
| 16 | Stored(ShutdownIgnoredWriter<&'b mut AsyncOffsetWriter<W>>), |
| 17 | #[cfg(feature = "deflate")] |
| 18 | Deflate(write::DeflateEncoder<ShutdownIgnoredWriter<&'b mut AsyncOffsetWriter<W>>>), |
| 19 | #[cfg(feature = "bzip2")] |
| 20 | Bz(write::BzEncoder<ShutdownIgnoredWriter<&'b mut AsyncOffsetWriter<W>>>), |
| 21 | #[cfg(feature = "lzma")] |
| 22 | Lzma(write::LzmaEncoder<ShutdownIgnoredWriter<&'b mut AsyncOffsetWriter<W>>>), |
| 23 | #[cfg(feature = "zstd")] |
| 24 | Zstd(write::ZstdEncoder<ShutdownIgnoredWriter<&'b mut AsyncOffsetWriter<W>>>), |
| 25 | #[cfg(feature = "xz")] |
| 26 | Xz(write::XzEncoder<ShutdownIgnoredWriter<&'b mut AsyncOffsetWriter<W>>>), |
| 27 | } |
| 28 | |
| 29 | impl<'b, W: AsyncWrite + Unpin> CompressedAsyncWriter<'b, W> { |
| 30 | pub fn from_raw(writer: &'b mut AsyncOffsetWriter<W>, compression: Compression, precompressed: bool) -> Self { |
| 31 | if precompressed { |
| 32 | return CompressedAsyncWriter::Stored(ShutdownIgnoredWriter(writer)); |
| 33 | } |
| 34 | |
| 35 | match compression { |
| 36 | Compression::Stored => CompressedAsyncWriter::Stored(ShutdownIgnoredWriter(writer)), |
| 37 | #[cfg(feature = "deflate")] |
| 38 | Compression::Deflate => { |
| 39 | CompressedAsyncWriter::Deflate(write::DeflateEncoder::new(ShutdownIgnoredWriter(writer))) |
| 40 | } |
| 41 | #[cfg(feature = "deflate64")] |
| 42 | Compression::Deflate64 => panic!("writing deflate64 is not supported"), |
| 43 | #[cfg(feature = "bzip2")] |
| 44 | Compression::Bz => CompressedAsyncWriter::Bz(write::BzEncoder::new(ShutdownIgnoredWriter(writer))), |
| 45 | #[cfg(feature = "lzma")] |
| 46 | Compression::Lzma => CompressedAsyncWriter::Lzma(write::LzmaEncoder::new(ShutdownIgnoredWriter(writer))), |
| 47 | #[cfg(feature = "zstd")] |
| 48 | Compression::Zstd => CompressedAsyncWriter::Zstd(write::ZstdEncoder::new(ShutdownIgnoredWriter(writer))), |
| 49 | #[cfg(feature = "xz")] |
| 50 | Compression::Xz => CompressedAsyncWriter::Xz(write::XzEncoder::new(ShutdownIgnoredWriter(writer))), |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | pub fn into_inner(self) -> &'b mut AsyncOffsetWriter<W> { |
| 55 | match self { |
| 56 | CompressedAsyncWriter::Stored(inner) => inner.into_inner(), |
| 57 | #[cfg(feature = "deflate")] |
| 58 | CompressedAsyncWriter::Deflate(inner) => inner.into_inner().into_inner(), |
| 59 | #[cfg(feature = "bzip2")] |
| 60 | CompressedAsyncWriter::Bz(inner) => inner.into_inner().into_inner(), |
| 61 | #[cfg(feature = "lzma")] |
| 62 | CompressedAsyncWriter::Lzma(inner) => inner.into_inner().into_inner(), |
| 63 | #[cfg(feature = "zstd")] |
| 64 | CompressedAsyncWriter::Zstd(inner) => inner.into_inner().into_inner(), |
| 65 | #[cfg(feature = "xz")] |
| 66 | CompressedAsyncWriter::Xz(inner) => inner.into_inner().into_inner(), |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | impl<'b, W: AsyncWrite + Unpin> AsyncWrite for CompressedAsyncWriter<'b, W> { |
| 72 | fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll<std::result::Result<usize, Error>> { |
| 73 | match *self { |
| 74 | CompressedAsyncWriter::Stored(ref mut inner) => Pin::new(inner).poll_write(cx, buf), |
| 75 | #[cfg(feature = "deflate")] |
| 76 | CompressedAsyncWriter::Deflate(ref mut inner) => Pin::new(inner).poll_write(cx, buf), |
| 77 | #[cfg(feature = "bzip2")] |
| 78 | CompressedAsyncWriter::Bz(ref mut inner) => Pin::new(inner).poll_write(cx, buf), |
| 79 | #[cfg(feature = "lzma")] |
| 80 | CompressedAsyncWriter::Lzma(ref mut inner) => Pin::new(inner).poll_write(cx, buf), |
| 81 | #[cfg(feature = "zstd")] |
| 82 | CompressedAsyncWriter::Zstd(ref mut inner) => Pin::new(inner).poll_write(cx, buf), |
| 83 | #[cfg(feature = "xz")] |
| 84 | CompressedAsyncWriter::Xz(ref mut inner) => Pin::new(inner).poll_write(cx, buf), |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<std::result::Result<(), Error>> { |
| 89 | match *self { |
| 90 | CompressedAsyncWriter::Stored(ref mut inner) => Pin::new(inner).poll_flush(cx), |
| 91 | #[cfg(feature = "deflate")] |
| 92 | CompressedAsyncWriter::Deflate(ref mut inner) => Pin::new(inner).poll_flush(cx), |
| 93 | #[cfg(feature = "bzip2")] |
| 94 | CompressedAsyncWriter::Bz(ref mut inner) => Pin::new(inner).poll_flush(cx), |
| 95 | #[cfg(feature = "lzma")] |
| 96 | CompressedAsyncWriter::Lzma(ref mut inner) => Pin::new(inner).poll_flush(cx), |
| 97 | #[cfg(feature = "zstd")] |
| 98 | CompressedAsyncWriter::Zstd(ref mut inner) => Pin::new(inner).poll_flush(cx), |
| 99 | #[cfg(feature = "xz")] |
| 100 | CompressedAsyncWriter::Xz(ref mut inner) => Pin::new(inner).poll_flush(cx), |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<std::result::Result<(), Error>> { |
| 105 | match *self { |
| 106 | CompressedAsyncWriter::Stored(ref mut inner) => Pin::new(inner).poll_close(cx), |
| 107 | #[cfg(feature = "deflate")] |
| 108 | CompressedAsyncWriter::Deflate(ref mut inner) => Pin::new(inner).poll_close(cx), |
| 109 | #[cfg(feature = "bzip2")] |
| 110 | CompressedAsyncWriter::Bz(ref mut inner) => Pin::new(inner).poll_close(cx), |
| 111 | #[cfg(feature = "lzma")] |
| 112 | CompressedAsyncWriter::Lzma(ref mut inner) => Pin::new(inner).poll_close(cx), |
| 113 | #[cfg(feature = "zstd")] |
| 114 | CompressedAsyncWriter::Zstd(ref mut inner) => Pin::new(inner).poll_close(cx), |
| 115 | #[cfg(feature = "xz")] |
| 116 | CompressedAsyncWriter::Xz(ref mut inner) => Pin::new(inner).poll_close(cx), |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | pub struct ShutdownIgnoredWriter<W: AsyncWrite + Unpin>(W); |
| 122 | |
| 123 | impl<W: AsyncWrite + Unpin> ShutdownIgnoredWriter<W> { |
| 124 | pub fn into_inner(self) -> W { |
| 125 | self.0 |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | impl<W: AsyncWrite + Unpin> AsyncWrite for ShutdownIgnoredWriter<W> { |
| 130 | fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll<std::result::Result<usize, Error>> { |
| 131 | Pin::new(&mut self.0).poll_write(cx, buf) |
| 132 | } |
| 133 | |
| 134 | fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<std::result::Result<(), Error>> { |
| 135 | Pin::new(&mut self.0).poll_flush(cx) |
| 136 | } |
| 137 | |
| 138 | fn poll_close(self: Pin<&mut Self>, _: &mut Context) -> Poll<std::result::Result<(), Error>> { |
| 139 | Poll::Ready(Ok(())) |
| 140 | } |
| 141 | } |