elf-magic 0.3.1

Automatic compile-time ELF exports for Solana programs. One-liner integration, zero config, just works. ✨
Documentation
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use std::{fs, path::Path};

use serde::{Deserialize, Serialize};

use crate::error::Error;

/// Configuration for elf-magic from package.metadata.elf-magic
///
/// Clean three-mode system: Magic (default single workspace) vs Permissive (multi-workspace with excludes) vs Laser Eyes (multi-workspace with includes)
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(tag = "mode", rename_all = "kebab-case")]
pub enum Config {
    #[serde(rename = "magic")]
    Magic, // No fields! Just "run cargo metadata here"

    #[serde(rename = "laser-eyes")]
    LaserEyes {
        workspaces: Vec<LaserEyesWorkspaceConfig>,
    },

    #[serde(rename = "permissive")]
    Permissive {
        workspaces: Vec<PermissiveWorkspaceConfig>,
        #[serde(default)]
        global_deny: Vec<String>,
    },
}

impl Config {
    pub fn load(manifest_dir: &Path) -> Result<Self, Error> {
        let manifest_path = manifest_dir.join("Cargo.toml");
        let content = fs::read_to_string(&manifest_path).map_err(|e| {
            let message = format!("Failed to read Cargo.toml: {}", e);
            Error::Config(message)
        })?;

        let toml_value: toml::Value = toml::from_str(&content).map_err(|e| {
            let message = format!("Invalid TOML in Cargo.toml: {}", e);
            Error::Config(message)
        })?;

        // Extract package.metadata.elf-magic, default to Magic mode if not present
        let metadata = toml_value
            .get("package")
            .and_then(|p| p.get("metadata"))
            .and_then(|m| m.get("elf-magic"));

        match metadata {
            Some(config_value) => {
                let json_value = serde_json::to_value(config_value).map_err(|e| {
                    let message = format!("Failed to convert config: {}", e);
                    Error::Config(message)
                })?;
                serde_json::from_value(json_value).map_err(|e| {
                    let message = format!("Invalid elf-magic config: {}", e);
                    Error::Config(message)
                })
            }
            None => Ok(Config::Magic),
        }
    }
}

impl Default for Config {
    fn default() -> Self {
        Self::Magic
    }
}

/// Configuration for a single workspace in laser-eyes mode
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct LaserEyesWorkspaceConfig {
    pub manifest_path: String,
    pub only: Vec<String>,
}

