mostro_core/
rating.rs

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use anyhow::{Ok, Result};
use nostr_sdk::prelude::*;
use serde::{Deserialize, Serialize};

/// We use this struct to create a user reputation
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct Rating {
    pub total_reviews: u64,
    pub total_rating: f64,
    pub last_rating: u8,
    pub max_rate: u8,
    pub min_rate: u8,
}

impl Rating {
    pub fn new(
        total_reviews: u64,
        total_rating: f64,
        last_rating: u8,
        min_rate: u8,
        max_rate: u8,
    ) -> Self {
        Self {
            total_reviews,
            total_rating,
            last_rating,
            min_rate,
            max_rate,
        }
    }

    /// New order from json string
    pub fn from_json(json: &str) -> Result<Self> {
        Ok(serde_json::from_str(json)?)
    }

    /// Get order as json string
    pub fn as_json(&self) -> Result<String> {
        Ok(serde_json::to_string(&self)?)
    }

    /// Transform Rating struct to tuple vector to easily interact with Nostr
    pub fn to_tags(&self) -> Result<Tags> {
        let tags = vec![
            Tag::custom(
                TagKind::Custom(std::borrow::Cow::Borrowed("total_reviews")),
                vec![self.total_reviews.to_string()],
            ),
            Tag::custom(
                TagKind::Custom(std::borrow::Cow::Borrowed("total_rating")),
                vec![self.total_rating.to_string()],
            ),
            Tag::custom(
                TagKind::Custom(std::borrow::Cow::Borrowed("last_rating")),
                vec![self.last_rating.to_string()],
            ),
            Tag::custom(
                TagKind::Custom(std::borrow::Cow::Borrowed("max_rate")),
                vec![self.max_rate.to_string()],
            ),
            Tag::custom(
                TagKind::Custom(std::borrow::Cow::Borrowed("min_rate")),
                vec![self.min_rate.to_string()],
            ),
            Tag::custom(
                TagKind::Custom(std::borrow::Cow::Borrowed("z")),
                vec!["rating".to_string()],
            ),
        ];

        Ok(Tags::new(tags))
    }

    /// Transform tuple vector to Rating struct
    pub fn from_tags(tags: Tags) -> Result<Self> {
        let mut total_reviews = 0;
        let mut total_rating = 0.0;
        let mut last_rating = 0;
        let mut max_rate = 0;
        let mut min_rate = 0;

        for tag in tags.into_iter() {
            let t = tag.to_vec();
            let key = t
                .first()
                .ok_or_else(|| anyhow::anyhow!("Missing tag key"))?;
            let value = t
                .get(1)
                .ok_or_else(|| anyhow::anyhow!("Missing tag value"))?;
            match key.as_str() {
                "total_reviews" => total_reviews = value.parse::<u64>()?,
                "total_rating" => total_rating = value.parse::<f64>()?,
                "last_rating" => last_rating = value.parse::<u8>()?,
                "max_rate" => max_rate = value.parse::<u8>()?,
                "min_rate" => min_rate = value.parse::<u8>()?,
                _ => {}
            }
        }

        Ok(Self {
            total_reviews,
            total_rating,
            last_rating,
            max_rate,
            min_rate,
        })
    }
}