Skip to main content

foch_annotation/
schema.rs

1//! What each annotation accepts, as plain data shared by validation,
2//! completion, hover and command-line help.
3
4use crate::value::ValueType;
5use serde::Serialize;
6
7#[derive(Clone, Copy, Debug, Serialize)]
8pub struct ParamSchema {
9	pub name: &'static str,
10	pub value_type: ValueType,
11	pub required: bool,
12	pub description: &'static str,
13	pub example: &'static str,
14}
15
16/// Unnamed arguments, such as the labels of `#mark(war, slow)`.
17#[derive(Clone, Copy, Debug, Serialize)]
18pub struct PositionalSchema {
19	pub description: &'static str,
20	pub example: &'static str,
21}
22
23/// The definition an annotation may be attached to.
24#[derive(Clone, Copy, Debug, Serialize)]
25pub struct TargetRule {
26	/// Top-level key of the definition, such as `country_event`.
27	pub key: &'static str,
28	/// Fields the definition must set to `yes`.
29	pub required_yes: &'static [&'static str],
30}
31
32#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
33#[serde(tag = "kind", rename_all = "snake_case")]
34pub enum AnnotationKind {
35	/// Attaches to the next top-level definition.
36	Primary,
37	/// Applies to the next primary annotation named `of`, like a Rust
38	/// attribute applies to the next item.
39	Modifier { of: &'static str },
40}
41
42#[derive(Clone, Copy, Debug, Serialize)]
43pub struct AnnotationSchema {
44	pub name: &'static str,
45	pub kind: AnnotationKind,
46	pub description: &'static str,
47	pub params: &'static [ParamSchema],
48	pub positional: Option<PositionalSchema>,
49	pub target: Option<TargetRule>,
50}
51
52impl AnnotationSchema {
53	pub fn param(&self, name: &str) -> Option<&'static ParamSchema> {
54		self.params.iter().find(|param| param.name == name)
55	}
56}
57
58/// The annotations a consumer understands.
59#[derive(Clone, Copy, Debug)]
60pub struct Registry {
61	pub schemas: &'static [AnnotationSchema],
62}
63
64impl Registry {
65	pub fn get(&self, name: &str) -> Option<&'static AnnotationSchema> {
66		self.schemas.iter().find(|schema| schema.name == name)
67	}
68}