Skip to main content

conjure_cp_core/representation/
legacy.rs

1use std::collections::BTreeMap;
2
3use core::fmt::Debug;
4
5//TODO: write good documentation on this! ~niklasdewally
6
7use crate::{
8    ast::{DeclarationPtr, Expression, Literal, Name, SymbolTable},
9    rule_engine::ApplicationError,
10};
11
12pub trait Representation: Send + Sync + Debug {
13    /// Creates a representation object for the given name.
14    fn init(name: &Name, symtab: &SymbolTable) -> Option<Self>
15    where
16        Self: Sized;
17
18    /// The variable being represented.
19    fn variable_name(&self) -> &Name;
20
21    /// Given an assignment for `self`, creates assignments for its representation variables.
22    fn value_down(&self, value: Literal) -> Result<BTreeMap<Name, Literal>, ApplicationError>;
23
24    /// Given assignments for its representation variables, creates an assignment for `self`.
25    fn value_up(&self, values: &BTreeMap<Name, Literal>) -> Result<Literal, ApplicationError>;
26
27    /// Returns [`Expression`]s representing each representation variable.
28    fn expression_down(
29        &self,
30        symtab: &SymbolTable,
31    ) -> Result<BTreeMap<Name, Expression>, ApplicationError>;
32
33    /// Creates declarations for the representation variables of `self`.
34    fn declaration_down(&self) -> Result<Vec<DeclarationPtr>, ApplicationError>;
35
36    /// The rule name for this representaion.
37    fn repr_name(&self) -> &str;
38
39    /// The identity of this representation.
40    fn repr_id(&self) -> super::ReprId;
41
42    /// Makes a clone of `self` into a `Representation` trait object.
43    fn box_clone(&self) -> Box<dyn Representation>;
44}
45
46impl Clone for Box<dyn Representation> {
47    fn clone(&self) -> Self {
48        self.box_clone()
49    }
50}