Skip to main content

ui_cli/shared/
rust_ui_client.rs

1use crate::shared::cli_error::{CliError, CliResult};
2use crate::shared::markdown_utils::extract_rust_code_from_markdown;
3
4pub struct RustUIClient;
5
6impl RustUIClient {
7    const BASE_URL: &str = "https://www.rust-ui.com/registry";
8    const SITE_URL: &str = "https://www.rust-ui.com";
9
10    // URL builders - centralized URL construction
11    fn tree_url() -> String {
12        format!("{}/tree.md", Self::BASE_URL)
13    }
14
15    fn component_url(component_name: &str) -> String {
16        format!("{}/styles/default/{component_name}.md", Self::BASE_URL)
17    }
18
19    fn js_file_url(path: &str) -> String {
20        format!("{}{path}", Self::SITE_URL)
21    }
22
23    // Consolidated HTTP fetch method
24    async fn fetch_response(url: &str) -> CliResult<reqwest::Response> {
25        let response = reqwest::get(url).await.map_err(|_| CliError::registry_request_failed())?;
26
27        if !response.status().is_success() {
28            return Err(CliError::registry_request_failed());
29        }
30
31        Ok(response)
32    }
33
34    // Public API methods
35    pub async fn fetch_tree_md() -> CliResult<String> {
36        let response = Self::fetch_response(&Self::tree_url()).await?;
37        let content = response.text().await.map_err(|_| CliError::registry_request_failed())?;
38
39        if content.is_empty() {
40            return Err(CliError::registry_request_failed());
41        }
42
43        Ok(content)
44    }
45
46    pub async fn fetch_styles_default(component_name: &str) -> CliResult<String> {
47        let response = Self::fetch_response(&Self::component_url(component_name)).await?;
48        let markdown_content = response.text().await.map_err(|_| CliError::registry_request_failed())?;
49
50        extract_rust_code_from_markdown(&markdown_content).ok_or_else(CliError::registry_component_missing)
51    }
52
53    /// Fetch a JS file from the site (e.g., /hooks/lock_scroll.js)
54    pub async fn fetch_js_file(path: &str) -> CliResult<String> {
55        let response = Self::fetch_response(&Self::js_file_url(path)).await?;
56        let content = response.text().await.map_err(|_| CliError::registry_request_failed())?;
57
58        if content.is_empty() {
59            return Err(CliError::registry_request_failed());
60        }
61
62        Ok(content)
63    }
64}