changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > demo / obj/src/types.rs

changeset 11: d8f806f1d327
parent: 315fedf35bc7
author: ellis <ellis@rwest.io>
date: Sun, 14 May 2023 21:27:04 -0400
permissions: -rw-r--r--
description: obj updates
1 //! obj/src/types.rs --- OBJ type descriptions used by our demo
2 use crate::{Deserialize, Objective, Result, Serialize};
3 use std::collections::HashMap;
4 
5 /// APPLICATION TYPES
6 #[derive(Serialize,Deserialize,Default)]
7 pub enum Service {
8  Weather,
9  Stocks,
10  Dynamic(Vec<Service>),
11  Custom(CustomService),
12  #[default]
13  Bench,
14 }
15 
16 impl Objective for Service {}
17 
18 impl From<&str> for Service {
19  fn from(value: &str) -> Self {
20  match value {
21  "weather" => Service::Weather,
22  "stocks" => Service::Stocks,
23  "bench" => Service::Bench,
24  s => {
25  if s.contains(",") {
26  let x = s.split(",");
27  Service::Dynamic(
28  x.map(|y| Service::Custom(y.into()))
29  .collect::<Vec<Service>>(),
30  )
31  } else {
32  Service::Custom(s.into())
33  }
34  }
35  }
36  }
37 }
38 
39 #[derive(Serialize,Deserialize,Default)]
40 pub struct CustomService {
41  name: String,
42  registry: HashMap<String,Vec<u8>>,
43 }
44 
45 impl Objective for CustomService {}
46 impl From<CustomService> for Service {
47  fn from(value: CustomService) -> Self {
48  Service::Custom(value)
49  }
50 }
51 impl From<&str> for CustomService {
52  fn from(value: &str) -> Self {
53  let name = value.to_owned();
54  let registry = HashMap::new();
55  CustomService { name, registry }
56  }
57 }
58 
59 #[derive(Serialize, Deserialize,Default)]
60 pub struct Complex<X: Objective> {
61  data: X,
62  stack: Vec<u8>,
63  registry: HashMap<String,Vec<u8>>,
64 }
65 
66 impl Objective for Complex<Service> {}
67 
68 pub fn generate_complex() -> Result<Complex<Service>> {
69  Ok(Complex::<Service>::from_json_str("hi")?)
70 }