Skip to main content

object_store/aws/
checksum.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use crate::config::Parse;
19use std::str::FromStr;
20
21#[allow(non_camel_case_types)]
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23#[non_exhaustive]
24/// Enum representing checksum algorithm supported by S3.
25pub enum Checksum {
26    /// SHA-256 algorithm.
27    SHA256,
28    /// CRC64-NVME algorithm.
29    CRC64NVME,
30}
31
32impl std::fmt::Display for Checksum {
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        match &self {
35            Self::SHA256 => write!(f, "sha256"),
36            Self::CRC64NVME => write!(f, "crc64nvme"),
37        }
38    }
39}
40
41impl FromStr for Checksum {
42    type Err = ();
43
44    fn from_str(s: &str) -> Result<Self, Self::Err> {
45        match s.to_lowercase().as_str() {
46            "sha256" => Ok(Self::SHA256),
47            "crc64nvme" => Ok(Self::CRC64NVME),
48            _ => Err(()),
49        }
50    }
51}
52
53impl TryFrom<&String> for Checksum {
54    type Error = ();
55
56    fn try_from(value: &String) -> Result<Self, Self::Error> {
57        value.parse()
58    }
59}
60
61impl Parse for Checksum {
62    fn parse(v: &str) -> crate::Result<Self> {
63        v.parse().map_err(|_| crate::Error::Generic {
64            store: "Config",
65            source: format!("\"{v}\" is not a valid checksum algorithm").into(),
66        })
67    }
68}