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/// Enum representing checksum algorithm supported by S3.
24pub enum Checksum {
25 /// SHA-256 algorithm.
26 SHA256,
27}
28
29impl std::fmt::Display for Checksum {
30 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31 match &self {
32 Self::SHA256 => write!(f, "sha256"),
33 }
34 }
35}
36
37impl FromStr for Checksum {
38 type Err = ();
39
40 fn from_str(s: &str) -> Result<Self, Self::Err> {
41 match s.to_lowercase().as_str() {
42 "sha256" => Ok(Self::SHA256),
43 _ => Err(()),
44 }
45 }
46}
47
48impl TryFrom<&String> for Checksum {
49 type Error = ();
50
51 fn try_from(value: &String) -> Result<Self, Self::Error> {
52 value.parse()
53 }
54}
55
56impl Parse for Checksum {
57 fn parse(v: &str) -> crate::Result<Self> {
58 v.parse().map_err(|_| crate::Error::Generic {
59 store: "Config",
60 source: format!("\"{v}\" is not a valid checksum algorithm").into(),
61 })
62 }
63}