Skip to main content

amaru_kernel/cardano/
required_script.rs

1// Copyright 2025 PRAGMA
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::cmp::Ordering;
16
17use crate::{AsIndex, Hash, MemoizedDatum, RedeemerKey, ScriptPurpose, size::SCRIPT};
18
19#[derive(Clone, Eq, PartialEq, Debug, serde::Deserialize)]
20pub struct RequiredScript {
21    pub hash: Hash<SCRIPT>,
22    pub index: u32,
23    pub purpose: ScriptPurpose,
24    pub datum: MemoizedDatum,
25}
26
27impl PartialOrd for RequiredScript {
28    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
29        Some(self.cmp(other))
30    }
31}
32
33impl From<&RequiredScript> for Hash<SCRIPT> {
34    fn from(value: &RequiredScript) -> Self {
35        value.hash
36    }
37}
38
39impl Ord for RequiredScript {
40    fn cmp(&self, other: &Self) -> Ordering {
41        match self.hash.cmp(&other.hash) {
42            Ordering::Equal => match self.purpose.as_index().cmp(&other.purpose.as_index()) {
43                Ordering::Equal => self.index.cmp(&other.index),
44                by_purpose @ Ordering::Less | by_purpose @ Ordering::Greater => by_purpose,
45            },
46            by_hash @ Ordering::Less | by_hash @ Ordering::Greater => by_hash,
47        }
48    }
49}
50
51impl From<&RequiredScript> for RedeemerKey {
52    fn from(value: &RequiredScript) -> Self {
53        RedeemerKey { tag: value.purpose, index: value.index }
54    }
55}