wimcm/
presets.rs

1use wbdl::Date;
2use wjp::{Serialize, Values};
3
4use crate::method::WIMCMethods;
5use crate::out::{WIMCOutput};
6use crate::r#in::WIMCInput;
7use crate::WIMCError;
8
9const SPACE: char = ' ';
10const CLEANUP: WIMCInput = WIMCInput::from_val(Values::Null, Vec::new(), WIMCMethods::Cleanup,None);
11const PING: WIMCInput = WIMCInput::from_val(Values::Null, Vec::new(), WIMCMethods::Ping,None);
12
13pub fn echo(msg: &str) -> WIMCInput {
14    let vec = msg.split(SPACE).map(String::from).collect();
15    WIMCInput::from_val(Values::Null, vec, WIMCMethods::Echo,None)
16}
17
18pub const fn ping() -> WIMCInput {
19    PING
20}
21
22pub fn pong() -> WIMCOutput {
23    WIMCOutput::from_values(Values::String(String::from("Pong")))
24}
25
26pub fn respond(msg: Vec<String>) -> WIMCOutput {
27    let mut string = String::new();
28    let len = msg.len();
29    for word in msg {
30        string.push_str(word.as_str());
31        string.push(SPACE);
32    }
33    if len == 1 {
34        string.pop();
35    }
36    WIMCOutput::from_values(Values::String(string))
37}
38
39pub const fn cleanup() -> WIMCInput {
40    CLEANUP
41}
42
43pub fn store<T: Serialize,S: ToString>(obj: T, mut params: Vec<String>, time: Option<Date>,id: S) -> WIMCInput {
44    if let Some(time) = time {
45        params.push(time.to_string())
46    }
47    WIMCInput::new(obj, params, WIMCMethods::Store,Some(id.to_string().as_str()))
48}
49pub fn store_incr<T: Serialize>(obj: T, mut params: Vec<String>, time: Option<Date>) -> WIMCInput{
50    if let Some(time) = time {
51        params.push(time.to_string())
52    }
53    WIMCInput::new(obj, params, WIMCMethods::StoreInc,None)
54}
55
56pub const fn stored(id: u128) -> WIMCOutput {
57    WIMCOutput::from_values(Values::Number(id as f64))
58}
59
60pub const fn error(error: WIMCError) -> WIMCOutput {
61    WIMCOutput::from_err(error)
62}
63
64pub const fn found(values: Values) -> WIMCOutput {
65    WIMCOutput::from_values(values)
66}
67
68pub const fn get(id: u128) -> WIMCInput {
69    WIMCInput::from_val(Values::Number(id as f64), vec![], WIMCMethods::Get,None)
70}
71pub const fn query(params: Vec<String>) -> WIMCInput {
72    WIMCInput::from_val(Values::Null, params, WIMCMethods::Query,None)
73}
74pub const fn remove(id: u128) -> WIMCInput {
75    WIMCInput::from_val(Values::Number(id as f64), vec![], WIMCMethods::Remove,None)
76}