Skip to content

Commit

Permalink
Finish up docs, improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperSonicHub1 committed Sep 1, 2024
1 parent 9ace865 commit b77a518
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 44 deletions.
166 changes: 123 additions & 43 deletions src/adapter/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1674,9 +1674,10 @@ fn enum_discriminants() {
Crate {
item {
... on Enum {
name @output
enum_name: name @output
variant {
discriminant {
variant_name: name @output
discriminant @optional {
value @output
}
}
Expand All @@ -1692,8 +1693,9 @@ fn enum_discriminants() {

#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, serde::Deserialize)]
struct Output {
name: String,
value: String,
enum_name: String,
variant_name: String,
value: Option<String>,
}

let mut results: Vec<Output> =
Expand All @@ -1706,81 +1708,159 @@ fn enum_discriminants() {
similar_asserts::assert_eq!(
vec![
Output {
name: "A".into(),
value: "0".into(),
enum_name: "A".into(),
variant_name: "Five".into(),
value: Some(
"100".into(),
),
},
Output {
name: "A".into(),
value: "1".into(),
enum_name: "A".into(),
variant_name: "Four".into(),
value: Some(
"99".into(),
),
},
Output {
name: "A".into(),
value: "100".into(),
enum_name: "A".into(),
variant_name: "One".into(),
value: Some(
"1".into(),
),
},
Output {
name: "A".into(),
value: "2".into(),
enum_name: "A".into(),
variant_name: "Three".into(),
value: Some(
"3".into(),
),
},
Output {
name: "A".into(),
value: "3".into(),
enum_name: "A".into(),
variant_name: "Two".into(),
value: Some(
"2".into(),
),
},
Output {
name: "A".into(),
value: "99".into(),
enum_name: "A".into(),
variant_name: "Zero".into(),
value: Some(
"0".into(),
),
},
Output {
name: "Fieldful".into(),
value: "0".into(),
enum_name: "Fieldful".into(),
variant_name: "Struct".into(),
value: Some(
"2".into(),
),
},
Output {
name: "Fieldful".into(),
value: "1".into(),
enum_name: "Fieldful".into(),
variant_name: "Tuple".into(),
value: Some(
"1".into(),
),
},
Output {
name: "Fieldful".into(),
value: "2".into(),
enum_name: "Fieldful".into(),
variant_name: "Unit".into(),
value: Some(
"0".into(),
),
},
Output {
name: "Fieldful".into(),
value: "9".into(),
enum_name: "Fieldful".into(),
variant_name: "Unit2".into(),
value: Some(
"9".into(),
),
},
Output {
name: "FieldlessWithDiscrimants".into(),
value: "10".into(),
enum_name: "FieldfulNoRepr".into(),
variant_name: "Struct".into(),
value: Some(
"2".into(),
),
},
Output {
name: "FieldlessWithDiscrimants".into(),
value: "11".into(),
enum_name: "FieldfulNoRepr".into(),
variant_name: "Tuple".into(),
value: Some(
"1".into(),
),
},
Output {
name: "FieldlessWithDiscrimants".into(),
value: "20".into(),
enum_name: "FieldfulNoRepr".into(),
variant_name: "Unit".into(),
value: Some(
"0".into(),
),
},
Output {
enum_name: "FieldlessWithDiscrimants".into(),
variant_name: "First".into(),
value: Some(
"10".into(),
),
},
Output {
name: "FieldlessWithDiscrimants".into(),
value: "21".into(),
enum_name: "FieldlessWithDiscrimants".into(),
variant_name: "Second".into(),
value: Some(
"20".into(),
),
},
Output {
name: "FieldlessWithDiscrimants".into(),
value: "22".into(),
enum_name: "FieldlessWithDiscrimants".into(),
variant_name: "Struct".into(),
value: Some(
"21".into(),
),
},
Output {
name: "Pathological".into(),
value: "-170141183460469231731687303715884105726".into(),
enum_name: "FieldlessWithDiscrimants".into(),
variant_name: "Tuple".into(),
value: Some(
"11".into(),
),
},
Output {
name: "Pathological".into(),
value: "-170141183460469231731687303715884105727".into(),
enum_name: "FieldlessWithDiscrimants".into(),
variant_name: "Unit".into(),
value: Some(
"22".into(),
),
},
Output {
name: "Pathological".into(),
value: "-170141183460469231731687303715884105728".into(),
enum_name: "Pathological".into(),
variant_name: "Max".into(),
value: Some(
"170141183460469231731687303715884105727".into(),
),
},
Output {
name: "Pathological".into(),
value: "170141183460469231731687303715884105727".into(),
}
enum_name: "Pathological".into(),
variant_name: "Min".into(),
value: Some(
"-170141183460469231731687303715884105728".into(),
),
},
Output {
enum_name: "Pathological".into(),
variant_name: "MinPlusOne".into(),
value: Some(
"-170141183460469231731687303715884105727".into(),
),
},
Output {
enum_name: "Pathological".into(),
variant_name: "MinPlusTwo".into(),
value: Some(
"-170141183460469231731687303715884105726".into(),
),
},
],
results
);
Expand Down
3 changes: 2 additions & 1 deletion src/adapter/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ impl<'a> Vertex<'a> {
}

pub(super) fn as_item(&self) -> Option<&'a Item> {
match self.kind {
match &self.kind {
VertexKind::Item(item) => Some(item),
VertexKind::Variant(variant) => Some(variant.item()),
_ => None,
}
}
Expand Down
57 changes: 57 additions & 0 deletions src/rustdoc_schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,25 @@ type PlainVariant implements Item & Variant {

# edges from Variant
field: [StructField!]

"""
The discriminant value of this variant, if its value is well-defined.
A discriminant is an integer value that is logically associated with an enum variant,
and is used to determine which variant an enum value holds.
Discriminant values are not always well-defined.
In order for variants to have well-defined discriminants, their enum must either
- define a primitive representation (e.g., `#[repr(u8)]`), or
- be "unit-only" i.e. consisting solely of unit variants.
The variants of such enums may be explicitly assigned a discriminant value, or
the discriminant may be defined implicitly:
- The first variant has a discriminant of zero, unless explicitly assigned a value.
- Subsequent variants have a discriminant one higher than the previous variant,
unless explicitly assigned a value.
"""
discriminant: Discriminant
}

Expand Down Expand Up @@ -569,6 +588,25 @@ type TupleVariant implements Item & Variant {

# edges from Variant
field: [StructField!]

"""
The discriminant value of this variant, if its value is well-defined.
A discriminant is an integer value that is logically associated with an enum variant,
and is used to determine which variant an enum value holds.
Discriminant values are not always well-defined.
In order for variants to have well-defined discriminants, their enum must either
- define a primitive representation (e.g., `#[repr(u8)]`), or
- be "unit-only" i.e. consisting solely of unit variants.
The variants of such enums may be explicitly assigned a discriminant value, or
the discriminant may be defined implicitly:
- The first variant has a discriminant of zero, unless explicitly assigned a value.
- Subsequent variants have a discriminant one higher than the previous variant,
unless explicitly assigned a value.
"""
discriminant: Discriminant
}

Expand Down Expand Up @@ -624,6 +662,25 @@ type StructVariant implements Item & Variant {

# edges from Variant
field: [StructField!]

"""
The discriminant value of this variant, if its value is well-defined.
A discriminant is an integer value that is logically associated with an enum variant,
and is used to determine which variant an enum value holds.
Discriminant values are not always well-defined.
In order for variants to have well-defined discriminants, their enum must either
- define a primitive representation (e.g., `#[repr(u8)]`), or
- be "unit-only" i.e. consisting solely of unit variants.
The variants of such enums may be explicitly assigned a discriminant value, or
the discriminant may be defined implicitly:
- The first variant has a discriminant of zero, unless explicitly assigned a value.
- Subsequent variants have a discriminant one higher than the previous variant,
unless explicitly assigned a value.
"""
discriminant: Discriminant
}

Expand Down
6 changes: 6 additions & 0 deletions test_crates/enum_discriminants/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ pub enum Fieldful {
Unit2 = 9
}

pub enum FieldfulNoRepr {
Unit,
Tuple(bool),
Struct{a: bool},
}

#[repr(i128)]
pub enum Pathological {
Min = i128::MIN,
Expand Down

0 comments on commit b77a518

Please sign in to comment.