Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeNameInfo gated behind "info" and "serde" features. #16

Merged
merged 3 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 0.1.10 (2024-02-12)

* Add `TypeNameInfo` gated behind `"info"` and `"serde"` features. ([#16])

[#16]: https://github.com/azriel91/tynm/pulls/16


## 0.1.9 (2023-09-28)

* Add `tynm::type_name*_opts` methods, taking in `TypeParamsFmtOpts` to specify how type parameters are formatted. ([#15])
Expand Down
6 changes: 5 additions & 1 deletion Cargo.toml
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."
Expand All @@ -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" }
Expand Down
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@ Returns type names with a specifiable number of module segments as a `String`.
Add the following to `Cargo.toml`

```toml
tynm = "0.1.8"
tynm = "0.1.10"
```

In code:

```rust
#[rustfmt::skip]
assert_eq!(
core::any::type_name::<Option<String>>(), "core::option::Option<alloc::string::String>"
);

#[rustfmt::skip]
let tuples = vec![
(tynm::type_name::<Option<String>>(), "Option<String>"),
(tynm::type_namem::<Option<String>>(1), "core::..::Option<alloc::..::String>"),
Expand All @@ -26,26 +32,24 @@ let tuples = vec![
(tynm::type_namemn::<rust_out::two::three::Struct>(1, 1), "rust_out::..::three::Struct"),
// traits
(tynm::type_name::<dyn core::fmt::Debug>(), "dyn Debug"),

// core / std
(core::any::type_name::<Option<String>>(), "core::option::Option<alloc::string::String>")
];

tuples
.iter()
.for_each(|(left, right)| assert_eq!(left, right));

```

## Motivation

The [`std::any::type_name`] function stabilized in Rust 1.38 returns the fully qualified type
The [`core::any::type_name`] function stabilized in Rust 1.38 returns the fully qualified type
name with all module segments. This can be difficult to read in error messages, especially for
type-parameterized types.

Often, the simple type name is more readable, and enough to distinguish the type referenced in
an error.

[`std::any::type_name`]: https://doc.rust-lang.org/std/any/fn.type_name.html
[`core::any::type_name`]: https://doc.rust-lang.org/std/any/fn.type_name.html

## License

Expand Down
4 changes: 2 additions & 2 deletions README.tpl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# 🪶 Tynm -- Type Name

[![Crates.io](https://img.shields.io/crates/v/tynm.svg)](https://crates.io/crates/tynm)
[![docs.rs](https://img.shields.io/docsrs/tynm)](https://docs.rs/tynm)
[![CI](https://github.com/azriel91/tynm/workflows/CI/badge.svg)](https://github.com/azriel91/tynm/actions/workflows/ci.yml)
[![Coverage Status](https://codecov.io/gh/azriel91/tynm/branch/main/graph/badge.svg)](https://codecov.io/gh/azriel91/tynm)

# Tynm -- Type Name

{{readme}}

## License
Expand Down
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//! Add the following to `Cargo.toml`
//!
//! ```toml
//! tynm = "0.1.8"
//! tynm = "0.1.10"
//! ```
//!
//! In code:
Expand Down Expand Up @@ -60,10 +60,16 @@ pub use crate::{
types::{TypeName, TypeNameDisplay},
};

#[cfg(feature = "info")]
pub use crate::type_name_info::TypeNameInfo;

mod parser;
mod type_params_fmt_opts;
mod types;

#[cfg(feature = "info")]
mod type_name_info;

/// Returns the simple type name.
///
/// # Type Parameters
Expand Down
86 changes: 86 additions & 0 deletions src/type_name_info.rs
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(())
}
}
Loading