changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > demo / obj/src/types.rs

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