camel_case_token_with_comment/
camel_case_token_with_comment.rs

1// ---------------- [ File: src/camel_case_token_with_comment.rs ]
2crate::ix!();
3
4pub type TokenQuad = [CamelCaseTokenWithComment; 4];
5
6#[derive(Hash,Debug,Clone,PartialEq,Eq)]
7pub struct CamelCaseTokenWithComment {
8    data:    String,
9    comment: Option<String>,
10}
11
12impl CamelCaseTokenWithComment {
13
14    pub fn data(&self) -> &str {
15        &self.data
16    }
17}
18
19impl Display for CamelCaseTokenWithComment {
20    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
21        match &self.comment {
22            Some(comment) => write!(f, "{} -- {}", self.data, comment),
23            None => write!(f, "{}", self.data),
24        }
25    }
26}
27
28impl CamelCaseTokenWithComment {
29
30    pub fn target_path_for_ai_json_expansion(&self, target_dir: impl AsRef<Path>) -> PathBuf {
31
32        // Convert 'token_name' to snake_case
33        let snake_token_name = to_snake_case(&self.data);
34
35        // Determine the output filename based on custom_id
36        // You can customize this as needed, e.g., using token names
37        let filename = format!("{}.json", snake_token_name);
38
39        target_dir.as_ref().to_path_buf().join(filename)
40    }
41}
42
43impl Into<String> for CamelCaseTokenWithComment {
44
45    fn into(self) -> String {
46        match self.comment {
47            Some(ref comment) => format!("{} -- {}", self.data, comment),
48            None              => format!("{}", self.data),
49        }
50    }
51}
52
53impl std::str::FromStr for CamelCaseTokenWithComment {
54    type Err = TokenParseError;
55
56    fn from_str(s: &str) -> Result<Self, Self::Err> {
57        let line = s.trim();
58        if line.is_empty() {
59            return Err(TokenParseError::InvalidTokenLine(line.to_string()));
60        }
61
62        let parts: Vec<&str> = line.splitn(2, "--").collect();
63        let data = parts[0].trim();
64        if data.is_empty() {
65            return Err(TokenParseError::InvalidTokenLine(line.to_string()));
66        }
67
68        let comment = parts.get(1).map(|&s| s.trim().to_string()).filter(|s| !s.is_empty());
69
70        Ok(CamelCaseTokenWithComment {
71            data: data.to_string(),
72            comment,
73        })
74    }
75}
76
77pub async fn parse_token_file(filename: &str) 
78    -> Result<Vec<CamelCaseTokenWithComment>, TokenParseError> 
79{
80    info!("parsing token file {}", filename);
81
82    let file = File::open(filename).await?;
83    let reader = BufReader::new(file);
84    let mut lines = reader.lines();
85
86    let mut tokens = Vec::new();
87
88    while let Some(line) = lines.next_line().await? {
89        let line = line.trim();
90        if line.is_empty() {
91            continue;
92        }
93        match line.parse::<CamelCaseTokenWithComment>() {
94            Ok(token) => tokens.push(token),
95            Err(e) => {
96                warn!("{:?}", e);
97            }
98        }
99    }
100
101    Ok(tokens)
102}