cardano_sdk/cardano/
plutus_version.rs

1//  This Source Code Form is subject to the terms of the Mozilla Public
2//  License, v. 2.0. If a copy of the MPL was not distributed with this
3//  file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5use crate::cbor;
6use anyhow::anyhow;
7use std::fmt;
8
9/// The version of a Plutus program, defining available semantic and builtins.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, cbor::Encode, cbor::Decode)]
11#[cbor(index_only)]
12pub enum PlutusVersion {
13    #[n(0)]
14    V1,
15    #[n(1)]
16    V2,
17    #[n(2)]
18    V3,
19}
20
21impl fmt::Display for PlutusVersion {
22    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23        write!(f, "v{}", u8::from(*self))
24    }
25}
26
27// ----------------------------------------------------------- Converting (from)
28
29impl TryFrom<u8> for PlutusVersion {
30    type Error = anyhow::Error;
31
32    fn try_from(version: u8) -> anyhow::Result<Self> {
33        match version {
34            1 => Ok(PlutusVersion::V1),
35            2 => Ok(PlutusVersion::V2),
36            3 => Ok(PlutusVersion::V3),
37            _ => Err(anyhow!(
38                "unknown plutus version version={version}; only 1, 2 and 3 are known"
39            )),
40        }
41    }
42}
43
44// ------------------------------------------------------------- Converting (to)
45
46impl From<PlutusVersion> for u8 {
47    fn from(version: PlutusVersion) -> Self {
48        match version {
49            PlutusVersion::V1 => 1,
50            PlutusVersion::V2 => 2,
51            PlutusVersion::V3 => 3,
52        }
53    }
54}