feattle_core/definition.rs
1use chrono::{DateTime, Utc};
2use serde::Serialize;
3use serde_json::Value;
4use std::fmt;
5
6/// A precise description of a feattle type
7#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
8pub struct SerializedFormat {
9 /// An exact and machine-readable description of the format
10 pub kind: SerializedFormatKind,
11 /// A human-readable description of the format, shown by `Display`
12 pub tag: String,
13}
14
15/// An exact and machine-readable description of a feattle type.
16///
17/// This type can be used to create a nice human interface, like a HTML form, to edit the value
18/// of a feattle, for example. It can also be used to validate user input.
19#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
20#[serde(tag = "tag", content = "content")]
21pub enum SerializedFormatKind {
22 Bool,
23 Integer,
24 Float,
25 String(StringFormatKind),
26 /// An ordered list of homogenous types
27 List(Box<SerializedFormatKind>),
28 /// An unordered bag of homogenous types
29 Set(Box<SerializedFormatKind>),
30 /// An unordered bag of homogenous keys and values
31 Map(StringFormatKind, Box<SerializedFormatKind>),
32 Optional(Box<SerializedFormatKind>),
33}
34
35/// A precise description of a feattle string-type
36#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
37pub struct StringFormat {
38 /// An exact and machine-readable description of the format
39 pub kind: StringFormatKind,
40 /// A human-readable description of the format, shown by `Display`
41 pub tag: String,
42}
43
44/// An exact and machine-readable description of a feattle string-type
45#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
46#[serde(tag = "tag", content = "content")]
47pub enum StringFormatKind {
48 /// Accepts any possible string.
49 Any,
50 /// The string must conform to the pattern, described using
51 /// [JavaScript's RegExp syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Cheatsheet)
52 /// in `'u'` (Unicode) mode.
53 /// The matching is done against the entire value, not just any subset, as if a `^(?:` was
54 /// implied at the start of the pattern and a `)$` at the end.
55 Pattern(&'static str),
56 /// Only one of the listed values is accepted.
57 Choices(&'static [&'static str]),
58}
59
60/// A data struct, describing a single feattle.
61#[derive(Debug, Clone, Serialize)]
62pub struct FeattleDefinition {
63 /// The feattle's name
64 pub key: &'static str,
65 /// Its documentation
66 pub description: String,
67 /// The precise description of its format
68 pub format: SerializedFormat,
69 /// Its current in-memory value, as JSON
70 pub value: Value,
71 /// A short human description of its current in-memory value
72 pub value_overview: String,
73 /// Its default value, as JSON
74 pub default: Value,
75 /// The last time it was modified by an user
76 pub modified_at: Option<DateTime<Utc>>,
77 /// The user that last modified it
78 pub modified_by: Option<String>,
79}
80
81impl fmt::Display for SerializedFormat {
82 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
83 write!(f, "{}", self.tag)
84 }
85}
86
87impl fmt::Display for StringFormat {
88 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
89 write!(f, "{}", self.tag)
90 }
91}