use serde_json::Value;
use crate::registry;
use crate::types::{Attrs, Form, Node, Range};
pub fn is_connected(kind: &str) -> bool {
kind == "resource" || registry::kind(kind).is_some_and(|k| k.family == "connected")
}
pub fn carries_configuration(kind: &str) -> bool {
registry::kind(kind).is_some_and(|k| k.family == "technical" && k.body == "yaml")
}
pub fn is_layout(kind: &str) -> bool {
registry::kind(kind).is_some_and(|k| k.family == "layout")
}
#[derive(Clone, Debug, PartialEq)]
pub struct Location<'a> {
pub range: Option<Range>,
pub parents: Vec<&'a str>,
pub instance: &'a str,
}
impl Location<'_> {
pub fn in_layout(&self) -> bool {
self.parents.iter().all(|k| is_layout(k))
}
pub fn line(&self) -> Option<u32> {
self.range.map(|r| r.start)
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct ConnectedObject<'a> {
pub node: &'a Node,
pub kind: &'a str,
pub form: Form,
pub id: Option<&'a str>,
pub title: Option<&'a str>,
pub description: Option<&'a str>,
pub src: Option<&'a str>,
pub endpoint: Option<&'a str>,
pub attrs: &'a Attrs,
pub body: Option<&'a str>,
pub location: Location<'a>,
}
impl<'a> ConnectedObject<'a> {
pub fn attr(&self, key: &str) -> Option<&'a str> {
nonempty(self.attrs.get(key))
}
pub fn list(&self, key: &str) -> Vec<&'a str> {
list(self.attrs.get(key))
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct ConfigurationBlock<'a> {
pub node: &'a Node,
pub kind: &'a str,
pub id: Option<&'a str>,
pub title: Option<&'a str>,
pub dialect: Option<&'a str>,
pub adapter: Option<&'a str>,
pub target: Option<&'a str>,
pub schema: Option<&'a str>,
pub schema_sha256: Option<&'a str>,
pub format: &'a str,
pub data: Option<&'a Value>,
pub body: Option<&'a str>,
pub attrs: &'a Attrs,
pub location: Location<'a>,
}
impl<'a> ConfigurationBlock<'a> {
pub fn attr(&self, key: &str) -> Option<&'a str> {
nonempty(self.attrs.get(key))
}
}
fn nonempty(v: Option<&Value>) -> Option<&str> {
v.and_then(Value::as_str).map(str::trim).filter(|s| !s.is_empty())
}
fn list(v: Option<&Value>) -> Vec<&str> {
match v {
Some(Value::Array(a)) => a.iter().filter_map(Value::as_str).map(str::trim).filter(|s| !s.is_empty()).collect(),
Some(Value::String(s)) => s.split(',').map(str::trim).filter(|s| !s.is_empty()).collect(),
_ => Vec::new(),
}
}
pub fn walk<'a>(body: &'a [Node], visit: &mut dyn FnMut(&'a Node, &[&'a Node]) -> bool) {
let mut parents: Vec<&'a Node> = Vec::new();
walk_in(body, &mut parents, visit);
}
fn walk_in<'a>(body: &'a [Node], parents: &mut Vec<&'a Node>, visit: &mut dyn FnMut(&'a Node, &[&'a Node]) -> bool) {
for n in body {
if !n.is_directive() {
continue;
}
if visit(n, parents) && !n.children().is_empty() {
parents.push(n);
walk_in(n.children(), parents, visit);
parents.pop();
}
}
}
fn location<'a>(n: &'a Node, parents: &[&'a Node]) -> Location<'a> {
Location { range: n.range, parents: parents.iter().map(|p| p.kind.as_str()).collect(), instance: &n.instance }
}
pub fn connected_objects(body: &[Node]) -> Vec<ConnectedObject<'_>> {
let mut out = Vec::new();
walk(body, &mut |n, parents| {
if is_connected(&n.kind) {
out.push(ConnectedObject {
node: n,
kind: &n.kind,
form: n.form.unwrap_or(Form::Container),
id: nonempty(n.attrs.get("id")),
title: nonempty(n.attrs.get("title")),
description: nonempty(n.attrs.get("description")),
src: nonempty(n.attrs.get("src")),
endpoint: nonempty(n.attrs.get("endpoint")),
attrs: &n.attrs,
body: n.body_source(),
location: location(n, parents),
});
}
true
});
out
}
pub fn configuration_blocks(body: &[Node]) -> Vec<ConfigurationBlock<'_>> {
let mut out = Vec::new();
walk(body, &mut |n, parents| {
if carries_configuration(&n.kind) && n.form == Some(Form::Container) {
let attr = |k: &str| nonempty(n.attrs.get(k));
out.push(ConfigurationBlock {
node: n,
kind: &n.kind,
id: attr("id"),
title: attr("title"),
dialect: attr("dialect"),
adapter: attr("adapter"),
target: attr("target"),
schema: attr("schema"),
schema_sha256: attr("schema-sha256"),
format: n.value.as_ref().map_or("yaml", |v| v.format.as_str()),
data: n.value.as_ref().and_then(|v| v.data.as_ref()),
body: n.body_source(),
attrs: &n.attrs,
location: location(n, parents),
});
}
true
});
out
}