-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #586 from rusterlium/serde
Import of serde_rustler
- Loading branch information
Showing
41 changed files
with
127,914 additions
and
19 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
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
Empty file.
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,56 @@ | ||
//! Constants and utilities for conversion between Rust string-likes and Elixir atoms. | ||
use crate::serde::Error; | ||
use crate::{types::atom::Atom, Encoder, Env, Term}; | ||
|
||
use lazy_static::lazy_static; | ||
|
||
lazy_static! { | ||
pub static ref OK: String = String::from("Ok"); | ||
pub static ref ERROR: String = String::from("Err"); | ||
} | ||
|
||
atoms! { | ||
nil, | ||
ok, | ||
error, | ||
true_ = "true", | ||
false_ = "false", | ||
undefined, | ||
nan, | ||
inf, | ||
neg_inf, | ||
__struct__, | ||
} | ||
|
||
/** | ||
* Attempts to create an atom term from the provided string (if the atom already exists in the atom table). If not, returns a string term. | ||
*/ | ||
pub fn str_to_term<'a>(env: &Env<'a>, string: &str) -> Result<Term<'a>, Error> { | ||
if string == "Ok" { | ||
Ok(ok().encode(*env)) | ||
} else if string == "Err" { | ||
Ok(error().encode(*env)) | ||
} else { | ||
match Atom::try_from_bytes(*env, string.as_bytes()) { | ||
Ok(Some(term)) => Ok(term.encode(*env)), | ||
Ok(None) => Err(Error::InvalidStringable), | ||
_ => Err(Error::InvalidStringable), | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Attempts to create a `String` from the term. | ||
*/ | ||
pub fn term_to_string(term: &Term) -> Result<String, Error> { | ||
if ok().eq(term) { | ||
Ok(OK.to_string()) | ||
} else if error().eq(term) { | ||
Ok(ERROR.to_string()) | ||
} else if term.is_atom() { | ||
term.atom_to_string().or(Err(Error::InvalidAtom)) | ||
} else { | ||
Err(Error::InvalidStringable) | ||
} | ||
} |
Oops, something went wrong.