pub use tilo_actions::surface::{
CONTENT_VERBS, FieldSurface, FieldType, NamedType, ParamSurface, ReturnsSurface, Surface,
TYPE_VERB, TypeCase, TypeField, TypeSet, VerbSurface,
};
use crate::resource::Resource;
use crate::shape::Shape;
use crate::vocabulary::{Declaration, vocabulary};
pub type FlatTypes = fn(&mut TypeSet) -> Vec<(String, FieldType)>;
fn params_of(
params: &[tilo_actions::ActionParam],
param_types: &[(&'static str, FieldTypeOf)],
flat_types: Option<FlatTypes>,
types: &mut TypeSet,
) -> Vec<ParamSurface> {
let flat: Vec<(String, FieldType)> = flat_types.map_or_else(Vec::new, |flat| flat(types));
params
.iter()
.map(|param| {
let schema = param
.schema
.as_deref()
.cloned()
.unwrap_or(serde_json::Value::Null);
let declared = param_types
.iter()
.find(|(name, _)| *name == param.name.as_ref());
let ty = if param.ty == tilo_actions::ParamType::Secret {
FieldType::Secret
} else {
match declared {
Some((_, ty)) => ty(types),
None => flat
.iter()
.find(|(name, _)| name == param.name.as_ref())
.map_or_else(|| FieldType::from_schema(&schema), |(_, ty)| ty.clone()),
}
};
ParamSurface {
name: param.name.to_string(),
required: param.required,
schema,
ty,
within: None,
}
})
.collect()
}
fn returns_of(
result: Option<&tilo_actions::ResultSpec>,
declared: Option<FieldTypeOf>,
types: &mut TypeSet,
) -> Option<ReturnsSurface> {
let tilo_actions::ResultSpec::Json { schema } = result? else {
return None;
};
Some(ReturnsSurface {
schema: schema.clone(),
ty: declared.map_or_else(|| FieldType::from_schema(schema), |ty| ty(types)),
})
}
fn verb_of(decl: &Declaration, types: &mut TypeSet) -> VerbSurface {
VerbSurface {
id: decl.id.to_owned(),
name: String::new(),
label: decl.shown_label(),
description: decl.description.clone(),
kind: decl.kind,
destructive: decl.destructive,
incoming_message: decl
.tags
.iter()
.any(|tag| tag == tilo_actions::INCOMING_MESSAGE_TAG),
params: params_of(&decl.params, &decl.param_types, decl.flat_types, types),
returns: returns_of(decl.result.as_ref(), decl.result_type, types),
writes: decl.writes.clone(),
field: decl.field.map(str::to_owned),
tags: Vec::new(),
outside: Vec::new(),
notice: false,
origin: None,
}
}
fn framework_verb(id: &str, writes: Vec<String>) -> VerbSurface {
VerbSurface {
id: id.to_owned(),
name: String::new(),
label: String::new(),
description: None,
kind: crate::ActionKind::Undoable,
destructive: false,
incoming_message: false,
params: Vec::new(),
returns: None,
writes,
field: None,
tags: Vec::new(),
outside: Vec::new(),
notice: false,
origin: None,
}
}
fn units_of(shape: Shape) -> Vec<String> {
let mut units = vec![tilo_actions::surface::RESOURCE_UNIT.to_owned()];
if let Shape::Record(fields) = shape {
for field in fields.iter().filter(|field| field.unit) {
let unit = match field.shape {
Shape::Collection(_) | Shape::Keyed(_) => format!("{}.*", field.key),
_ => field.key.to_owned(),
};
if !units.contains(&unit) {
units.push(unit);
}
}
}
units
}
fn edits_content(shape: Shape) -> bool {
match shape {
Shape::Record(fields) => fields
.iter()
.any(|field| !field.readonly && edits_content(field.shape)),
Shape::Collection(_) | Shape::Keyed(_) | Shape::Map(_) | Shape::Value => true,
}
}
fn stored(shape: Shape) -> &'static str {
match shape {
Shape::Value | Shape::Map(_) => "value",
Shape::Record(_) => "record",
Shape::Collection(_) => "collection",
Shape::Keyed(_) => "keyed",
}
}
fn fields_of(shape: Shape, prefix: &str, types: &mut TypeSet, out: &mut Vec<FieldSurface>) {
let (fields, item) = match shape {
Shape::Record(fields) => (fields, false),
Shape::Collection(item) | Shape::Keyed(item) => {
fields_of(*item, &format!("{prefix}*."), types, out);
return;
}
Shape::Value | Shape::Map(_) => return,
};
let _ = item;
for field in fields {
let path = format!("{prefix}{}", field.key);
out.push(FieldSurface {
path: path.clone(),
owner: field.owner.to_owned(),
title: field.title.to_owned(),
stored: stored(field.shape).to_owned(),
ty: (field.field_type)(types),
readonly: field.readonly,
unit: field.unit,
label: field.label,
whole: false,
ops: None,
});
fields_of(field.shape, &format!("{path}."), types, out);
}
}
#[must_use]
pub fn surface_of<R: Resource>() -> Surface {
surface_in(vocabulary::<R>(), R::VERSION)
}
fn surface_in(vocabulary: &crate::Vocabulary, version: u32) -> Surface {
let mut fields = Vec::new();
let mut types = TypeSet::new();
let shape = (vocabulary.shape)();
if let Some(shape) = shape {
fields_of(shape, "", &mut types, &mut fields);
}
for (path, schema) in (vocabulary.op_schemas)() {
if let Some(field) = fields.iter_mut().find(|field| field.path == path) {
field.ops = Some(schema);
}
}
let mut verbs = Vec::new();
for decl in vocabulary.declarations().iter() {
verbs.push(verb_of(decl, &mut types));
}
if let Some(shape) = shape
&& edits_content(shape)
{
verbs.push(framework_verb(TYPE_VERB, units_of(shape)));
}
Surface {
kind: vocabulary.kind.to_owned(),
label: vocabulary.label.to_owned(),
version,
person: false,
readers: reader_rule(vocabulary.readers),
verbs,
fields,
types: types.into_types(),
shows: Vec::new(),
live: None,
setters: false,
}
}
fn reader_rule(readers: &str) -> Option<tilo_actions::surface::ReaderRule> {
tilo_actions::surface::ReaderRule::parse(readers)
}
#[must_use]
pub fn reading_surface<R: crate::JsonSchema, E: crate::Effect>(kind: &str, label: &str) -> Surface {
let mut types = TypeSet::new();
let root = R::field_type(&mut types);
let verbs: Vec<VerbSurface> = E::declarations()
.iter()
.map(|decl| effect_verb(decl, &mut types))
.collect();
let mut declared = types.into_types();
let own = match &root {
FieldType::Named(name) => declared
.iter()
.position(|ty| ty.name == *name && !ty.is_enum())
.map(|at| declared.remove(at)),
_ => None,
};
let fields = own
.map(|own| {
own.fields
.into_iter()
.map(|field| FieldSurface {
path: field.name,
owner: String::new(),
title: field.title,
stored: "value".to_owned(),
ty: field.ty,
readonly: true,
unit: false,
label: false,
whole: false,
ops: None,
})
.collect()
})
.unwrap_or_default();
Surface {
kind: kind.to_owned(),
label: label.to_owned(),
version: 1,
person: false,
readers: None,
verbs,
fields,
types: declared,
shows: Vec::new(),
live: None,
setters: false,
}
}
#[must_use]
pub fn row_surface<R: crate::words::RowType, E: crate::Effect>() -> Surface {
let mut types = TypeSet::new();
let verbs = E::declarations()
.iter()
.map(|decl| effect_verb(decl, &mut types))
.collect();
Surface {
kind: R::KIND.to_owned(),
label: R::LABEL.to_owned(),
version: 1,
person: false,
readers: None,
verbs,
fields: Vec::new(),
types: types.into_types(),
shows: Vec::new(),
live: None,
setters: false,
}
}
#[must_use]
pub fn effect_verb(decl: &crate::EffectDecl, types: &mut TypeSet) -> VerbSurface {
VerbSurface {
id: decl.id.to_owned(),
name: String::new(),
label: decl.label.read(),
description: (!decl.description.is_empty()).then(|| decl.description.to_owned()),
kind: decl.action_kind,
destructive: decl.risk == tilo_actions::RiskLevel::Destructive,
incoming_message: false,
params: params_of(&decl.params, &decl.param_types, None, types),
returns: decl.result.map(|result| ReturnsSurface {
schema: (result.schema)(),
ty: result.ty.map_or_else(|| FieldType::Any, |ty| ty(types)),
}),
writes: vec!["record".to_owned()],
field: None,
tags: Vec::new(),
outside: Vec::new(),
notice: false,
origin: None,
}
}
#[must_use]
pub fn view_effects(rows: &[crate::effect::EffectRow]) -> Vec<String> {
let mut shows: Vec<String> = rows
.iter()
.filter(|row| row.kind == tilo_actions::OperationKind::View)
.map(|row| row.id.to_owned())
.collect();
shows.sort();
shows.dedup();
shows
}
#[must_use]
pub fn effect_verbs(rows: &[crate::effect::EffectRow], types: &mut TypeSet) -> Vec<VerbSurface> {
rows.iter()
.filter(|row| row.kind != tilo_actions::OperationKind::View)
.filter(|row| row.action_kind == crate::ActionKind::Read)
.filter(|row| !row.row)
.filter(|row| {
!row.items
.iter()
.any(|target| matches!(target, crate::effect::EffectTarget::Field(_)))
})
.map(|row| effect_verb(&crate::EffectDecl::of_row(row), types))
.collect()
}
#[doc(hidden)]
pub fn resource_type(
types: &mut TypeSet,
identity: &str,
name: &str,
doc: &str,
shape: Shape,
) -> FieldType {
types.named(identity, name, |types| NamedType {
tag: None,
name: name.to_owned(),
doc: doc.to_owned(),
fields: shape
.fields()
.iter()
.map(|field| TypeField {
name: field.key.to_owned(),
ty: (field.field_type)(types),
title: field.title.to_owned(),
})
.collect(),
cases: Vec::new(),
})
}
pub type FieldTypeOf = fn(&mut TypeSet) -> FieldType;
#[doc(hidden)]
pub fn resource_of(
types: &mut TypeSet,
identity: &str,
name: &str,
doc: &str,
fields: &[(&str, FieldTypeOf)],
) -> FieldType {
types.named(identity, name, |types| NamedType {
tag: None,
name: name.to_owned(),
doc: doc.to_owned(),
fields: fields
.iter()
.map(|(field, ty)| TypeField {
name: (*field).to_owned(),
ty: ty(types),
title: String::new(),
})
.collect(),
cases: Vec::new(),
})
}
#[doc(hidden)]
pub type CaseOf<'a> = (&'a str, &'a str, &'a [(&'a str, FieldTypeOf)]);
#[doc(hidden)]
pub fn enum_type(
types: &mut TypeSet,
identity: &str,
name: &str,
doc: &str,
cases: &[CaseOf<'_>],
) -> FieldType {
types.named(identity, name, |types| NamedType {
tag: None,
name: name.to_owned(),
doc: doc.to_owned(),
fields: Vec::new(),
cases: cases
.iter()
.map(|(case, label, fields)| TypeCase {
name: (*case).to_owned(),
label: if label.is_empty() {
tilo_actions::field_type::label_of(case)
} else {
(*label).trim_end_matches('.').to_owned()
},
fields: fields
.iter()
.map(|(field, ty)| TypeField {
name: (*field).to_owned(),
ty: ty(types),
title: String::new(),
})
.collect(),
})
.collect(),
})
}