1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
extern crate self as ftd_rt;

mod condition;
mod dnode;
mod event;
mod html;
mod ui;
#[cfg(feature = "wasm")]
mod wasm;

pub use condition::Condition;
pub use event::{Action, Event};
pub use html::{anchor, color, length, overflow, Node};
pub use ui::{
    Anchor, AttributeType, Code, Color, Column, Common, ConditionalAttribute, ConditionalValue,
    Container, Element, ExternalFont, FontDisplay, GradientDirection, IFrame, Image, Input, Length,
    NamedFont, Overflow, Position, Region, Row, Scene, Style, Text, TextAlign, TextBlock,
    TextFormat, Weight,
};

#[derive(serde::Serialize, serde::Deserialize, Eq, PartialEq, Debug, Default, Clone)]
pub struct Rendered {
    pub original: String,
    pub rendered: String,
}

impl From<&str> for Rendered {
    fn from(item: &str) -> Self {
        Rendered {
            original: item.to_string(),
            rendered: item.to_string(),
        }
    }
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("invalid input: {message}")]
    InvalidInput { message: String, context: String },
}

pub type Map = std::collections::BTreeMap<String, String>;
type Result<T> = std::result::Result<T, Error>;

#[derive(serde::Deserialize)]
#[cfg_attr(
    not(feature = "wasm"),
    derive(serde::Serialize, PartialEq, Debug, Default, Clone)
)]
pub struct Document {
    pub html: String,
    pub data: ftd_rt::DataDependenciesMap,
    pub external_children: ExternalChildrenDependenciesMap,
}

pub fn e<T, S1, S2>(m: S1, c: S2) -> Result<T>
where
    S1: Into<String>,
    S2: Into<String>,
{
    Err(Error::InvalidInput {
        message: m.into(),
        context: c.into(),
    })
}

pub fn get_name<'a, 'b>(prefix: &'a str, s: &'b str, doc_id: &str) -> ftd_rt::Result<&'b str> {
    match s.split_once(' ') {
        Some((p1, p2)) => {
            if p1 != prefix {
                return ftd_rt::e(format!("must start with {}", prefix), doc_id);
            }
            Ok(p2)
        }
        None => ftd_rt::e(
            format!("{} does not contain space (prefix={})", s, prefix),
            doc_id,
        ),
    }
}

pub type DataDependenciesMap = std::collections::BTreeMap<String, Data>;

#[derive(serde::Deserialize)]
#[cfg_attr(
    not(feature = "wasm"),
    derive(serde::Serialize, PartialEq, Debug, Default, Clone)
)]
pub struct Data {
    pub value: String,
    pub dependencies: ftd_rt::Map,
}

pub type ExternalChildrenDependenciesMap =
    std::collections::BTreeMap<String, Vec<ExternalChildrenCondition>>;

#[derive(serde::Deserialize)]
#[cfg_attr(
    not(feature = "wasm"),
    derive(serde::Serialize, PartialEq, Debug, Default, Clone)
)]
pub struct ExternalChildrenCondition {
    pub condition: Vec<String>,
    pub set_at: String,
}

#[derive(serde::Deserialize)]
#[cfg_attr(
    not(feature = "wasm"),
    derive(PartialEq, Debug, Clone, serde::Serialize)
)]
pub enum DependencyType {
    Style,
    Visible,
    Value,
}

#[derive(serde::Deserialize)]
#[cfg_attr(
    not(feature = "wasm"),
    derive(serde::Serialize, PartialEq, Debug, Clone)
)]
pub struct Dependencies {
    pub dependency_type: DependencyType,
    pub condition: Option<String>,
    pub parameters: std::collections::BTreeMap<String, ValueWithDefault>,
}

#[derive(serde::Deserialize)]
#[cfg_attr(
    not(feature = "wasm"),
    derive(serde::Serialize, PartialEq, Debug, Clone)
)]
pub struct ValueWithDefault {
    pub value: ConditionalValue,
    pub default: Option<ConditionalValue>,
}

#[cfg(test)]
mod test {
    #[test]
    fn get_name() {
        assert_eq!(super::get_name("fn", "fn foo").unwrap(), "foo")
    }
}