Skip to main content

components_rs/components/
types.rs

1use serde::{Deserialize, Serialize};
2
3pub const PREFIX_OO: &str =
4    "https://linkedsoftwaredependencies.org/vocabularies/object-oriented#";
5pub const PREFIX_OM: &str =
6    "https://linkedsoftwaredependencies.org/vocabularies/object-mapping#";
7pub const PREFIX_RDF: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
8pub const PREFIX_RDFS: &str = "http://www.w3.org/2000/01/rdf-schema#";
9pub const PREFIX_XSD: &str = "http://www.w3.org/2001/XMLSchema#";
10pub const PREFIX_DOAP: &str = "http://usefulinc.com/ns/doap#";
11pub const PREFIX_OWL: &str = "http://www.w3.org/2002/07/owl#";
12
13// Well-known OO IRIs
14pub const IRI_MODULE: &str =
15    "https://linkedsoftwaredependencies.org/vocabularies/object-oriented#Module";
16pub const IRI_CLASS: &str =
17    "https://linkedsoftwaredependencies.org/vocabularies/object-oriented#Class";
18pub const IRI_ABSTRACT_CLASS: &str =
19    "https://linkedsoftwaredependencies.org/vocabularies/object-oriented#AbstractClass";
20pub const IRI_COMPONENT_INSTANCE: &str =
21    "https://linkedsoftwaredependencies.org/vocabularies/object-oriented#ComponentInstance";
22pub const IRI_COMPONENT: &str =
23    "https://linkedsoftwaredependencies.org/vocabularies/object-oriented#component";
24pub const IRI_COMPONENT_PATH: &str =
25    "https://linkedsoftwaredependencies.org/vocabularies/object-oriented#componentPath";
26pub const IRI_PARAMETER: &str =
27    "https://linkedsoftwaredependencies.org/vocabularies/object-oriented#parameter";
28pub const IRI_CONSTRUCTOR_ARGUMENTS: &str =
29    "https://linkedsoftwaredependencies.org/vocabularies/object-oriented#constructorArguments";
30
31pub const IRI_DOAP_NAME: &str = "http://usefulinc.com/ns/doap#name";
32pub const IRI_RDFS_SUBCLASS_OF: &str = "http://www.w3.org/2000/01/rdf-schema#subClassOf";
33pub const IRI_RDFS_SEE_ALSO: &str = "http://www.w3.org/2000/01/rdf-schema#seeAlso";
34pub const IRI_RDFS_RANGE: &str = "http://www.w3.org/2000/01/rdf-schema#range";
35pub const IRI_RDFS_COMMENT: &str = "http://www.w3.org/2000/01/rdf-schema#comment";
36
37/// The type of a CJS component.
38#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
39pub enum ComponentType {
40    Class,
41    AbstractClass,
42    Instance,
43}
44
45impl ComponentType {
46    pub fn from_type_iris(types: &[String]) -> Option<Self> {
47        for t in types {
48            match t.as_str() {
49                IRI_CLASS => return Some(ComponentType::Class),
50                IRI_ABSTRACT_CLASS => return Some(ComponentType::AbstractClass),
51                IRI_COMPONENT_INSTANCE => return Some(ComponentType::Instance),
52                _ => {}
53            }
54        }
55        None
56    }
57}
58
59impl std::fmt::Display for ComponentType {
60    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61        match self {
62            ComponentType::Class => write!(f, "Class"),
63            ComponentType::AbstractClass => write!(f, "AbstractClass"),
64            ComponentType::Instance => write!(f, "Instance"),
65        }
66    }
67}
68
69/// A CJS module containing components.
70#[derive(Debug, Clone, Serialize, Deserialize)]
71pub struct CjsModule {
72    pub iri: String,
73    pub require_name: Option<String>,
74    pub components: Vec<CjsComponent>,
75    pub source_file: String,
76}
77
78/// A CJS component (class, abstract class, or instance).
79#[derive(Debug, Clone, Serialize, Deserialize)]
80pub struct CjsComponent {
81    pub iri: String,
82    pub component_type: ComponentType,
83    pub require_element: Option<String>,
84    pub comment: Option<String>,
85    pub parameters: Vec<CjsParameter>,
86    pub extends: Vec<String>,
87    pub constructor_arguments: Option<serde_json::Value>,
88    pub module_iri: Option<String>,
89}
90
91/// A parameter on a CJS component.
92#[derive(Debug, Clone, Serialize, Deserialize)]
93pub struct CjsParameter {
94    pub iri: String,
95    pub range: Option<String>,
96    pub comment: Option<String>,
97    pub required: bool,
98    pub lazy: bool,
99    pub unique: bool,
100    pub default_value: Option<serde_json::Value>,
101}
102
103/// A configuration instance (an instantiation of a component with concrete values).
104#[derive(Debug, Clone, Serialize, Deserialize)]
105pub struct ConfigInstance {
106    pub iri: String,
107    pub component_type_iri: String,
108    pub parameters: std::collections::HashMap<String, serde_json::Value>,
109    pub source_file: String,
110}