use serde::{Deserialize, Serialize};
use crate::schema;
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct ProjectIr {
pub files: Vec<FileIr>,
pub modules: Vec<ModuleIr>,
pub functions: Vec<FunctionIr>,
pub api_endpoints: Vec<ApiEndpointIr>,
pub external_apis: Vec<ExternalApiIr>,
pub edges: Vec<EdgeIr>,
#[serde(default)]
pub classes: Vec<ClassIr>,
#[serde(default)]
pub properties: Vec<PropertyIr>,
#[serde(default)]
pub behaviours: Vec<BehaviourIr>,
#[serde(default)]
pub callbacks: Vec<CallbackIr>,
}
impl ProjectIr {
pub fn empty() -> Self {
Self::default()
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct FileIr {
pub path: String,
pub language: String,
pub framework: Option<String>,
pub project_name: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ModuleIr {
pub name: String,
pub path: String,
pub language: String,
pub framework: Option<String>,
pub project_name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub code_bytes: Option<Vec<u8>>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ClassIr {
pub fqn: String,
pub name: String,
pub path: String,
pub language: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub project_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub kind: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub code_bytes: Option<Vec<u8>>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct PropertyIr {
pub fqn: String,
pub name: String,
pub class_fqn: String,
pub path: String,
pub language: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub project_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub declared_type: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub code_bytes: Option<Vec<u8>>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct FunctionIr {
pub name: String,
pub fqn: String,
pub path: String,
pub language: String,
pub framework: Option<String>,
pub project_name: Option<String>,
pub arity: Option<u32>,
pub return_type: Option<String>,
pub param_count: Option<u32>,
pub param_types: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub code_bytes: Option<Vec<u8>>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ApiEndpointIr {
pub methods: Vec<String>,
pub path: String,
pub protocol: Option<String>,
pub framework: Option<String>,
pub project_name: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ExternalApiIr {
pub name: String,
pub base_url: Option<String>,
pub protocol: Option<String>,
pub provider: Option<String>,
pub service: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub norm_path: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct BehaviourIr {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub path: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub language: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub project_name: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct CallbackIr {
pub name: String,
pub fqn: String,
pub arity: u32,
pub optional: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub language: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub project_name: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum EdgeKind {
DeclaresModule,
DeclaresFunction,
DeclaresClass,
DeclaresProperty,
DependsOnFile,
CallsFunction,
HandlesApi,
CallsExternalApi,
UsesClass,
ClassUsesClass,
SameApi,
ImplementsBehaviour,
DeclaresCallback,
ImplementsCallback,
DeclaresBehaviour,
ExtendsBehaviour,
OverridesCallback,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EdgeIr {
pub kind: EdgeKind,
pub from_label: String,
pub from_key: String,
pub to_label: String,
pub to_key: String,
}
impl EdgeKind {
pub fn to_rel_type(&self) -> crate::edge::RelType {
use crate::edge::RelType;
match self {
EdgeKind::DeclaresModule => RelType::DeclaresModule,
EdgeKind::DeclaresFunction => RelType::DeclaresFunction,
EdgeKind::DeclaresClass => RelType::DeclaresClass,
EdgeKind::DeclaresProperty => RelType::DeclaresProperty,
EdgeKind::DependsOnFile => RelType::DependsOnFile,
EdgeKind::CallsFunction => RelType::CallsFunction,
EdgeKind::HandlesApi => RelType::HandlesApi,
EdgeKind::CallsExternalApi => RelType::CallsExternalApi,
EdgeKind::UsesClass => RelType::UsesClass,
EdgeKind::ClassUsesClass => RelType::ClassUsesClass,
EdgeKind::SameApi => RelType::SameApi,
EdgeKind::ImplementsBehaviour => RelType::ImplementsBehaviour,
EdgeKind::DeclaresCallback => RelType::DeclaresCallback,
EdgeKind::ImplementsCallback => RelType::ImplementsCallback,
EdgeKind::DeclaresBehaviour => RelType::DeclaresBehaviour,
EdgeKind::ExtendsBehaviour => RelType::ExtendsBehaviour,
EdgeKind::OverridesCallback => RelType::OverridesCallback,
}
}
}
pub fn external_api_key(base_url: &str, norm_path: &str) -> String {
format!("{base_url}|{norm_path}")
}
pub fn api_endpoint_key(methods: &[String], path: &str) -> String {
format!("{} {}", methods.join(","), path)
}
pub fn module_key(name: &str, path: &str) -> String {
format!("{name}@{path}")
}
impl From<schema::NodeLabel> for String {
fn from(label: schema::NodeLabel) -> Self {
label.to_string()
}
}