Skip to main content

opencode_sdk/http/
find.rs

1//! Find API for OpenCode.
2//!
3//! Endpoints for searching files and symbols.
4
5use crate::error::Result;
6use crate::http::HttpClient;
7use crate::types::api::FindResponse;
8use reqwest::Method;
9
10/// Find API client.
11#[derive(Clone)]
12pub struct FindApi {
13    http: HttpClient,
14}
15
16impl FindApi {
17    /// Create a new Find API client.
18    pub fn new(http: HttpClient) -> Self {
19        Self { http }
20    }
21
22    /// Search for text in files using ripgrep.
23    ///
24    /// # Errors
25    ///
26    /// Returns an error if the request fails.
27    pub async fn text(&self, pattern: &str) -> Result<FindResponse> {
28        let encoded = urlencoding::encode(pattern);
29        self.http
30            .request_json(Method::GET, &format!("/find?pattern={}", encoded), None)
31            .await
32    }
33
34    /// Search for files by name.
35    ///
36    /// # Errors
37    ///
38    /// Returns an error if the request fails.
39    pub async fn files(&self, query: &str) -> Result<FindResponse> {
40        let encoded = urlencoding::encode(query);
41        self.http
42            .request_json(Method::GET, &format!("/find/file?query={}", encoded), None)
43            .await
44    }
45
46    /// Search for symbols.
47    ///
48    /// # Errors
49    ///
50    /// Returns an error if the request fails.
51    pub async fn symbols(&self, query: &str) -> Result<FindResponse> {
52        let encoded = urlencoding::encode(query);
53        self.http
54            .request_json(
55                Method::GET,
56                &format!("/find/symbol?query={}", encoded),
57                None,
58            )
59            .await
60    }
61}