lance_encoding/
version.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright The Lance Authors
3
4use std::str::FromStr;
5
6use lance_core::{Error, Result};
7use snafu::location;
8
9pub const LEGACY_FORMAT_VERSION: &str = "0.1";
10pub const V2_FORMAT_2_0: &str = "2.0";
11pub const V2_FORMAT_2_1: &str = "2.1";
12
13/// Lance file version
14#[derive(Debug, Default, PartialEq, Eq, Clone, Copy, Ord, PartialOrd)]
15pub enum LanceFileVersion {
16    // Note that Stable must come AFTER the stable version and Next must come AFTER the next version
17    // this way comparisons like x >= V2_0 will work the same if x is Stable or V2_0
18    /// The legacy (0.1) format
19    Legacy,
20    #[default]
21    V2_0,
22    /// The latest stable release
23    Stable,
24    V2_1,
25    /// The latest unstable release
26    Next,
27}
28
29impl LanceFileVersion {
30    /// Convert Stable or Next to the actual version
31    pub fn resolve(&self) -> Self {
32        match self {
33            Self::Stable => Self::V2_0,
34            Self::Next => Self::V2_1,
35            _ => *self,
36        }
37    }
38
39    pub fn try_from_major_minor(major: u32, minor: u32) -> Result<Self> {
40        match (major, minor) {
41            (0, 0) => Ok(Self::Legacy),
42            (0, 1) => Ok(Self::Legacy),
43            (0, 2) => Ok(Self::Legacy),
44            (0, 3) => Ok(Self::V2_0),
45            (2, 0) => Ok(Self::V2_0),
46            (2, 1) => Ok(Self::V2_1),
47            _ => Err(Error::InvalidInput {
48                source: format!("Unknown Lance storage version: {}.{}", major, minor).into(),
49                location: location!(),
50            }),
51        }
52    }
53
54    pub fn to_numbers(&self) -> (u32, u32) {
55        match self {
56            Self::Legacy => (0, 2),
57            Self::V2_0 => (2, 0),
58            Self::V2_1 => (2, 1),
59            Self::Stable => self.resolve().to_numbers(),
60            Self::Next => self.resolve().to_numbers(),
61        }
62    }
63}
64
65impl std::fmt::Display for LanceFileVersion {
66    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67        write!(
68            f,
69            "{}",
70            match self {
71                Self::Legacy => LEGACY_FORMAT_VERSION,
72                Self::V2_0 => V2_FORMAT_2_0,
73                Self::V2_1 => V2_FORMAT_2_1,
74                Self::Stable => "stable",
75                Self::Next => "next",
76            }
77        )
78    }
79}
80
81impl FromStr for LanceFileVersion {
82    type Err = Error;
83
84    fn from_str(value: &str) -> Result<Self> {
85        match value.to_lowercase().as_str() {
86            LEGACY_FORMAT_VERSION => Ok(Self::Legacy),
87            V2_FORMAT_2_0 => Ok(Self::V2_0),
88            V2_FORMAT_2_1 => Ok(Self::V2_1),
89            "stable" => Ok(Self::Stable),
90            "legacy" => Ok(Self::Legacy),
91            // Version 0.3 is an alias of 2.0
92            "0.3" => Ok(Self::V2_0),
93            _ => Err(Error::InvalidInput {
94                source: format!("Unknown Lance storage version: {}", value).into(),
95                location: location!(),
96            }),
97        }
98    }
99}