1use extism_pdk::*;
2use fluentci_types::file::{self as types, Chmod};
3use serde::{Deserialize, Serialize};
4
5use super::directory::Directory;
6
7#[host_fn]
8extern "ExtismHost" {
9 fn zip(path: String) -> Json<File>;
10 fn unzip(path: String) -> Json<Directory>;
11 fn tar_czvf(path: String) -> Json<File>;
12 fn tar_xzvf(path: String) -> Json<Directory>;
13 fn md5(path: String) -> String;
14 fn sha256(path: String) -> String;
15 fn chmod(opt: Json<Chmod>) -> Json<File>;
16}
17
18#[derive(Serialize, Deserialize)]
19pub struct File {
20 pub id: String,
21 pub path: String,
22}
23
24impl From<types::File> for File {
25 fn from(file: types::File) -> Self {
26 File {
27 id: file.id,
28 path: file.path,
29 }
30 }
31}
32
33impl File {
34 pub fn zip(&self) -> Result<File, Error> {
35 let file = unsafe { zip(self.path.clone()) }?;
36 Ok(file.into_inner())
37 }
38
39 pub fn unzip(&self) -> Result<Directory, Error> {
40 let directory = unsafe { unzip(self.path.clone()) }?;
41 Ok(directory.into_inner())
42 }
43
44 pub fn tar_czvf(&self) -> Result<File, Error> {
45 let file = unsafe { tar_czvf(self.path.clone()) }?;
46 Ok(file.into_inner())
47 }
48
49 pub fn tar_xzvf(&self) -> Result<Directory, Error> {
50 let directory = unsafe { tar_xzvf(self.path.clone()) }?;
51 Ok(directory.into_inner())
52 }
53
54 pub fn md5(&self) -> Result<String, Error> {
55 unsafe { md5(self.path.clone()) }
56 }
57
58 pub fn sha256(&self) -> Result<String, Error> {
59 unsafe { sha256(self.path.clone()) }
60 }
61
62 pub fn chmod(&self, mode: &str) -> Result<File, Error> {
63 let options = Chmod {
64 path: self.path.clone(),
65 mode: mode.to_string(),
66 };
67 let file = unsafe { chmod(Json::from(options)) }?;
68 Ok(file.into_inner())
69 }
70}