/// Configuration for a single workspace in permissive mode
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct PermissiveWorkspaceConfig {
    pub manifest_path: String,
    #[serde(default)]
    #[serde(alias = "exclude")]
    pub deny: Vec<String>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    fn create_temp_manifest(content: &str) -> (TempDir, std::path::PathBuf) {
        let temp_dir = TempDir::new().unwrap();
        let manifest_path = temp_dir.path().join("Cargo.toml");
        fs::write(&manifest_path, content).unwrap();
        let path = temp_dir.path().to_path_buf();
        (temp_dir, path)
    }

    #[test]
    fn test_load_config_magic_mode_default() {
        let manifest_content = r#"
[package]
name = "test-package"
version = "0.1.0"
edition = "2021"
"#;

        let (_temp_dir, manifest_dir) = create_temp_manifest(manifest_content);
        let config = Config::load(&manifest_dir).unwrap();

        match config {
            Config::Magic => {}
            _ => panic!("Expected Magic mode"),
        }
    }

    #[test]
    fn test_load_config_magic_mode_explicit() {
        let manifest_content = r#"
[package]
name = "test-package"
version = "0.1.0"
edition = "2021"

[package.metadata.elf-magic]
mode = "magic"
"#;

        let (_temp_dir, manifest_dir) = create_temp_manifest(manifest_content);
        let config = Config::load(&manifest_dir).unwrap();

        match config {
            Config::Magic => {}
            _ => panic!("Expected Magic mode"),
        }
    }

    #[test]
    fn test_load_config_permissive_mode() {
        let manifest_content = r#"
[package]
name = "test-package"
version = "0.1.0"
edition = "2021"

[package.metadata.elf-magic]
mode = "permissive"
workspaces = [
    { manifest_path = "./Cargo.toml" },
    { manifest_path = "examples/basic/Cargo.toml", deny = ["target:test*"] }
]
"#;

        let (_temp_dir, manifest_dir) = create_temp_manifest(manifest_content);
        let config = Config::load(&manifest_dir).unwrap();

        match config {
            Config::Permissive {
                workspaces,
                global_deny,
            } => {
                assert_eq!(workspaces.len(), 2);
                assert_eq!(workspaces[0].manifest_path, "./Cargo.toml");
                assert_eq!(workspaces[0].deny.len(), 0);
                assert_eq!(workspaces[1].manifest_path, "examples/basic/Cargo.toml");
                assert_eq!(workspaces[1].deny, vec!["target:test*"]);
                assert_eq!(global_deny.len(), 0); // No global excludes in this test
            }
            _ => panic!("Expected Permissive mode"),
        }
    }

    #[test]
    fn test_load_config_permissive_mode_with_exclude_alias() {
        let manifest_content = r#"
[package]
name = "test-package"
version = "0.1.0"
edition = "2021"

[package.metadata.elf-magic]
mode = "permissive"
workspaces = [
    { manifest_path = "./Cargo.toml", exclude = ["target:test*", "package:dev*"] }
]
"#;

        let (_temp_dir, manifest_dir) = create_temp_manifest(manifest_content);
        let config = Config::load(&manifest_dir).unwrap();

        match config {
            Config::Permissive {
                workspaces,
                global_deny,
            } => {
                assert_eq!(workspaces.len(), 1);
                assert_eq!(workspaces[0].deny, vec!["target:test*", "package:dev*"]);
                assert_eq!(global_deny.len(), 0); // No global excludes in this test
            }
            _ => panic!("Expected Permissive mode"),
        }
    }

    #[test]
    fn test_load_config_missing_file() {
        let temp_dir = TempDir::new().unwrap();
        let non_existent = temp_dir.path().join("missing");

        let result = Config::load(&non_existent);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Failed to read Cargo.toml"));
    }

    #[test]
    fn test_load_config_invalid_toml() {
        let invalid_toml = r#"
[package
name = "invalid"
"#;

        let (_temp_dir, manifest_dir) = create_temp_manifest(invalid_toml);
        let result = Config::load(&manifest_dir);

        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Invalid TOML"));
    }

    #[test]
    fn test_load_config_invalid_elf_magic_config() {
        let manifest_content = r#"
[package]
name = "test-package"
version = "0.1.0"
edition = "2021"

[package.metadata.elf-magic]
mode = "invalid-mode"
"#;

        let (_temp_dir, manifest_dir) = create_temp_manifest(manifest_content);
        let result = Config::load(&manifest_dir);

        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Invalid elf-magic config"));
    }

    #[test]
    fn test_config_default() {
        let config = Config::default();
        match config {
            Config::Magic => {}
            _ => panic!("Default should be Magic mode"),
        }
    }

    #[test]
    fn test_workspace_config_defaults() {
        let manifest_content = r#"
[package]
name = "test-package"
version = "0.1.0"
edition = "2021"

[package.metadata.elf-magic]
mode = "permissive"
workspaces = [
    { manifest_path = "./Cargo.toml" }
]
"#;

        let (_temp_dir, manifest_dir) = create_temp_manifest(manifest_content);
        let config = Config::load(&manifest_dir).unwrap();

        match config {
            Config::Permissive {
                workspaces,
                global_deny,
            } => {
                assert_eq!(workspaces[0].deny.len(), 0); // Should default to empty
                assert_eq!(global_deny.len(), 0); // Should default to empty
            }
            _ => panic!("Expected Permissive mode"),
        }
    }

    #[test]
    fn test_load_config_with_global_exclude() {
        let manifest_content = r#"
[package]
name = "test-package"
version = "0.1.0"
edition = "2021"

[package.metadata.elf-magic]
mode = "permissive"
global_deny = ["package:apl-token", "package:apl-associated-token-account"]
workspaces = [
    { manifest_path = "./Cargo.toml" },
    { manifest_path = "examples/escrow/Cargo.toml", deny = ["target:test*"] }
]
"#;

        let (_temp_dir, manifest_dir) = create_temp_manifest(manifest_content);
        let config = Config::load(&manifest_dir).unwrap();

        match config {
            Config::Permissive {
                workspaces,
                global_deny,
            } => {
                assert_eq!(workspaces.len(), 2);
                assert_eq!(
                    global_deny,
                    vec!["package:apl-token", "package:apl-associated-token-account"]
                );

                // First workspace has no local excludes
                assert_eq!(workspaces[0].manifest_path, "./Cargo.toml");
                assert_eq!(workspaces[0].deny.len(), 0);

                // Second workspace has local excludes
                assert_eq!(workspaces[1].manifest_path, "examples/escrow/Cargo.toml");
                assert_eq!(workspaces[1].deny, vec!["target:test*"]);
            }
            _ => panic!("Expected Permissive mode"),
        }
    }

    #[test]
    fn test_load_config_laser_eyes_mode() {
        let manifest_content = r#"
[package]
name = "test-package"
version = "0.1.0"
edition = "2021"

[package.metadata.elf-magic]
mode = "laser-eyes"
workspaces = [
    { manifest_path = "./Cargo.toml", only = ["target:token_manager", "target:governance"] },
    { manifest_path = "examples/defi/Cargo.toml", only = ["target:swap*", "package:my-*-program"] }
]
"#;

        let (_temp_dir, manifest_dir) = create_temp_manifest(manifest_content);
        let config = Config::load(&manifest_dir).unwrap();

        match config {
            Config::LaserEyes { workspaces } => {
                assert_eq!(workspaces.len(), 2);

                // First workspace
                assert_eq!(workspaces[0].manifest_path, "./Cargo.toml");
                assert_eq!(
                    workspaces[0].only,
                    vec!["target:token_manager", "target:governance"]
                );

                // Second workspace
                assert_eq!(workspaces[1].manifest_path, "examples/defi/Cargo.toml");
                assert_eq!(
                    workspaces[1].only,
                    vec!["target:swap*", "package:my-*-program"]
                );
            }
            _ => panic!("Expected LaserEyes mode"),
        }
    }

    #[test]
    fn test_load_config_laser_eyes_mode_single_workspace() {
        let manifest_content = r#"
[package]
name = "test-package"
version = "0.1.0"
edition = "2021"

[package.metadata.elf-magic]
mode = "laser-eyes"
workspaces = [
    { manifest_path = "./Cargo.toml", only = ["target:my_program"] }
]
"#;

        let (_temp_dir, manifest_dir) = create_temp_manifest(manifest_content);
        let config = Config::load(&manifest_dir).unwrap();

        match config {
            Config::LaserEyes { workspaces } => {
                assert_eq!(workspaces.len(), 1);
                assert_eq!(workspaces[0].manifest_path, "./Cargo.toml");
                assert_eq!(workspaces[0].only, vec!["target:my_program"]);
            }
            _ => panic!("Expected LaserEyes mode"),
        }
    }

    #[test]
    fn test_load_config_laser_eyes_mode_empty_include() {
        let manifest_content = r#"
[package]
name = "test-package"
version = "0.1.0"
edition = "2021"

[package.metadata.elf-magic]
mode = "laser-eyes"
workspaces = [
    { manifest_path = "./Cargo.toml", only = [] }
]
"#;

        let (_temp_dir, manifest_dir) = create_temp_manifest(manifest_content);
        let config = Config::load(&manifest_dir).unwrap();

        match config {
            Config::LaserEyes { workspaces } => {
                assert_eq!(workspaces.len(), 1);
                assert_eq!(workspaces[0].manifest_path, "./Cargo.toml");
                assert_eq!(workspaces[0].only.len(), 0);
            }
            _ => panic!("Expected LaserEyes mode"),
        }
    }
}