Skip to main content

df_hyperliquid_core/deploy/
annotation.rs

1//! What frontends show for a spot token or a HIP-3 perp: the text `setTokenAnnotation` and
2//! `setPerpAnnotation` carry, checked against the lengths the docs give.
3
4use crate::deploy::error::BadDeploy;
5use crate::utils::sign::{Map, Value};
6
7/// The longest category, in characters.
8pub const MAX_CATEGORY_LEN: usize = 15;
9/// The longest description, in characters.
10pub const MAX_DESCRIPTION_LEN: usize = 400;
11/// The longest display name, in characters.
12pub const MAX_DISPLAY_NAME_LEN: usize = 9;
13/// The most keywords an annotation takes.
14pub const MAX_KEYWORDS: usize = 2;
15/// The longest keyword, in characters.
16pub const MAX_KEYWORD_LEN: usize = 10;
17
18/// A token's or perp's annotation: a category, a description, a display name
19/// frontends show instead of the listed name (`None` to keep the listed
20/// name) and up to two keywords searches match against.
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub struct Annotation {
23    category: String,
24    description: String,
25    display_name: Option<String>,
26    keywords: Vec<String>,
27}
28
29/// `text` if it is at most `most` characters.
30fn short(what: &'static str, text: String, most: usize) -> Result<String, BadDeploy> {
31    if text.chars().count() > most {
32        return Err(BadDeploy::TooLong { what, most, value: text });
33    }
34    Ok(text)
35}
36
37impl Annotation {
38    /// Refuses a category over 15 characters, a description over 400, a
39    /// display name over 9, more than 2 keywords or a keyword over 10.
40    pub fn new(
41        category: impl Into<String>,
42        description: impl Into<String>,
43        display_name: Option<String>,
44        keywords: Vec<String>,
45    ) -> Result<Self, BadDeploy> {
46        if keywords.len() > MAX_KEYWORDS {
47            return Err(BadDeploy::Keywords(keywords.len()));
48        }
49        Ok(Annotation {
50            category: short("a category", category.into(), MAX_CATEGORY_LEN)?,
51            description: short("a description", description.into(), MAX_DESCRIPTION_LEN)?,
52            display_name: display_name.map(|name| short("a display name", name, MAX_DISPLAY_NAME_LEN)).transpose()?,
53            keywords: keywords
54                .into_iter()
55                .map(|keyword| short("a keyword", keyword, MAX_KEYWORD_LEN))
56                .collect::<Result<_, _>>()?,
57        })
58    }
59
60    /// The category, at most 15 characters.
61    #[must_use]
62    pub fn category(&self) -> &str {
63        &self.category
64    }
65
66    /// The description, at most 400 characters.
67    #[must_use]
68    pub fn description(&self) -> &str {
69        &self.description
70    }
71
72    /// The name frontends show instead of the listed name; `None` keeps the listed name.
73    #[must_use]
74    pub fn display_name(&self) -> Option<&str> {
75        self.display_name.as_deref()
76    }
77
78    /// Up to two words searches match against, each at most 10 characters.
79    #[must_use]
80    pub fn keywords(&self) -> &[String] {
81        &self.keywords
82    }
83
84    /// `{"category","description","displayName","keywords"}` after the keys
85    /// already in `map`; a missing display name is sent as null.
86    #[must_use]
87    pub fn wire_after(&self, map: Map) -> Map {
88        let display_name = self.display_name.clone().map_or(Value::Nil, Value::from);
89        map.put("category", self.category.clone())
90            .put("description", self.description.clone())
91            .put("displayName", display_name)
92            .put("keywords", Value::array(self.keywords.iter().cloned()))
93    }
94}
95
96#[cfg(test)]
97#[path = "tests/annotation.rs"]
98mod tests;