components-rs 0.1.1

Static analysis tooling for Components.js dependency injection projects
Documentation
use serde::{Deserialize, Serialize};

pub const PREFIX_OO: &str =
    "https://linkedsoftwaredependencies.org/vocabularies/object-oriented#";
pub const PREFIX_OM: &str =
    "https://linkedsoftwaredependencies.org/vocabularies/object-mapping#";
pub const PREFIX_RDF: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
pub const PREFIX_RDFS: &str = "http://www.w3.org/2000/01/rdf-schema#";
pub const PREFIX_XSD: &str = "http://www.w3.org/2001/XMLSchema#";
pub const PREFIX_DOAP: &str = "http://usefulinc.com/ns/doap#";
pub const PREFIX_OWL: &str = "http://www.w3.org/2002/07/owl#";

// Well-known OO IRIs
pub const IRI_MODULE: &str =
    "https://linkedsoftwaredependencies.org/vocabularies/object-oriented#Module";
pub const IRI_CLASS: &str =
    "https://linkedsoftwaredependencies.org/vocabularies/object-oriented#Class";
pub const IRI_ABSTRACT_CLASS: &str =
    "https://linkedsoftwaredependencies.org/vocabularies/object-oriented#AbstractClass";
pub const IRI_COMPONENT_INSTANCE: &str =
    "https://linkedsoftwaredependencies.org/vocabularies/object-oriented#ComponentInstance";
pub const IRI_COMPONENT: &str =
    "https://linkedsoftwaredependencies.org/vocabularies/object-oriented#component";
pub const IRI_COMPONENT_PATH: &str =
    "https://linkedsoftwaredependencies.org/vocabularies/object-oriented#componentPath";
pub const IRI_PARAMETER: &str =
    "https://linkedsoftwaredependencies.org/vocabularies/object-oriented#parameter";
pub const IRI_CONSTRUCTOR_ARGUMENTS: &str =
    "https://linkedsoftwaredependencies.org/vocabularies/object-oriented#constructorArguments";

pub const IRI_DOAP_NAME: &str = "http://usefulinc.com/ns/doap#name";
pub const IRI_RDFS_SUBCLASS_OF: &str = "http://www.w3.org/2000/01/rdf-schema#subClassOf";
pub const IRI_RDFS_SEE_ALSO: &str = "http://www.w3.org/2000/01/rdf-schema#seeAlso";
pub const IRI_RDFS_RANGE: &str = "http://www.w3.org/2000/01/rdf-schema#range";
pub const IRI_RDFS_COMMENT: &str = "http://www.w3.org/2000/01/rdf-schema#comment";

/// The type of a CJS component.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ComponentType {
    Class,
    AbstractClass,
    Instance,
}

impl ComponentType {
    pub fn from_type_iris(types: &[String]) -> Option<Self> {
        for t in types {
            match t.as_str() {
                IRI_CLASS => return Some(ComponentType::Class),
                IRI_ABSTRACT_CLASS => return Some(ComponentType::AbstractClass),
                IRI_COMPONENT_INSTANCE => return Some(ComponentType::Instance),
                _ => {}
            }
        }
        None
    }
}

impl std::fmt::Display for ComponentType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ComponentType::Class => write!(f, "Class"),
            ComponentType::AbstractClass => write!(f, "AbstractClass"),
            ComponentType::Instance => write!(f, "Instance"),
        }
    }
}

/// A CJS module containing components.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CjsModule {
    pub iri: String,
    pub require_name: Option<String>,
    pub components: Vec<CjsComponent>,
    pub source_file: String,
}

/// A CJS component (class, abstract class, or instance).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CjsComponent {
    pub iri: String,
    pub component_type: ComponentType,
    pub require_element: Option<String>,
    pub comment: Option<String>,
    pub parameters: Vec<CjsParameter>,
    pub extends: Vec<String>,
    pub constructor_arguments: Option<serde_json::Value>,
    pub module_iri: Option<String>,
}

/// A parameter on a CJS component.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CjsParameter {
    pub iri: String,
    pub range: Option<String>,
    pub comment: Option<String>,
    pub required: bool,
    pub lazy: bool,
    pub unique: bool,
    pub default_value: Option<serde_json::Value>,
}

/// A configuration instance (an instantiation of a component with concrete values).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfigInstance {
    pub iri: String,
    pub component_type_iri: String,
    pub parameters: std::collections::HashMap<String, serde_json::Value>,
    pub source_file: String,
}