tensorlogic_adapters/
domain.rs1use serde::{Deserialize, Serialize};
4
5use crate::metadata::Metadata;
6use crate::parametric::ParametricType;
7
8#[derive(Clone, Debug, Serialize, Deserialize)]
10pub struct DomainInfo {
11 pub name: String,
12 pub cardinality: usize,
13 pub elements: Option<Vec<String>>,
14 pub description: Option<String>,
15 pub metadata: Option<Metadata>,
17 pub parametric_type: Option<ParametricType>,
19}
20
21impl DomainInfo {
22 pub fn new(name: impl Into<String>, cardinality: usize) -> Self {
23 DomainInfo {
24 name: name.into(),
25 cardinality,
26 elements: None,
27 description: None,
28 metadata: None,
29 parametric_type: None,
30 }
31 }
32
33 pub fn with_elements(name: impl Into<String>, elements: Vec<String>) -> Self {
34 let cardinality = elements.len();
35 DomainInfo {
36 name: name.into(),
37 cardinality,
38 elements: Some(elements),
39 description: None,
40 metadata: None,
41 parametric_type: None,
42 }
43 }
44
45 pub fn with_description(mut self, desc: impl Into<String>) -> Self {
46 self.description = Some(desc.into());
47 self
48 }
49
50 pub fn with_metadata(mut self, metadata: Metadata) -> Self {
51 self.metadata = Some(metadata);
52 self
53 }
54
55 pub fn with_parametric_type(mut self, ptype: ParametricType) -> Self {
56 self.parametric_type = Some(ptype);
57 self
58 }
59
60 pub fn has_element(&self, element: &str) -> bool {
61 self.elements
62 .as_ref()
63 .map(|elems| elems.contains(&element.to_string()))
64 .unwrap_or(false)
65 }
66
67 pub fn get_index(&self, element: &str) -> Option<usize> {
68 self.elements
69 .as_ref()
70 .and_then(|elems| elems.iter().position(|e| e == element))
71 }
72}