1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use extism_pdk::*;
use fluentci_types::git as types;
use serde::{Deserialize, Serialize};

use super::directory::Directory;

#[host_fn]
extern "ExtismHost" {
    fn branch(name: String);
    fn commit() -> String;
    fn tree() -> Json<Directory>;
}

#[derive(Serialize, Deserialize)]
pub struct Git {
    pub id: String,
}

impl From<types::Git> for Git {
    fn from(git: types::Git) -> Self {
        Git { id: git.id }
    }
}

impl Git {
    pub fn branch(&self, name: &str) -> Result<Git, Error> {
        unsafe { branch(name.into()) }?;
        Ok(Git {
            id: self.id.clone(),
        })
    }

    pub fn commit(&self) -> Result<String, Error> {
        unsafe { commit() }
    }

    pub fn tree(&self) -> Result<Directory, Error> {
        unsafe { tree() }.map(|directory| directory.into_inner())
    }
}