Skip to main content

fluss/client/write/
write_format.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::error::Error::IllegalArgument;
19use crate::error::Result;
20use crate::metadata::KvFormat;
21use std::fmt::Display;
22
23#[derive(Copy, Clone)]
24pub enum WriteFormat {
25    ArrowLog,
26    CompactedLog,
27    CompactedKv,
28}
29
30impl WriteFormat {
31    pub const fn is_log(&self) -> bool {
32        matches!(self, Self::ArrowLog | Self::CompactedLog)
33    }
34
35    pub fn is_kv(&self) -> bool {
36        !self.is_log()
37    }
38
39    pub fn to_kv_format(&self) -> Result<KvFormat> {
40        match self {
41            WriteFormat::CompactedKv => Ok(KvFormat::COMPACTED),
42            other => Err(IllegalArgument {
43                message: format!("WriteFormat `{other}` is not a KvFormat"),
44            }),
45        }
46    }
47
48    pub fn from_kv_format(kv_format: &KvFormat) -> Result<Self> {
49        match kv_format {
50            KvFormat::COMPACTED => Ok(WriteFormat::CompactedKv),
51            other => Err(IllegalArgument {
52                message: format!("Unknown KvFormat: `{other}`"),
53            }),
54        }
55    }
56}
57
58impl Display for WriteFormat {
59    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60        match self {
61            WriteFormat::ArrowLog => f.write_str("ArrowLog"),
62            WriteFormat::CompactedLog => f.write_str("CompactedLog"),
63            WriteFormat::CompactedKv => f.write_str("CompactedKv"),
64        }
65    }
66}