df_hyperliquid_core/deploy/
annotation.rs1use crate::deploy::error::BadDeploy;
7use crate::utils::sign::{Map, Value};
8
9pub const MAX_CATEGORY_LEN: usize = 15;
11pub const MAX_DESCRIPTION_LEN: usize = 400;
13pub const MAX_DISPLAY_NAME_LEN: usize = 9;
15pub const MAX_KEYWORDS: usize = 2;
17pub const MAX_KEYWORD_LEN: usize = 10;
19
20#[derive(Debug, Clone, PartialEq, Eq)]
24pub struct Annotation {
25 category: String,
26 description: String,
27 display_name: Option<String>,
28 keywords: Vec<String>,
29}
30
31fn short(what: &'static str, text: String, most: usize) -> Result<String, BadDeploy> {
33 if text.len() > most {
34 return Err(BadDeploy::TooLong { what, most, value: text });
35 }
36 Ok(text)
37}
38
39impl Annotation {
40 pub fn new(
43 category: impl Into<String>,
44 description: impl Into<String>,
45 display_name: Option<String>,
46 keywords: Vec<String>,
47 ) -> Result<Self, BadDeploy> {
48 if keywords.len() > MAX_KEYWORDS {
49 return Err(BadDeploy::Keywords(keywords.len()));
50 }
51 Ok(Annotation {
52 category: short("a category", category.into(), MAX_CATEGORY_LEN)?,
53 description: short("a description", description.into(), MAX_DESCRIPTION_LEN)?,
54 display_name: display_name.map(|name| short("a display name", name, MAX_DISPLAY_NAME_LEN)).transpose()?,
55 keywords: keywords
56 .into_iter()
57 .map(|keyword| short("a keyword", keyword, MAX_KEYWORD_LEN))
58 .collect::<Result<_, _>>()?,
59 })
60 }
61
62 #[must_use]
64 pub fn category(&self) -> &str {
65 &self.category
66 }
67
68 #[must_use]
70 pub fn description(&self) -> &str {
71 &self.description
72 }
73
74 #[must_use]
76 pub fn display_name(&self) -> Option<&str> {
77 self.display_name.as_deref()
78 }
79
80 #[must_use]
82 pub fn keywords(&self) -> &[String] {
83 &self.keywords
84 }
85
86 #[must_use]
89 pub fn wire_after(&self, map: Map) -> Map {
90 let display_name = self.display_name.clone().map_or(Value::Nil, Value::from);
91 map.put("category", self.category.clone())
92 .put("description", self.description.clone())
93 .put("displayName", display_name)
94 .put("keywords", Value::array(self.keywords.iter().cloned()))
95 }
96}
97
98#[cfg(test)]
99#[path = "tests/annotation.rs"]
100mod tests;