-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix allocator usage and implement simple module formatting
- Loading branch information
Showing
5 changed files
with
189 additions
and
15 deletions.
There are no files selected for viewing
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
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,62 @@ | ||
use crate::NifLibrary; | ||
use std::fmt; | ||
|
||
pub struct LibAsElixir(pub NifLibrary); | ||
|
||
impl fmt::Display for LibAsElixir { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
writeln!( | ||
f, | ||
"defmodule {} do", | ||
string_to_elixir_atom(&self.0.name, false) | ||
)?; | ||
|
||
for nif in &self.0.nifs { | ||
write!(f, " def {}(", string_to_elixir_atom(&nif.name, true))?; | ||
for i in 0..nif.arity { | ||
if i > 0 { | ||
write!(f, ", ")?; | ||
} | ||
write!(f, "_")?; | ||
} | ||
writeln!(f, "), do: :erlang.nif_error(not_loaded)")?; | ||
} | ||
writeln!(f, "end") | ||
} | ||
} | ||
|
||
fn string_to_elixir_atom(s: &str, func: bool) -> String { | ||
match s { | ||
"false" | "true" | "nil" => s.to_string(), | ||
_ if s.starts_with("Elixir.") => s[7..].to_string(), | ||
_ => { | ||
let mut output = String::new(); | ||
let mut needs_quotes = false; | ||
for c in s.chars() { | ||
match c { | ||
'a'..='z' | 'A'..='Z' | '0'..='9' | '@' | '_' => output.push(c), | ||
'"' => { | ||
needs_quotes = true; | ||
output.push_str("\\\""); | ||
} | ||
'\\' => { | ||
needs_quotes = true; | ||
output.push_str(r"\\"); | ||
} | ||
_ => { | ||
needs_quotes = true; | ||
output.push(c); | ||
} | ||
} | ||
} | ||
|
||
if needs_quotes { | ||
format!(":\"{}\"", output).to_string() | ||
} else if !func { | ||
format!(":{}", output).to_string() | ||
} else { | ||
output | ||
} | ||
} | ||
} | ||
} |
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,72 @@ | ||
use crate::NifLibrary; | ||
use std::fmt; | ||
|
||
pub struct LibAsErlang(pub NifLibrary); | ||
|
||
impl fmt::Display for LibAsErlang { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "-module({}).\n\n", string_to_erlang_atom(&self.0.name))?; | ||
writeln!(f, "-export([")?; | ||
let count = self.0.nifs.len(); | ||
for (n, nif) in self.0.nifs.iter().enumerate() { | ||
write!(f, " {}/{}", string_to_erlang_atom(&nif.name), nif.arity)?; | ||
if n == count - 1 { | ||
write!(f, ",")?; | ||
} | ||
writeln!(f)?; | ||
} | ||
write!(f, "]).\n\n")?; | ||
|
||
// TODO: On Load function | ||
|
||
for nif in &self.0.nifs { | ||
write!(f, "{}(", string_to_erlang_atom(&nif.name))?; | ||
for i in 0..nif.arity { | ||
if i > 0 { | ||
write!(f, ", ")?; | ||
} | ||
write!(f, "_")?; | ||
} | ||
write!(f, ") ->\n erlang:nif_error(not_loaded).\n\n")?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
fn string_to_erlang_atom(input: &str) -> String { | ||
let mut output = String::with_capacity(input.len()); | ||
let mut needs_quotes = false; | ||
|
||
let mut first = true; | ||
|
||
for c in input.chars() { | ||
match c { | ||
'A'..='Z' if first => { | ||
needs_quotes = true; | ||
output.push(c); | ||
} | ||
'a'..='z' | 'A'..='Z' | '0'..='9' | '@' | '_' => output.push(c), | ||
'\'' => { | ||
needs_quotes = true; | ||
output.push_str(r"\'"); | ||
} | ||
'\\' => { | ||
needs_quotes = true; | ||
output.push_str(r"\\"); | ||
} | ||
_ => { | ||
needs_quotes = true; | ||
output.push(c); | ||
} | ||
} | ||
|
||
first = false; | ||
} | ||
|
||
if needs_quotes { | ||
format!("'{}'", output).to_string() | ||
} else { | ||
output | ||
} | ||
} |