-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create shared proc-macro primitives lib
commit-id:4d5003fc
- Loading branch information
Showing
8 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# scarb-proc-macro-interface |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod plugin; | ||
pub mod shared; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |