Skip to content

Commit

Permalink
Create shared proc-macro primitives lib
Browse files Browse the repository at this point in the history
commit-id:4d5003fc
  • Loading branch information
maciektr committed Feb 5, 2024
1 parent 7c819ff commit 1925f5b
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ members = [
"extensions/scarb-snforge-test-collector",
"utils/create-output-dir",
"utils/scarb-build-metadata",
"utils/scarb-proc-macro-interface",
"utils/scarb-test-support",
"utils/scarb-ui",
"utils/test-for-each-example",
Expand Down
1 change: 1 addition & 0 deletions scarb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ windows-sys.workspace = true
xxhash-rust.workspace = true
zip.workspace = true
zstd.workspace = true
scarb-proc-macro-interface = { path = "../utils/scarb-proc-macro-interface" }

[target.'cfg(not(target_os = "linux"))'.dependencies]
reqwest = { workspace = true, default-features = true}
Expand Down
19 changes: 19 additions & 0 deletions utils/scarb-proc-macro-interface/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "scarb-proc-macro-interface"
version = "0.0.1"
edition.workspace = true

authors.workspace = true
categories = ["development-tools"]
description = "Cariro procedural macro interface primitives."
homepage.workspace = true
keywords = ["scarb"]
license.workspace = true
readme = "README.md"
repository.workspace = true

[dependencies]
anyhow.workspace = true
serde.workspace = true
serde_json.workspace = true
libc.workspace = true
1 change: 1 addition & 0 deletions utils/scarb-proc-macro-interface/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# scarb-proc-macro-interface
2 changes: 2 additions & 0 deletions utils/scarb-proc-macro-interface/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod plugin;
pub mod shared;
46 changes: 46 additions & 0 deletions utils/scarb-proc-macro-interface/src/plugin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::shared::{FfiProcMacroResult, FfiTokenStream};
use std::ffi::CString;

#[derive(Debug)]
#[allow(dead_code)]
pub enum ProcMacroResult {
/// Plugin has not taken any action.
Leave,
/// Plugin generated TokenStream replacement.
Replace(TokenStream),
/// Plugin ordered item removal.
Remove,
}

#[derive(Debug, Default, Clone)]
pub struct TokenStream(String);

impl FfiTokenStream {
/// # Safety
pub unsafe fn to_string(&self) -> String {
if self.0.is_null() {
String::default()
} else {
let cstr = CString::from_raw(self.0);
cstr.to_string_lossy().to_string()
}
}
}

impl TokenStream {
/// # Safety
pub unsafe fn to_ffi(&self) -> FfiTokenStream {
FfiTokenStream(CString::new(self.0.clone()).unwrap().into_raw())
}
}

impl ProcMacroResult {
/// # Safety
pub unsafe fn to_ffi(&self) -> FfiProcMacroResult {
match self {
Self::Leave => FfiProcMacroResult::Leave,
Self::Remove => FfiProcMacroResult::Remove,
Self::Replace(token_stream) => FfiProcMacroResult::Replace(token_stream.to_ffi()),
}
}
}
17 changes: 17 additions & 0 deletions utils/scarb-proc-macro-interface/src/shared.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::os::raw::c_char;

#[repr(C)]
#[derive(Debug)]
#[allow(dead_code)]
pub enum FfiProcMacroResult {
/// Plugin has not taken any action.
Leave,
/// Plugin generated TokenStream replacement.
Replace(FfiTokenStream),
/// Plugin ordered item removal.
Remove,
}

#[repr(C)]
#[derive(Debug)]
pub struct FfiTokenStream(pub *mut c_char);

0 comments on commit 1925f5b

Please sign in to comment.