strict_types/typesys/
symbols.rs

1// Strict encoding schema library, implementing validation and parsing of strict encoded data
2// against a schema.
3//
4// SPDX-License-Identifier: Apache-2.0
5//
6// Designed in 2019-2025 by Dr Maxim Orlovsky <orlovsky@ubideco.org>
7// Written in 2024-2025 by Dr Maxim Orlovsky <orlovsky@ubideco.org>
8//
9// Copyright (C) 2022-2025 Laboratories for Ubiquitous Deterministic Computing (UBIDECO),
10//                         Institute for Distributed and Cognitive Systems (InDCS), Switzerland.
11// Copyright (C) 2022-2025 Dr Maxim Orlovsky.
12// All rights under the above copyrights are reserved.
13//
14// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
15// in compliance with the License. You may obtain a copy of the License at
16//
17//        http://www.apache.org/licenses/LICENSE-2.0
18//
19// Unless required by applicable law or agreed to in writing, software distributed under the License
20// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
21// or implied. See the License for the specific language governing permissions and limitations under
22// the License.
23
24use std::collections::BTreeMap;
25use std::fmt::{self, Display, Formatter};
26use std::ops::Index;
27
28use amplify::confinement::{self, MediumOrdSet, SmallOrdSet};
29use encoding::{StrictDeserialize, StrictSerialize, STRICT_TYPES_LIB};
30
31use crate::typesys::{translate, SymTy, TypeFqn, TypeSymbol, TypeSysId, TypeTree};
32use crate::typify::TypeSpec;
33use crate::{Dependency, SemId, Translate, Ty, TypeSystem};
34
35#[derive(Clone, Eq, PartialEq, Debug)]
36#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
37#[strict_type(lib = STRICT_TYPES_LIB)]
38#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
39pub struct Symbols {
40    libs: SmallOrdSet<Dependency>,
41    symbols: MediumOrdSet<TypeSymbol>,
42}
43
44impl StrictSerialize for Symbols {}
45impl StrictDeserialize for Symbols {}
46
47impl Symbols {
48    pub(crate) fn with(
49        libs: impl IntoIterator<Item = Dependency>,
50    ) -> Result<Self, confinement::Error> {
51        Ok(Self {
52            libs: SmallOrdSet::try_from_iter(libs)?,
53            symbols: empty!(),
54        })
55    }
56
57    pub(crate) fn update_unchecked(
58        &mut self,
59        sem_id: SemId,
60        orig: Option<TypeFqn>,
61    ) -> Result<(), translate::Error> {
62        let sym = TypeSymbol {
63            id: sem_id,
64            fqn: orig,
65        };
66        if let Some(present) = self.symbols.get(&sym) {
67            return Err(translate::Error::RepeatedType {
68                new: sym,
69                present: present.clone(),
70            });
71        }
72        self.symbols.push(sym)?;
73        Ok(())
74    }
75
76    pub fn get(&self, spec: impl Into<TypeFqn>) -> Option<&SemId> {
77        let needle = spec.into();
78        self.symbols.iter().find(|fqid| fqid.fqn.as_ref() == Some(&needle)).map(|fqid| &fqid.id)
79    }
80
81    pub fn lookup(&self, sem_id: SemId) -> Option<&TypeFqn> {
82        self.symbols.iter().find(|sym| sym.id == sem_id).and_then(|sym| sym.fqn.as_ref())
83    }
84}
85
86impl Index<&'static str> for Symbols {
87    type Output = SemId;
88
89    fn index(&self, index: &'static str) -> &Self::Output {
90        self.get(index).unwrap_or_else(|| panic!("type {index} is absent in the type system"))
91    }
92}
93
94impl Display for Symbols {
95    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
96        writeln!(f)?;
97        for dep in &self.libs {
98            writeln!(f, "{dep} as {}", dep.name)?;
99        }
100        writeln!(f, "{{--")?;
101        for sym in &self.symbols {
102            if let Some(fqn) = &sym.fqn {
103                writeln!(f, "  {} => {}", fqn, sym.id)?;
104            }
105        }
106        writeln!(f, "--}}")
107    }
108}
109
110#[derive(Getters, Clone, Eq, PartialEq, Debug)]
111#[getter(prefix = "as_")]
112#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
113#[strict_type(lib = STRICT_TYPES_LIB)]
114#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
115pub struct SymbolicSys {
116    pub(super) symbols: Symbols,
117    types: TypeSystem,
118}
119
120impl StrictSerialize for SymbolicSys {}
121impl StrictDeserialize for SymbolicSys {}
122
123impl SymbolicSys {
124    pub(crate) fn with(
125        libs: impl IntoIterator<Item = Dependency>,
126        types: BTreeMap<SemId, SymTy>,
127    ) -> Result<Self, translate::Error> {
128        let mut sys = TypeSystem::new();
129        let mut sym = Symbols::with(libs)?;
130
131        for (sem_id, info) in types {
132            sys.insert_unchecked(sem_id, info.ty)?;
133            sym.update_unchecked(sem_id, info.orig)?;
134        }
135
136        Ok(Self {
137            symbols: sym,
138            types: sys,
139        })
140    }
141
142    pub fn new(types: TypeSystem, symbols: Symbols) -> Self { Self { symbols, types } }
143
144    pub fn id(&self) -> TypeSysId { self.types.id() }
145
146    pub fn get(&self, spec: impl Into<TypeSpec>) -> Option<&Ty<SemId>> {
147        let sem_id = self.to_sem_id(spec)?;
148        self.types.get(sem_id)
149    }
150
151    pub fn type_tree(&self, spec: impl Into<TypeSpec>) -> Option<TypeTree<'_>> {
152        let sem_id = self.to_sem_id(spec)?;
153        let _ = self.types.get(sem_id)?;
154        Some(TypeTree::new(sem_id, self))
155    }
156
157    pub fn resolve(&self, fqn: impl Into<TypeFqn>) -> Option<&SemId> { self.symbols.get(fqn) }
158
159    pub fn lookup(&self, sem_id: SemId) -> Option<&TypeFqn> { self.symbols.lookup(sem_id) }
160
161    pub fn to_sem_id(&self, spec: impl Into<TypeSpec>) -> Option<SemId> {
162        match spec.into() {
163            TypeSpec::SemId(sem_id) => Some(sem_id),
164            TypeSpec::Fqn(fqn) => self.resolve(fqn).copied(),
165        }
166    }
167
168    pub fn into_type_system(self) -> TypeSystem { self.types }
169}
170
171impl Display for SymbolicSys {
172    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
173        writeln!(f, "typesys -- {:+}", self.id())?;
174        writeln!(f)?;
175        for (id, ty) in self.types.as_unconfined() {
176            let ty = ty.clone().translate(&mut (), self).expect("type system inconsistency");
177            match self.lookup(*id) {
178                Some(fqn) => {
179                    writeln!(f, "-- {id:-}")?;
180                    writeln!(f, "data {fqn}: {ty:-}")?;
181                }
182                None => writeln!(f, "data {id:-}: {ty:-}")?,
183            }
184        }
185        Ok(())
186    }
187}
188
189#[cfg(feature = "armor")]
190impl armor::StrictArmor for SymbolicSys {
191    type Id = TypeSysId;
192    const PLATE_TITLE: &'static str = "STRICT SYMBOLIC TYPES";
193
194    fn armor_id(&self) -> Self::Id { self.id() }
195}