use std::collections::{BTreeMap, BTreeSet};
use serde::Serialize;
use serde::de::DeserializeOwned;
use serde_json::{Value, json};
use crate::effect::{Effect, EffectReq};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Pointer {
pub phase: u8,
pub x: f32,
pub y: f32,
pub at_ms: u64,
}
impl Pointer {
pub const DOWN: u8 = 0;
pub const MOVE: u8 = 1;
pub const UP: u8 = 2;
pub const CANCEL: u8 = 3;
}
#[derive(Debug)]
pub struct Ctx<X> {
pub(crate) at: u64,
pub(crate) next_id: u64,
pub(crate) dirty: BTreeSet<String>,
pub(crate) effects: Vec<EffectReq<X>>,
}
impl<X> Default for Ctx<X> {
fn default() -> Self {
Self {
at: 0,
next_id: 0,
dirty: BTreeSet::new(),
effects: Vec::new(),
}
}
}
impl<X> Ctx<X> {
#[must_use]
pub const fn at(&self) -> u64 {
self.at
}
pub fn dirty(&mut self, key: impl Into<String>) {
self.dirty.insert(key.into());
}
pub fn emit(&mut self, effect: Effect<X>) -> u64 {
self.next_id += 1;
let id: u64 = self.next_id;
self.effects.push(EffectReq { id, effect });
id
}
pub fn emit_app(&mut self, effect: X) -> u64 {
self.emit(Effect::App(effect))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Pending<T> {
waiting: BTreeMap<u64, T>,
}
impl<T> Default for Pending<T> {
fn default() -> Self {
Self {
waiting: BTreeMap::new(),
}
}
}
impl<T> Pending<T> {
pub fn emit<X>(&mut self, ctx: &mut Ctx<X>, effect: Effect<X>, what: T) -> u64 {
let id: u64 = ctx.emit(effect);
self.waiting.insert(id, what);
id
}
pub fn take(&mut self, id: u64) -> Option<T> {
self.waiting.remove(&id)
}
#[must_use]
pub fn len(&self) -> usize {
self.waiting.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.waiting.is_empty()
}
}
#[derive(Debug, Clone, Copy)]
pub enum FieldType {
String,
Integer,
Number,
Boolean,
}
impl FieldType {
const fn name(self) -> &'static str {
match self {
Self::String => "string",
Self::Integer => "integer",
Self::Number => "number",
Self::Boolean => "boolean",
}
}
}
#[derive(Debug, Clone)]
pub struct ActionSpec {
pub name: String,
pub schema: Value,
}
impl ActionSpec {
#[must_use]
pub fn new(name: &str) -> Self {
Self {
name: name.to_owned(),
schema: json!({}),
}
}
#[must_use]
pub fn field(mut self, field: &str, ty: FieldType) -> Self {
let mut props: Value = self.schema.get("properties").cloned().unwrap_or(json!({}));
let mut required: Value = self.schema.get("required").cloned().unwrap_or(json!([]));
props[field] = json!({"type": ty.name()});
if let Some(list) = required.as_array_mut() {
list.push(json!(field));
}
self.schema = json!({"type": "object", "properties": props, "required": required});
self
}
}
pub trait App: Sized {
type Action: DeserializeOwned;
type Effect: Serialize;
fn boot(boot: &Value, ctx: &mut Ctx<Self::Effect>) -> Self;
fn update(&mut self, action: Self::Action, ctx: &mut Ctx<Self::Effect>);
fn resolve(&mut self, id: u64, result: Result<Value, String>, ctx: &mut Ctx<Self::Effect>);
fn pointer(&mut self, _pointer: Pointer, _ctx: &mut Ctx<Self::Effect>) {}
fn blob(&mut self, _name: &str, _bytes: &[u8], _ctx: &mut Ctx<Self::Effect>) {}
fn view(&self, key: &str) -> Option<Value>;
fn screen(&self) -> String;
fn actions(&self) -> Vec<ActionSpec>;
}