|
| 1 | +// Copyright 2021 Datafuse Labs |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use std::path::PathBuf; |
| 16 | + |
| 17 | +use serde::Deserialize; |
| 18 | +use serde::Serialize; |
| 19 | + |
| 20 | +/// CompressAlgorithm represents all compress algorithm that OpenDAL supports. |
| 21 | +#[derive(Serialize, Deserialize, Copy, Clone, Eq, PartialEq, Debug)] |
| 22 | +pub enum CompressAlgorithm { |
| 23 | + /// [Brotli](https://github.com/google/brotli) compression format. |
| 24 | + Brotli, |
| 25 | + /// [bzip2](http://sourceware.org/bzip2/) compression format. |
| 26 | + Bz2, |
| 27 | + /// [Deflate](https://datatracker.ietf.org/doc/html/rfc1951) Compressed Data Format. |
| 28 | + /// |
| 29 | + /// Similar to [`CompressAlgorithm::Gzip`] and [`CompressAlgorithm::Zlib`] |
| 30 | + Deflate, |
| 31 | + /// [Gzip](https://datatracker.ietf.org/doc/html/rfc1952) compress format. |
| 32 | + /// |
| 33 | + /// Similar to [`CompressAlgorithm::Deflate`] and [`CompressAlgorithm::Zlib`] |
| 34 | + Gzip, |
| 35 | + /// [LZMA](https://www.7-zip.org/sdk.html) compress format. |
| 36 | + Lzma, |
| 37 | + /// [Xz](https://tukaani.org/xz/) compress format, the successor of [`CompressAlgorithm::Lzma`]. |
| 38 | + Xz, |
| 39 | + /// [Zlib](https://datatracker.ietf.org/doc/html/rfc1950) compress format. |
| 40 | + /// |
| 41 | + /// Similar to [`CompressAlgorithm::Deflate`] and [`CompressAlgorithm::Gzip`] |
| 42 | + Zlib, |
| 43 | + /// [Zstd](https://github.com/facebook/zstd) compression algorithm |
| 44 | + Zstd, |
| 45 | +} |
| 46 | + |
| 47 | +impl CompressAlgorithm { |
| 48 | + /// Get the file extension of this compress algorithm. |
| 49 | + pub fn extension(&self) -> &str { |
| 50 | + match self { |
| 51 | + CompressAlgorithm::Brotli => "br", |
| 52 | + CompressAlgorithm::Bz2 => "bz2", |
| 53 | + CompressAlgorithm::Deflate => "deflate", |
| 54 | + CompressAlgorithm::Gzip => "gz", |
| 55 | + CompressAlgorithm::Lzma => "lzma", |
| 56 | + CompressAlgorithm::Xz => "xz", |
| 57 | + CompressAlgorithm::Zlib => "zl", |
| 58 | + CompressAlgorithm::Zstd => "zstd", |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /// Create CompressAlgorithm from file extension. |
| 63 | + /// |
| 64 | + /// If the file extension is not supported, `None` will be return instead. |
| 65 | + pub fn from_extension(ext: &str) -> Option<CompressAlgorithm> { |
| 66 | + match ext { |
| 67 | + "br" => Some(CompressAlgorithm::Brotli), |
| 68 | + "bz2" => Some(CompressAlgorithm::Bz2), |
| 69 | + "deflate" => Some(CompressAlgorithm::Deflate), |
| 70 | + "gz" => Some(CompressAlgorithm::Gzip), |
| 71 | + "lzma" => Some(CompressAlgorithm::Lzma), |
| 72 | + "xz" => Some(CompressAlgorithm::Xz), |
| 73 | + "zl" => Some(CompressAlgorithm::Zlib), |
| 74 | + "zstd" | "zst" => Some(CompressAlgorithm::Zstd), |
| 75 | + _ => None, |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /// Create CompressAlgorithm from file path. |
| 80 | + /// |
| 81 | + /// If the extension in file path is not supported, `None` will be return instead. |
| 82 | + pub fn from_path(path: &str) -> Option<CompressAlgorithm> { |
| 83 | + let ext = PathBuf::from(path) |
| 84 | + .extension() |
| 85 | + .map(|s| s.to_string_lossy())? |
| 86 | + .to_string(); |
| 87 | + |
| 88 | + CompressAlgorithm::from_extension(&ext) |
| 89 | + } |
| 90 | +} |
0 commit comments