use crate::api::browse::rest_search::{Code, Page};
use crate::api::{ApiError, GitHub};
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct CodeHits {
pub paths: Vec<String>,
pub total: u64,
pub incomplete: bool,
}
impl GitHub {
pub async fn code_search(&self, query: &str) -> Result<CodeHits, ApiError> {
let q: String = url::form_urlencoded::byte_serialize(query.as_bytes()).collect();
let path = format!("/search/code?q={q}&per_page=10");
let body = self.rest_get(&path, "the code search").await?;
let page: Page<Code> = serde_json::from_str(&body)?;
Ok(CodeHits {
paths: page.items.into_iter().map(|c| c.path).collect(),
total: page.total_count,
incomplete: page.incomplete_results,
})
}
}