df_hyperliquid_core/deploy/
annotation.rs1use crate::deploy::error::BadDeploy;
5use crate::utils::sign::{Map, Value};
6
7pub const MAX_CATEGORY_LEN: usize = 15;
9pub const MAX_DESCRIPTION_LEN: usize = 400;
11pub const MAX_DISPLAY_NAME_LEN: usize = 9;
13pub const MAX_KEYWORDS: usize = 2;
15pub const MAX_KEYWORD_LEN: usize = 10;
17
18#[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
29fn 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 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 #[must_use]
62 pub fn category(&self) -> &str {
63 &self.category
64 }
65
66 #[must_use]
68 pub fn description(&self) -> &str {
69 &self.description
70 }
71
72 #[must_use]
74 pub fn display_name(&self) -> Option<&str> {
75 self.display_name.as_deref()
76 }
77
78 #[must_use]
80 pub fn keywords(&self) -> &[String] {
81 &self.keywords
82 }
83
84 #[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;