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

Fix compile errors in examples and docs in valuable crate #95

Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions valuable/examples/derive.rs
@@ -1,9 +1,12 @@
#[cfg(feature = "valuable-derive")]
alexanderkjall marked this conversation as resolved.
Show resolved Hide resolved
use valuable::Valuable;

#[cfg(feature = "valuable-derive")]
use std::collections::HashMap;

// `Debug` not implemented for struct, the debug implementation is going via
// valuable.
#[cfg(feature = "valuable-derive")]
#[derive(Valuable)]
struct Person {
name: String,
Expand All @@ -12,6 +15,7 @@ struct Person {
favorites: HashMap<String, String>,
}

#[cfg(feature = "valuable-derive")]
fn main() {
let mut p = Person {
name: "John Doe".to_string(),
Expand All @@ -24,3 +28,6 @@ fn main() {

println!("{:#?}", p.as_value());
}

#[cfg(not(feature = "valuable-derive",))]
fn main() {}
10 changes: 10 additions & 0 deletions valuable/examples/print.rs
@@ -1,13 +1,17 @@
#[cfg(feature = "valuable-derive")]
use valuable::{NamedValues, Valuable, Value, Visit};

#[cfg(feature = "valuable-derive")]
struct Print(String);

#[cfg(feature = "valuable-derive")]
impl Print {
fn indent(&self) -> Print {
Print(format!("{} ", self.0))
}
}

#[cfg(feature = "valuable-derive")]
impl Visit for Print {
fn visit_value(&mut self, value: Value<'_>) {
match value {
Expand Down Expand Up @@ -69,20 +73,23 @@ impl Visit for Print {
}
}

#[cfg(feature = "valuable-derive")]
#[derive(Valuable)]
struct Person {
name: String,
age: u32,
addresses: Vec<Address>,
}

#[cfg(feature = "valuable-derive")]
#[derive(Valuable)]
struct Address {
street: String,
city: String,
zip: String,
}

#[cfg(feature = "valuable-derive")]
fn main() {
let person = Person {
name: "Angela Ashton".to_string(),
Expand All @@ -104,3 +111,6 @@ fn main() {
let mut print = Print("".to_string());
valuable::visit(&person, &mut print);
}

#[cfg(not(feature = "valuable-derive",))]
fn main() {}
45 changes: 45 additions & 0 deletions valuable/src/enumerable.rs
Expand Up @@ -25,6 +25,9 @@ use core::fmt;
/// [`visit_named_fields()`]: Visit::visit_named_fields
/// [`visit_unnamed_fields()`]: Visit::visit_unnamed_fields
///
#[cfg_attr(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is fine that documentation or tests require all features. See rust-lang/futures-rs#2216 for more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The background of this patch was that the Debian build system does build crates with the different features both enabled and not enabled, so in order to avoid errors there i wrote this patch: https://salsa.debian.org/rust-team/debcargo-conf/-/blob/master/src/valuable/debian/patches/fix-example-and-doc-compile-errors.patch

And I thought that I should also provide it upstream to be a good community member :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to follow up, does this mean that you are not interested in this PR? That isn't a big problem for me, we can just close it if it won't get merged.

feature = "valuable-derive",
doc = r##"
/// ```
/// use valuable::{Valuable, Value, Visit};
///
Expand Down Expand Up @@ -58,6 +61,8 @@ use core::fmt;
///
/// valuable::visit(&my_enum, &mut PrintVariant);
/// ```
"##
)]
///
/// If the enum is **statically** defined, then all variants, and variant fields
/// are known ahead of time and may be accessed via the [`EnumDef`] instance
Expand All @@ -70,6 +75,9 @@ use core::fmt;
/// Implementing `Enumerable` is usually done by adding `#[derive(Valuable)]` to
/// a Rust `enum` definition.
///
#[cfg_attr(
feature = "valuable-derive",
doc = r##"
/// ```
/// use valuable::{Valuable, Enumerable, EnumDef};
///
Expand All @@ -93,13 +101,18 @@ use core::fmt;
/// assert_eq!("Foo", variants[0].name());
/// assert!(variants[0].fields().is_unnamed());
/// ```
"##
)]
pub trait Enumerable: Valuable {
/// Returns the enum's definition.
///
/// See [`EnumDef`] documentation for more details.
///
/// # Examples
///
#[cfg_attr(
feature = "valuable-derive",
doc = r##"
/// ```
/// use valuable::{Enumerable, Valuable};
///
Expand All @@ -113,12 +126,17 @@ pub trait Enumerable: Valuable {
///
/// assert_eq!("MyEnum", my_enum.definition().name());
/// ```
"##
)]
fn definition(&self) -> EnumDef<'_>;

/// Returns the `enum`'s current variant.
///
/// # Examples
///
#[cfg_attr(
feature = "valuable-derive",
doc = r##"
/// ```
/// use valuable::{Enumerable, Valuable};
///
Expand All @@ -131,6 +149,8 @@ pub trait Enumerable: Valuable {
/// let my_enum = MyEnum::Foo;
/// assert_eq!("Foo", my_enum.variant().name());
/// ```
"##
)]
fn variant(&self) -> Variant<'_>;
}

Expand All @@ -151,6 +171,9 @@ pub enum EnumDef<'a> {
///
/// A statically defined enum
///
#[cfg_attr(
feature = "valuable-derive",
doc = r##"
/// ```
/// use valuable::{Valuable, Enumerable, EnumDef};
///
Expand All @@ -174,6 +197,8 @@ pub enum EnumDef<'a> {
/// assert_eq!("Foo", variants[0].name());
/// assert_eq!("Bar", variants[1].name());
/// ```
"##
)]
#[non_exhaustive]
Static {
/// The enum's name
Expand Down Expand Up @@ -305,6 +330,9 @@ impl<'a> EnumDef<'a> {
///
/// # Examples
///
#[cfg_attr(
feature = "valuable-derive",
doc = r##"
/// ```
/// use valuable::{Enumerable, Valuable};
///
Expand All @@ -317,6 +345,8 @@ impl<'a> EnumDef<'a> {
/// let def = Foo::Bar.definition();
/// assert_eq!("Foo", def.name());
/// ```
"##
)]
pub fn name(&self) -> &str {
match self {
EnumDef::Static { name, .. } => name,
Expand All @@ -328,6 +358,9 @@ impl<'a> EnumDef<'a> {
///
/// # Examples
///
#[cfg_attr(
feature = "valuable-derive",
doc = r##"
/// ```
/// use valuable::{Enumerable, Valuable};
///
Expand All @@ -343,6 +376,8 @@ impl<'a> EnumDef<'a> {
/// assert_eq!(2, variants.len());
/// assert_eq!("Bar", variants[0].name());
/// ```
"##
)]
pub fn variants(&self) -> &[VariantDef<'_>] {
match self {
EnumDef::Static { variants, .. } => variants,
Expand All @@ -356,6 +391,9 @@ impl<'a> EnumDef<'a> {
///
/// With a static enum
///
#[cfg_attr(
feature = "valuable-derive",
doc = r##"
/// ```
/// use valuable::{Enumerable, Valuable};
///
Expand All @@ -368,6 +406,8 @@ impl<'a> EnumDef<'a> {
/// let def = Foo::Bar.definition();
/// assert!(def.is_static());
/// ```
"##
)]
///
/// With a dynamic enum
///
Expand All @@ -387,6 +427,9 @@ impl<'a> EnumDef<'a> {
///
/// With a static enum
///
#[cfg_attr(
feature = "valuable-derive",
doc = r##"
/// ```
/// use valuable::{Enumerable, Valuable};
///
Expand All @@ -399,6 +442,8 @@ impl<'a> EnumDef<'a> {
/// let def = Foo::Bar.definition();
/// assert!(!def.is_dynamic());
/// ```
"##
)]
///
/// With a dynamic enum
///
Expand Down
10 changes: 10 additions & 0 deletions valuable/src/lib.rs
Expand Up @@ -11,6 +11,9 @@
//!
//! First, derive [`Valuable`][macro@crate::Valuable] on your types.
//!
#![cfg_attr(
feature = "valuable-derive",
doc = r##"
//! ```
//! use valuable::Valuable;
//!
Expand All @@ -25,6 +28,8 @@
//! Custom(String),
//! }
//! ```
"##
)]
//!
//! Then, implement a [visitor][Visit] to inspect the data.
//!
Expand Down Expand Up @@ -81,6 +86,9 @@
//!
//! Then, use the visitor to visit the value.
//!
#![cfg_attr(
feature = "valuable-derive",
doc = r##"
//! ```
//! # use valuable::*;
//! # #[derive(Valuable)]
Expand All @@ -94,6 +102,8 @@
//! let hello_world = HelloWorld { message: Message::HelloWorld };
//! hello_world.visit(&mut Print);
//! ```
"##
)]
//!
//! # Related Crates
//!
Expand Down
21 changes: 21 additions & 0 deletions valuable/src/structable.rs
Expand Up @@ -17,6 +17,9 @@ use core::fmt;
/// may be called multiple times per `Structable`, but the two methods are never
/// mixed.
///
#[cfg_attr(
feature = "valuable-derive",
doc = r##"
/// ```
/// use valuable::{NamedValues, Valuable, Value, Visit};
///
Expand Down Expand Up @@ -50,6 +53,8 @@ use core::fmt;
///
/// valuable::visit(&my_struct, &mut PrintFields);
/// ```
"##
)]
///
/// If the struct is **statically** defined, then all fields are known ahead of
/// time and may be accessed via the [`StructDef`] instance returned by
Expand All @@ -61,6 +66,9 @@ use core::fmt;
/// Implementing `Structable` is usually done by adding `#[derive(Valuable)]` to
/// a Rust `struct` definition.
///
#[cfg_attr(
feature = "valuable-derive",
doc = r##"
/// ```
/// use valuable::{Fields, Valuable, Structable, StructDef};
///
Expand All @@ -86,6 +94,8 @@ use core::fmt;
/// _ => unreachable!(),
/// }
/// ```
"##
)]
///
/// [`definition()`]: Structable::definition()
pub trait Structable: Valuable {
Expand All @@ -95,6 +105,9 @@ pub trait Structable: Valuable {
///
/// # Examples
///
#[cfg_attr(
feature = "valuable-derive",
doc = r##"
/// ```
/// use valuable::{Structable, Valuable};
///
Expand All @@ -108,6 +121,9 @@ pub trait Structable: Valuable {
/// };
///
/// assert_eq!("MyStruct", my_struct.definition().name());
/// ```
"##
)]
fn definition(&self) -> StructDef<'_>;
}

Expand All @@ -129,6 +145,9 @@ pub enum StructDef<'a> {
///
/// A statically defined struct
///
#[cfg_attr(
feature = "valuable-derive",
doc = r##"
/// ```
/// use valuable::{Fields, Valuable, Structable, StructDef};
///
Expand All @@ -154,6 +173,8 @@ pub enum StructDef<'a> {
/// _ => unreachable!(),
/// }
/// ```
"##
)]
#[non_exhaustive]
Static {
/// The struct's name.
Expand Down