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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use std::{env, fs};
use std::path::PathBuf;

const CGOBIN: &'static str = "cgobin";

#[derive(Debug, Clone)]
pub struct Config {
    pub idl_file: PathBuf,
    /// Target crate directory for code generation
    pub target_crate_dir: Option<PathBuf>,
    pub go_root_path: Option<PathBuf>,
    pub go_mod_parent: &'static str,
}

/// unit-like struct path, e.g. `::mycrate::Abc`
#[derive(Debug, Clone)]
pub struct UnitLikeStructPath(pub &'static str);

#[derive(Debug, Clone)]
pub struct GoObjectPath {
    /// e.g. `github.com/xxx/mypkg`
    pub import: String,
    /// e.g. `mypkg.Abc`
    pub object_ident: String,
}

impl Config {
    pub(crate) fn pkg_dir(&self) -> PathBuf {
        if let Some(target_crate_dir) = &self.target_crate_dir {
            target_crate_dir.clone()
        } else {
            env::var("CARGO_MANIFEST_DIR").unwrap().into()
        }
    }
    fn pkg_name_prefix(&self) -> String {
        let idl_name = self.idl_file.file_name().unwrap().to_str().unwrap();
        idl_name
            .rsplit_once(".")
            .map_or(idl_name, |(idl_name, _)| idl_name)
            .replace(".", "_")
            .replace("-", "_")
            .trim_start_matches("_")
            .to_string()
            .trim_end_matches("_")
            .to_string()
    }
    fn file_name_base(&self) -> String {
        let pkg_name_prefix = self.pkg_name_prefix();
        format!("{pkg_name_prefix}_gen")
    }
    pub(crate) fn go_mod_name(&self) -> String {
        self.pkg_dir()
            .file_name()
            .unwrap()
            .to_str()
            .unwrap()
            .replace(".", "_")
            .replace("-", "_")
            .trim_start_matches("_")
            .to_string()
            .trim_end_matches("_")
            .to_string()
    }
    pub(crate) fn go_mod_path(&self) -> String {
        format!(
            "{}/{}",
            self.go_mod_parent.trim_end_matches("/"),
            self.go_mod_name()
        )
    }
    pub(crate) fn go_cmd_path(&self, cmd: &'static str) -> String {
        if let Some(go_root_path) = &self.go_root_path {
            go_root_path
                .join("bin")
                .join(cmd)
                .to_str()
                .unwrap()
                .to_string()
        } else {
            cmd.to_string()
        }
    }
    pub(crate) fn rust_mod_file(&self) -> PathBuf {
        self.pkg_dir()
            .join("src")
            .join(self.file_name_base() + ".rs")
    }
    pub(crate) fn rust_impl_file(&self) -> PathBuf {
        self.pkg_dir()
            .join("src")
            .join(self.pkg_name_prefix() + "_impl.rs")
    }
    pub(crate) fn rust_impl_name(&self) -> &'static str {
        "ImplFfi"
    }
    pub(crate) fn rust_mod_path(&self) -> String {
        format!("crate::{}", self.file_name_base())
    }
    pub(crate) fn go_mod_file(&self) -> PathBuf {
        self.pkg_dir().join("go.mod")
    }
    pub(crate) fn go_lib_file(&self) -> PathBuf {
        self.pkg_dir().join(self.file_name_base() + ".go")
    }
    pub(crate) fn go_main_dir(&self) -> PathBuf {
        self.pkg_dir().join(CGOBIN)
    }
    pub(crate) fn go_main_file(&self) -> PathBuf {
        self.go_main_dir().join("clib_goffi_gen.go")
    }
    pub(crate) fn go_main_impl_file(&self) -> PathBuf {
        self.go_main_dir().join("clib_goffi_impl.go")
    }
    pub(crate) fn new_crate_modified(&self) -> String {
        walkdir::WalkDir::new(self.pkg_dir())
            .into_iter()
            .filter_map(|entry| entry.ok())
            .filter(|entry| {
                if entry
                    .path()
                    .extension()
                    .map(|ext| ext == "go" || ext == "rs" || ext == "toml" || ext == "proto")
                    .unwrap_or_default()
                {
                    if let Ok(metadata) = entry.metadata() {
                        return metadata.is_file();
                    }
                };
                return false;
            })
            .fold(String::new(), |acc, m| {
                let digest = md5::compute(fs::read(m.path()).unwrap());
                format!("{acc}|{digest:x}")
            })
    }
}