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

Hide helper traits from calling code #7

Merged
merged 4 commits into from
Nov 10, 2019
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
on: [push]
on: [push, pull_request]

name: Continuous integration

Expand Down
50 changes: 23 additions & 27 deletions src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,48 @@ use quote::{format_ident, quote};
use syn::{Data, DataEnum, DataStruct, DeriveInput, Error, Fields, Result};

pub fn derive(input: &DeriveInput) -> Result<TokenStream> {
match &input.data {
let impls = match &input.data {
Data::Struct(data) => impl_struct(input, data),
Data::Enum(data) => impl_enum(input, data),
Data::Union(_) => Err(Error::new_spanned(input, "Unions are not supported")),
}
}?;

let helpers = specialization();
let dummy_const = format_ident!("_DERIVE_Display_FOR_{}", input.ident);
Ok(quote! {
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
const #dummy_const: () = {
#helpers
#impls
};
})
}

#[cfg(feature = "std")]
fn specialization() -> TokenStream {
quote! {
trait DisplayToDisplayDoc {
fn get_display(&self) -> Self;
fn __displaydoc_display(&self) -> &Self;
}

impl<T: core::fmt::Display> DisplayToDisplayDoc for &T {
fn get_display(&self) -> Self {
impl<T: core::fmt::Display> DisplayToDisplayDoc for T {
fn __displaydoc_display(&self) -> &Self {
self
}
}

trait PathToDisplayDoc {
fn get_display(&self) -> std::path::Display<'_>;
fn __displaydoc_display(&self) -> std::path::Display<'_>;
}

impl PathToDisplayDoc for std::path::Path {
fn get_display(&self) -> std::path::Display<'_> {
fn __displaydoc_display(&self) -> std::path::Display<'_> {
self.display()
}
}

impl PathToDisplayDoc for std::path::PathBuf {
fn get_display(&self) -> std::path::Display<'_> {
fn __displaydoc_display(&self) -> std::path::Display<'_> {
self.display()
}
}
Expand Down Expand Up @@ -74,13 +84,7 @@ fn impl_struct(input: &DeriveInput, data: &DataStruct) -> Result<TokenStream> {
}
});

let needed_traits = specialization();

Ok(quote! {
#needed_traits

#display
})
Ok(quote! { #display })
}

fn impl_enum(input: &DeriveInput, data: &DataEnum) -> Result<TokenStream> {
Expand All @@ -93,7 +97,7 @@ fn impl_enum(input: &DeriveInput, data: &DataEnum) -> Result<TokenStream> {
.map(|variant| attr::display(&variant.attrs))
.collect::<Result<Vec<_>>>()?;

let display = if displays.iter().any(Option::is_some) {
if displays.iter().any(Option::is_some) {
let arms = data
.variants
.iter()
Expand All @@ -115,7 +119,7 @@ fn impl_enum(input: &DeriveInput, data: &DataEnum) -> Result<TokenStream> {
})
})
.collect::<Result<Vec<_>>>()?;
Some(quote! {
Ok(quote! {
impl #impl_generics core::fmt::Display for #ty #ty_generics #where_clause {
fn fmt(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
#[allow(unused_variables)]
Expand All @@ -126,14 +130,6 @@ fn impl_enum(input: &DeriveInput, data: &DataEnum) -> Result<TokenStream> {
}
})
} else {
return Err(Error::new_spanned(input, "Missing doc comments"));
};

let needed_traits = specialization();

Ok(quote! {
#needed_traits

#display
})
Err(Error::new_spanned(input, "Missing doc comments"))
}
}
39 changes: 28 additions & 11 deletions src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ use proc_macro2::TokenStream;
use quote::quote_spanned;
use syn::{Ident, LitStr};

#[cfg(feature = "std")]
const IS_STD: bool = true;
#[cfg(not(feature = "std"))]
const IS_STD: bool = false;

macro_rules! peek_next {
($read:ident) => {
match $read.chars().next() {
Expand Down Expand Up @@ -49,8 +44,8 @@ impl Display {

let next = peek_next!(read);

let arg = if IS_STD && next == '}' {
quote_spanned!(span=> , (&#ident).get_display())
let arg = if cfg!(feature = "std") && next == '}' {
quote_spanned!(span=> , #ident.__displaydoc_display())
} else {
quote_spanned!(span=> , #ident)
};
Expand Down Expand Up @@ -120,11 +115,33 @@ mod tests {
assert(
"{v} {v:?} {0} {0:?}",
"{} {:?} {} {:?}",
", ( & v ) . get_display ( ) , v , ( & _0 ) . get_display ( ) , _0",
", v . __displaydoc_display ( ) , v , _0 . __displaydoc_display ( ) , _0",
);
assert(
"error {var}",
"error {}",
", var . __displaydoc_display ( )",
);

assert(
"The path {0}",
"The path {}",
", _0 . __displaydoc_display ( )",
);
assert("The path {0:?}", "The path {:?}", ", _0");
}

#[test]
#[cfg_attr(feature = "std", ignore)]
fn test_nostd_expand() {
assert(
"{v} {v:?} {0} {0:?}",
"{} {:?} {} {:?}",
", v , v , _0 , _0",
);
assert("error {var}", "error {}", ", ( & var ) . get_display ( )");
assert("error {var}", "error {}", ", var");

// assert("The path {0.display()}", "The path {}", "0.display()");
// assert("The path {0.display():?}", "The path {:?}", "0.display()");
assert("The path {0}", "The path {}", ", _0");
assert("The path {0:?}", "The path {:?}", ", _0");
}
}
31 changes: 24 additions & 7 deletions tests/happy.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
use displaydoc::Display;
// use std::path::PathBuf;

#[cfg(feature = "std")]
use std::path::PathBuf;

#[derive(Display)]
/// Just a basic struct {thing}
struct HappyStruct {
thing: &'static str,
}

#[derive(Display)]
enum Happy {
Expand All @@ -18,8 +26,10 @@ enum Happy {
/// Variant5 just has {0} many problems
/// but multi line comments aren't one of them
Variant5(u32),
// /// The path {0.display()}
// Variant6(PathBuf),

/// The path {0}
#[cfg(feature = "std")]
Variant6(PathBuf),
}

fn assert_display<T: std::fmt::Display>(input: T, expected: &'static str) {
Expand All @@ -37,8 +47,15 @@ fn does_it_print() {
"Variant4 wants to have a lot of lines\n\n Lets see how this works out for it",
);
assert_display(Happy::Variant5(2), "Variant5 just has 2 many problems");
// assert_display(
// Happy::Variant6(PathBuf::from("/var/log/happy")),
// "The path /var/log/happy",
// );

assert_display(HappyStruct { thing: "hi" }, "Just a basic struct hi");
}

#[test]
#[cfg(feature = "std")]
fn does_it_print_path() {
assert_display(
Happy::Variant6(PathBuf::from("/var/log/happy")),
"The path /var/log/happy",
);
}