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
use crate::data::primitive::{PrimitiveArray, PrimitiveString}; use crate::data::{Client, Interval, Literal}; use crate::error_format::*; use std::collections::HashMap; pub fn client_to_json(client: &Client, interval: Interval) -> HashMap<String, Literal> { let mut map = HashMap::new(); map.insert( "bot_id".to_owned(), PrimitiveString::get_literal(&client.bot_id, interval), ); map.insert( "channel_id".to_owned(), PrimitiveString::get_literal(&client.channel_id, interval), ); map.insert( "user_id".to_owned(), PrimitiveString::get_literal(&client.user_id, interval), ); map } pub fn accept_to_array(literal: &HashMap<String, Literal>, mut vec: Vec<Literal>) -> Vec<Literal> { match literal.get("accepts") { Some(literal) => { match Literal::get_value::<Vec<Literal>>( &literal.primitive, literal.interval, ERROR_UNREACHABLE.to_owned(), ) { Ok(array) => vec.append(&mut array.to_owned()), Err(_) => vec.push(literal.to_owned()), } vec } None => vec, } } pub fn accepts_from_buttons(buttons: &Literal) -> Literal { match Literal::get_value::<Vec<Literal>>( &buttons.primitive, buttons.interval, ERROR_UNREACHABLE.to_owned(), ) { Ok(vec) => { let array = vec.iter().fold(vec![], |vec, elem| { match Literal::get_value::<HashMap<String, Literal>>( &elem.primitive, buttons.interval, ERROR_UNREACHABLE.to_owned(), ) { Ok(value) => accept_to_array(value, vec), Err(..) => vec, } }); PrimitiveArray::get_literal(&array, buttons.interval) } Err(..) => PrimitiveArray::get_literal(&[], buttons.interval), } } pub fn format_accept( values: Option<&Literal>, mut title: Vec<Literal>, interval: Interval, ) -> Literal { match values { Some(literal) => match Literal::get_value::<Vec<Literal>>( &literal.primitive, literal.interval, ERROR_UNREACHABLE.to_owned(), ) { Ok(res) => { let mut vector = res.clone(); vector.append(&mut title); PrimitiveArray::get_literal(&vector, literal.interval) } Err(..) => { let mut vector = Vec::new(); vector.push(literal.to_owned()); vector.append(&mut title); PrimitiveArray::get_literal(&vector, literal.interval) } }, None => PrimitiveArray::get_literal(&title, interval), } }