use recoco::base::schema::{
BasicValueType, EnrichedValueType, FieldSchema, StructSchema, TableKind, TableSchema, ValueType,
};
use recoco::base::value::{BasicValue, FieldValues, ScopeValue, Value};
use std::sync::Arc;
use thread_services::types::{CallInfo, ImportInfo, ParsedDocument, SymbolInfo};
pub fn serialize_parsed_doc<D: thread_services::types::Doc>(
doc: &ParsedDocument<D>,
) -> Result<Value, recoco::prelude::Error> {
let symbols = doc
.metadata
.defined_symbols
.values()
.map(serialize_symbol)
.collect::<Result<Vec<_>, _>>()?;
let imports = doc
.metadata
.imported_symbols
.values()
.map(serialize_import)
.collect::<Result<Vec<_>, _>>()?;
let calls = doc
.metadata
.function_calls
.iter()
.map(serialize_call)
.collect::<Result<Vec<_>, _>>()?;
let fingerprint_bytes = bytes::Bytes::from(doc.content_fingerprint.as_slice().to_vec());
Ok(Value::Struct(FieldValues {
fields: vec![
Value::LTable(symbols),
Value::LTable(imports),
Value::LTable(calls),
Value::Basic(BasicValue::Bytes(fingerprint_bytes)),
],
}))
}
fn serialize_symbol(info: &SymbolInfo) -> Result<ScopeValue, recoco::prelude::Error> {
Ok(ScopeValue(FieldValues {
fields: vec![
Value::Basic(BasicValue::Str(info.name.clone().into())),
Value::Basic(BasicValue::Str(format!("{:?}", info.kind).into())),
Value::Basic(BasicValue::Str(info.scope.clone().into())),
],
}))
}
fn serialize_import(info: &ImportInfo) -> Result<ScopeValue, recoco::prelude::Error> {
Ok(ScopeValue(FieldValues {
fields: vec![
Value::Basic(BasicValue::Str(info.symbol_name.clone().into())),
Value::Basic(BasicValue::Str(info.source_path.clone().into())),
Value::Basic(BasicValue::Str(format!("{:?}", info.import_kind).into())),
],
}))
}
fn serialize_call(info: &CallInfo) -> Result<ScopeValue, recoco::prelude::Error> {
Ok(ScopeValue(FieldValues {
fields: vec![
Value::Basic(BasicValue::Str(info.function_name.clone().into())),
Value::Basic(BasicValue::Int64(info.arguments_count as i64)),
],
}))
}
pub fn get_thread_parse_output_schema() -> EnrichedValueType {
EnrichedValueType {
typ: ValueType::Struct(StructSchema {
fields: Arc::new(vec![
FieldSchema::new(
"symbols".to_string(),
EnrichedValueType {
typ: ValueType::Table(TableSchema {
kind: TableKind::LTable,
row: match symbol_type() {
ValueType::Struct(s) => s,
_ => unreachable!(),
},
}),
nullable: false,
attrs: Default::default(),
},
),
FieldSchema::new(
"imports".to_string(),
EnrichedValueType {
typ: ValueType::Table(TableSchema {
kind: TableKind::LTable,
row: match import_type() {
ValueType::Struct(s) => s,
_ => unreachable!(),
},
}),
nullable: false,
attrs: Default::default(),
},
),
FieldSchema::new(
"calls".to_string(),
EnrichedValueType {
typ: ValueType::Table(TableSchema {
kind: TableKind::LTable,
row: match call_type() {
ValueType::Struct(s) => s,
_ => unreachable!(),
},
}),
nullable: false,
attrs: Default::default(),
},
),
FieldSchema::new(
"content_fingerprint".to_string(),
EnrichedValueType {
typ: ValueType::Basic(BasicValueType::Bytes),
nullable: false,
attrs: Default::default(),
},
),
]),
description: None,
}),
nullable: false,
attrs: Default::default(),
}
}
pub fn symbol_type() -> ValueType {
ValueType::Struct(StructSchema {
fields: vec![
FieldSchema::new(
"name".to_string(),
EnrichedValueType {
typ: ValueType::Basic(BasicValueType::Str),
nullable: false,
attrs: Default::default(),
},
),
FieldSchema::new(
"kind".to_string(),
EnrichedValueType {
typ: ValueType::Basic(BasicValueType::Str),
nullable: false,
attrs: Default::default(),
},
),
FieldSchema::new(
"scope".to_string(),
EnrichedValueType {
typ: ValueType::Basic(BasicValueType::Str),
nullable: false,
attrs: Default::default(),
},
),
]
.into(),
description: None,
})
}
pub fn import_type() -> ValueType {
ValueType::Struct(StructSchema {
fields: vec![
FieldSchema::new(
"symbol_name".to_string(),
EnrichedValueType {
typ: ValueType::Basic(BasicValueType::Str),
nullable: false,
attrs: Default::default(),
},
),
FieldSchema::new(
"source_path".to_string(),
EnrichedValueType {
typ: ValueType::Basic(BasicValueType::Str),
nullable: false,
attrs: Default::default(),
},
),
FieldSchema::new(
"kind".to_string(),
EnrichedValueType {
typ: ValueType::Basic(BasicValueType::Str),
nullable: false,
attrs: Default::default(),
},
),
]
.into(),
description: None,
})
}
pub fn call_type() -> ValueType {
ValueType::Struct(StructSchema {
fields: vec![
FieldSchema::new(
"function_name".to_string(),
EnrichedValueType {
typ: ValueType::Basic(BasicValueType::Str),
nullable: false,
attrs: Default::default(),
},
),
FieldSchema::new(
"arguments_count".to_string(),
EnrichedValueType {
typ: ValueType::Basic(BasicValueType::Int64),
nullable: false,
attrs: Default::default(),
},
),
]
.into(),
description: None,
})
}