twetch_sdk/post/
mod.rs

1use crate::commands::PayCommand;
2use crate::mentions::Mentions;
3use serde::*;
4use wasm_bindgen::prelude::*;
5
6#[wasm_bindgen]
7#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
8pub struct Post {
9    description: String,
10}
11
12#[wasm_bindgen]
13impl Post {
14    #[wasm_bindgen(js_name = fromDescription)]
15    pub fn from_description(description: String) -> Post {
16        return Post { description };
17    }
18
19    #[wasm_bindgen(js_name = estimateUsd)]
20    pub fn estimate_usd(&self, exchange_rate: f64) -> f64 {
21        if self.description.chars().count() <= 0 {
22            return 0.00f64;
23        }
24
25        let mut sum = 0.02f64;
26
27        sum += match PayCommand::from_string(&self.description) {
28            None => 0f64,
29            Some(pay_command) => pay_command.get_amount_usd(&exchange_rate),
30        };
31
32        sum += match Mentions::from_string(&self.description) {
33            None => 0f64,
34            Some(mentions) => mentions.estimate_usd,
35        };
36
37        return format!("{:.1$}", sum, 2).parse::<f64>().unwrap();
38    }
39
40    #[wasm_bindgen(js_name = getPayCommand)]
41    pub fn get_pay_command(&self, exchange_rate: f64) -> Option<String> {
42        let pay_command = match PayCommand::from_string(&self.description) {
43            None => return None,
44            Some(p) => p,
45        };
46
47        let _bsv = pay_command.get_amount_bsv(&exchange_rate);
48        let _usd = pay_command.get_amount_usd(&exchange_rate);
49
50        return Some("".to_string());
51    }
52}