smartcalc/tokinizer/rule_tokinizer/rules/
dynamic_type_rules.rs

1/*
2 * smartcalc v1.0.8
3 * Copyright (c) Erhan BARIS (Ruslan Ognyanov Asenov)
4 * Licensed under the GNU General Public License v2.0.
5 */
6
7use alloc::rc::Rc;
8use alloc::string::String;
9use alloc::string::ToString;
10use alloc::collections::btree_map::BTreeMap;
11
12use crate::config::SmartCalcConfig;
13use crate::compiler::dynamic_type::DynamicTypeItem;
14use crate::tokinizer::get_dynamic_type;
15use crate::tokinizer::get_text;
16use crate::{tokinizer::Tokinizer, types::{TokenType}};
17use crate::tokinizer::{TokenInfo};
18
19pub fn dynamic_type_convert(config: &SmartCalcConfig, _: &Tokinizer, fields: &BTreeMap<String, Rc<TokenInfo>>) -> core::result::Result<TokenType, String> {
20    if fields.contains_key("source") && fields.contains_key("type") {
21        let target_type = match get_text("type", fields) {
22            Some(data) => data,
23            None => return Err("Dynamic type not valid".to_string())
24        };
25        let (number, source_type) = match get_dynamic_type("source", fields) {
26            Some(data) => data,
27            None => return Err("Dynamic type not valid".to_string())
28        };
29        
30        if let Some((new_number, new_type)) = DynamicTypeItem::convert(config, number, source_type, target_type) {
31            return Ok(TokenType::DynamicType(new_number, new_type))
32        };
33    }
34
35    Err("Dynamic type not valid".to_string())
36}