-
Notifications
You must be signed in to change notification settings - Fork 3
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 #16 from azriel91/feature/type-name-data-type
- Loading branch information
Showing
6 changed files
with
117 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "tynm" | ||
version = "0.1.9" | ||
version = "0.1.10" | ||
authors = ["Azriel Hoh <[email protected]>"] | ||
edition = "2021" | ||
description = "Returns type names in shorter form." | ||
|
@@ -12,12 +12,16 @@ license = "MIT OR Apache-2.0" | |
|
||
[dependencies] | ||
nom = { version = "7.1.3", default-features = false, features = ["alloc"] } | ||
serde = { version = "1.0.196", optional = true, features = ["derive"] } | ||
|
||
[dev-dependencies] | ||
pretty_assertions = "1.4.0" | ||
serde_yaml = "0.9.31" | ||
|
||
[features] | ||
default = [] | ||
info = [] | ||
serde = ["dep:serde"] | ||
|
||
[badges] | ||
appveyor = { repository = "azriel91/tynm" } | ||
|
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,86 @@ | ||
use alloc::string::String; | ||
|
||
/// Holds both the short and full type names. | ||
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] | ||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
pub struct TypeNameInfo { | ||
/// The short type name, e.g. `"Option<String>"`. | ||
pub short_name: String, | ||
/// The full type name, e.g. `"core::option::Option<alloc::string::String>"`. | ||
pub full_name: String, | ||
} | ||
|
||
impl TypeNameInfo { | ||
/// Returns `TypeNameInfo` for the given `T`. | ||
pub fn new<T>() -> Self { | ||
let short_name = crate::type_name::<T>(); | ||
let full_name = String::from(core::any::type_name::<T>()); | ||
|
||
Self { | ||
short_name, | ||
full_name, | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use alloc::{format, string::String}; | ||
|
||
use super::TypeNameInfo; | ||
|
||
#[test] | ||
fn clone() { | ||
let type_name_info = TypeNameInfo::new::<Option<String>>(); | ||
|
||
let clone = Clone::clone(&type_name_info); | ||
|
||
assert_eq!(type_name_info, clone); | ||
} | ||
|
||
#[test] | ||
fn debug() { | ||
let type_name_info = TypeNameInfo::new::<Option<String>>(); | ||
|
||
assert_eq!( | ||
"TypeNameInfo { \ | ||
short_name: \"Option<String>\", \ | ||
full_name: \"core::option::Option<alloc::string::String>\" \ | ||
}", | ||
format!("{type_name_info:?}") | ||
); | ||
} | ||
|
||
#[cfg(feature = "serde")] | ||
#[test] | ||
fn serialize() -> Result<(), serde_yaml::Error> { | ||
let type_name_info = TypeNameInfo::new::<Option<String>>(); | ||
|
||
let serialized = serde_yaml::to_string(&type_name_info)?; | ||
|
||
assert_eq!( | ||
"\ | ||
short_name: Option<String>\n\ | ||
full_name: core::option::Option<alloc::string::String>\n\ | ||
", | ||
serialized | ||
); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cfg(feature = "serde")] | ||
#[test] | ||
fn deserialize() -> Result<(), serde_yaml::Error> { | ||
let deserialized = serde_yaml::from_str( | ||
"\ | ||
short_name: Option<String>\n\ | ||
full_name: core::option::Option<alloc::string::String>\n\ | ||
", | ||
)?; | ||
|
||
assert_eq!(TypeNameInfo::new::<Option<String>>(), deserialized); | ||
|
||
Ok(()) | ||
} | ||
} |