aluvm/library/armor.rs
1// Reference rust implementation of AluVM (arithmetic logic unit virtual machine).
2// To find more on AluVM please check <https://aluvm.org>
3//
4// SPDX-License-Identifier: Apache-2.0
5//
6// Designed in 2021-2025 by Dr Maxim Orlovsky <orlovsky@ubideco.org>
7// Written in 2021-2025 by Dr Maxim Orlovsky <orlovsky@ubideco.org>
8//
9// Copyright (C) 2021-2024 LNP/BP Standards Association, Switzerland.
10// Copyright (C) 2024-2025 Laboratories for Ubiquitous Deterministic Computing (UBIDECO),
11// Institute for Distributed and Cognitive Systems (InDCS), Switzerland.
12// Copyright (C) 2021-2025 Dr Maxim Orlovsky.
13// All rights under the above copyrights are reserved.
14//
15// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
16// in compliance with the License. You may obtain a copy of the License at
17//
18// http://www.apache.org/licenses/LICENSE-2.0
19//
20// Unless required by applicable law or agreed to in writing, software distributed under the License
21// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
22// or implied. See the License for the specific language governing permissions and limitations under
23// the License.
24
25use ::armor::{ArmorHeader, ArmorParseError, AsciiArmor, ASCII_ARMOR_ID};
26use amplify::confinement::{self, Confined, U24 as U24MAX};
27use strict_encoding::{DeserializeError, StrictDeserialize, StrictSerialize};
28
29use super::*;
30
31const ASCII_ARMOR_ISAE: &str = "ISA-Extensions";
32const ASCII_ARMOR_DEPENDENCY: &str = "Dependency";
33
34/// Errors while deserializing lib-old from an ASCII Armor.
35#[derive(Clone, Eq, PartialEq, Debug, Display, Error, From)]
36#[display(inner)]
37pub enum LibArmorError {
38 /// Armor parse error.
39 #[from]
40 Armor(ArmorParseError),
41
42 /// The provided data exceed maximum possible lib-old size.
43 #[from(confinement::Error)]
44 TooLarge,
45
46 /// Library data deserialization error.
47 #[from]
48 Decode(DeserializeError),
49}
50
51impl AsciiArmor for Lib {
52 type Err = LibArmorError;
53 const PLATE_TITLE: &'static str = "ALUVM LIB";
54
55 fn ascii_armored_headers(&self) -> Vec<ArmorHeader> {
56 let mut headers = vec![
57 ArmorHeader::new(ASCII_ARMOR_ID, self.lib_id().to_string()),
58 ArmorHeader::new(ASCII_ARMOR_ISAE, self.isae_string()),
59 ];
60 for dep in &self.libs {
61 headers.push(ArmorHeader::new(ASCII_ARMOR_DEPENDENCY, dep.to_string()));
62 }
63 headers
64 }
65
66 fn to_ascii_armored_data(&self) -> Vec<u8> {
67 self.to_strict_serialized::<U24MAX>()
68 .expect("type guarantees")
69 .to_vec()
70 }
71
72 fn with_headers_data(_headers: Vec<ArmorHeader>, data: Vec<u8>) -> Result<Self, Self::Err> {
73 // TODO: check id, dependencies and ISAE
74 let data = Confined::try_from(data)?;
75 let me = Self::from_strict_serialized::<U24MAX>(data)?;
76 Ok(me)
77 }
78}