nushell/crates/nu-protocol/src/type_name.rs

45 lines
1.3 KiB
Rust
Raw Normal View History

use nu_source::{DebugDocBuilder, HasSpan, Spanned, SpannedItem, Tagged};
2019-11-04 16:47:03 +01:00
2020-01-18 20:42:36 +01:00
/// A trait that allows structures to define a known .type_name() which pretty-prints the type
2019-11-04 16:47:03 +01:00
pub trait ShellTypeName {
fn type_name(&self) -> &'static str;
}
impl<T: ShellTypeName> ShellTypeName for Spanned<T> {
2020-01-18 20:42:36 +01:00
/// Return the type_name of the spanned item
fn type_name(&self) -> &'static str {
self.item.type_name()
}
}
2019-11-04 16:47:03 +01:00
impl<T: ShellTypeName> ShellTypeName for &T {
2020-01-18 20:42:36 +01:00
/// Return the type_name for the borrowed reference
2019-11-04 16:47:03 +01:00
fn type_name(&self) -> &'static str {
(*self).type_name()
}
}
2020-01-18 20:42:36 +01:00
/// A trait that allows structures to define a known way to return a spanned type name
2019-11-04 16:47:03 +01:00
pub trait SpannedTypeName {
fn spanned_type_name(&self) -> Spanned<&'static str>;
}
impl<T: ShellTypeName + HasSpan> SpannedTypeName for T {
2020-01-18 20:42:36 +01:00
/// Return the type name as a spanned string
2019-11-04 16:47:03 +01:00
fn spanned_type_name(&self) -> Spanned<&'static str> {
self.type_name().spanned(self.span())
2019-11-04 16:47:03 +01:00
}
}
impl<T: ShellTypeName> SpannedTypeName for Tagged<T> {
2020-01-18 20:42:36 +01:00
/// Return the spanned type name for a Tagged value
2019-11-04 16:47:03 +01:00
fn spanned_type_name(&self) -> Spanned<&'static str> {
self.item.type_name().spanned(self.tag.span)
}
}
2019-07-24 00:22:11 +02:00
2020-01-18 20:42:36 +01:00
/// A trait to enable pretty-printing of type information
2019-11-04 16:47:03 +01:00
pub trait PrettyType {
fn pretty_type(&self) -> DebugDocBuilder;
}