1use extism_pdk::*;
2use fluentci_types::git as types;
3use serde::{Deserialize, Serialize};
4
5use super::directory::Directory;
6
7#[host_fn]
8extern "ExtismHost" {
9    fn branch(name: String);
10    fn commit() -> String;
11    fn tree() -> Json<Directory>;
12}
13
14#[derive(Serialize, Deserialize)]
15pub struct Git {
16    pub id: String,
17}
18
19impl From<types::Git> for Git {
20    fn from(git: types::Git) -> Self {
21        Git { id: git.id }
22    }
23}
24
25impl Git {
26    pub fn branch(&self, name: &str) -> Result<Git, Error> {
27        unsafe { branch(name.into()) }?;
28        Ok(Git {
29            id: self.id.clone(),
30        })
31    }
32
33    pub fn commit(&self) -> Result<String, Error> {
34        unsafe { commit() }
35    }
36
37    pub fn tree(&self) -> Result<Directory, Error> {
38        unsafe { tree() }.map(|directory| directory.into_inner())
39    }
40}