From fc75c49213c6313ee14a87f143fd063f6cecf0bd Mon Sep 17 00:00:00 2001 From: eugenesvk Date: Fri, 6 Oct 2023 02:23:36 +0700 Subject: [PATCH 01/23] add Constant property resolution function allow reading constant value/expr/is_literal and type (this one serialized to a json string since there is no impl to convert it to FieldValue) --- src/adapter/mod.rs | 3 +++ src/adapter/properties.rs | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/adapter/mod.rs b/src/adapter/mod.rs index ed43285..aebc038 100644 --- a/src/adapter/mod.rs +++ b/src/adapter/mod.rs @@ -141,6 +141,9 @@ impl<'a> Adapter<'a> for RustdocAdapter<'a> { "AssociatedConstant" => { properties::resolve_associated_constant_property(contexts, property_name) } + "Constant" => { + properties::resolve_constant_property(contexts, property_name) + } _ => unreachable!("resolve_property {type_name} {property_name}"), } } diff --git a/src/adapter/properties.rs b/src/adapter/properties.rs index 5bba3cc..5cbb389 100644 --- a/src/adapter/properties.rs +++ b/src/adapter/properties.rs @@ -465,3 +465,24 @@ pub(crate) fn resolve_associated_constant_property<'a>( _ => unreachable!("AssociatedConstant property {property_name}"), } } +pub(crate) fn resolve_constant_property<'a>( + contexts: ContextIterator<'a, Vertex<'a>>, + property_name: &str, +) -> ContextOutcomeIterator<'a, Vertex<'a>, FieldValue> { + match property_name { + "type_" => resolve_property_with(contexts, field_property!(as_item, inner, { + let ItemEnum::Constant(c) = &inner else {unreachable!("expected to have a Constant")}; + // c.type_ .clone().into()}),), // Type→FieldValue not implemented, use ↓ json + serde_json::to_string(&c.type_.clone()).unwrap().into()}),), + "expr" => resolve_property_with(contexts, field_property!(as_item, inner, { + let ItemEnum::Constant(c) = &inner else {unreachable!("expected to have a Constant")}; + c.expr .clone().into()}),), + "value" => resolve_property_with(contexts, field_property!(as_item, inner, { + let ItemEnum::Constant(c) = &inner else {unreachable!("expected to have a Constant")}; + c.value .clone().into()}),), + "is_literal" => resolve_property_with(contexts, field_property!(as_item, inner, { + let ItemEnum::Constant(c) = &inner else {unreachable!("expected to have a Constant")}; + c.is_literal.clone().into()}),), + _ => unreachable!("Constant property {property_name}"), + } +} From 364c401d9745ef2d52f23d97d95940feb747ed18 Mon Sep 17 00:00:00 2001 From: eugenesvk Date: Fri, 6 Oct 2023 02:24:07 +0700 Subject: [PATCH 02/23] add Constant property rustdocs example --- src/adapter/properties.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/adapter/properties.rs b/src/adapter/properties.rs index 5cbb389..d8ecdfe 100644 --- a/src/adapter/properties.rs +++ b/src/adapter/properties.rs @@ -465,6 +465,26 @@ pub(crate) fn resolve_associated_constant_property<'a>( _ => unreachable!("AssociatedConstant property {property_name}"), } } + + +// const example from rustdocs +// {"id" :"0:9:xx","crate_id":0, +// "name" :"PUB_CONST_NAME", +// "span" :{"filename":"","begin":[5,0],"end":[5,68]}, +// "visibility" :"public", +// "docs" :null, +// "links" :{}, +// "attrs" :[], +// "deprecation" : null, +// "inner" : { ← &inner will be set to ItemEnum::Constant(c), where c=struct rustdoc_types::Constant +// "constant" : { rustdoc_types::Constant has type_/expr/value/is_literal fields (docs.rs/rustdoc-types/latest/rustdoc_types/struct.Constant.html) +// "type" :{"primitive":"i32"}, or ↓ or any of docs.rs/rustdoc-types/latest/rustdoc_types/enum.Type.html +// "type" :{"borrowed_ref":{"lifetime": "'static","mutable": false,"type":{"primitive": "str"}}}, +// "type" :{"resolved_path":{"name":"Years","id": "0:3:1633","args":{"angle_bracketed":{"args":[],"bindings":[]}}}}, +// "expr" : "_", +// "value" : null, +// "is_literal" : false}} +// }, pub(crate) fn resolve_constant_property<'a>( contexts: ContextIterator<'a, Vertex<'a>>, property_name: &str, From 2d08ae20b1ae1b0bc6932114b0258415060c40cd Mon Sep 17 00:00:00 2001 From: eugenesvk Date: Fri, 6 Oct 2023 02:55:24 +0700 Subject: [PATCH 03/23] add Constant property rustdoc schema --- src/rustdoc_schema.graphql | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/rustdoc_schema.graphql b/src/rustdoc_schema.graphql index 367b750..a313e4a 100644 --- a/src/rustdoc_schema.graphql +++ b/src/rustdoc_schema.graphql @@ -694,6 +694,37 @@ type Constant implements Item & Importable & GlobalValue { docs: String attrs: [String!]! visibility_limit: String! + # properties for Constant + """ + A type represented in the "raw" rustdoc JSON representation, see interface RawType. + Currently serialized as a serde_json String instead of a proper RawType + ```rust + // // rustdocs type field + const MIN : usize = 16 ; // {"primitive":"usize"} + const LOG_AS : &'static str = "batch" ; // {"borrowed_ref":{"lifetime":"'static","mutable":false,"type":{"primitive":"str"}}} + const YEAR : Years = Years(42); // {"resolved_path":{"name":"Years","id":"0:3:1633","args":{"angle_bracketed":{"args":[],"bindings":[]}}}} + ``` + """ + type_: String + """ + The expression/value of the constant, if any, as a Rust literal, expression, or `"_"`, as well as its literal flag. For example: + ```rust + // // is_literal value expr + const MIN : usize = 16 ; // true `"16usize"` `16` + const MIN_SIZE : usize = MIN ; // false `"16usize"` "MIN", referring to the other constant's name + const LOG_AS : &'static str = "batch" ; // true None `"\"batch\""` including escaped quotes + const YEAR : Years = Years(42); // false None "_" + const EXPR_2_2 : i32 = 2 + 2 ; // false `"4i32" "_" + const FN_FIVE : i32 = five() ; // false `"5i32" "_" + const fn five() -> i32 { 5 } ; + struct Years(i32); + ``` + If the constant is set to be equal to another constant, the expr holds the name of that other constant, while the value holds the value of that other constant. + If the constant is set by evaluating a `const` expression, such as `2 + 2` or a `const fn` call, rustdoc's current behavior is to show an expr of `"_"` (the value is evaluated) + """ + expr: String + value: String + is_literal: Boolean # edges from Item span: Span From 443c08673e91db8714c72b9cfab5d5962158c9cc Mon Sep 17 00:00:00 2001 From: eugenesvk Date: Fri, 6 Oct 2023 02:55:43 +0700 Subject: [PATCH 04/23] dep: add serde_json for Constant property Type --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index aef380b..e8a919f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ readme = "./README.md" [dependencies] trustfall = "0.6.0" rustdoc-types = "0.22.0" +serde_json = "1.0.85" [dev-dependencies] anyhow = "1.0.58" From 6854eb9e54cfd8d25291f713dca4ae4d658f2eb6 Mon Sep 17 00:00:00 2001 From: eugenesvk Date: Fri, 6 Oct 2023 17:52:36 +0700 Subject: [PATCH 05/23] fix clippy --- src/adapter/properties.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/adapter/properties.rs b/src/adapter/properties.rs index d8ecdfe..8e3e692 100644 --- a/src/adapter/properties.rs +++ b/src/adapter/properties.rs @@ -502,7 +502,7 @@ pub(crate) fn resolve_constant_property<'a>( c.value .clone().into()}),), "is_literal" => resolve_property_with(contexts, field_property!(as_item, inner, { let ItemEnum::Constant(c) = &inner else {unreachable!("expected to have a Constant")}; - c.is_literal.clone().into()}),), + c.is_literal .into()}),), _ => unreachable!("Constant property {property_name}"), } } From 52331328449ecb6c63d61dec009519e1e109ee80 Mon Sep 17 00:00:00 2001 From: eugenesvk Date: Fri, 6 Oct 2023 17:56:21 +0700 Subject: [PATCH 06/23] uglify with rustfmt --- src/adapter/mod.rs | 4 +-- src/adapter/properties.rs | 51 ++++++++++++++++++++++++++++----------- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/adapter/mod.rs b/src/adapter/mod.rs index aebc038..3b21f2a 100644 --- a/src/adapter/mod.rs +++ b/src/adapter/mod.rs @@ -141,9 +141,7 @@ impl<'a> Adapter<'a> for RustdocAdapter<'a> { "AssociatedConstant" => { properties::resolve_associated_constant_property(contexts, property_name) } - "Constant" => { - properties::resolve_constant_property(contexts, property_name) - } + "Constant" => properties::resolve_constant_property(contexts, property_name), _ => unreachable!("resolve_property {type_name} {property_name}"), } } diff --git a/src/adapter/properties.rs b/src/adapter/properties.rs index 8e3e692..541dd4a 100644 --- a/src/adapter/properties.rs +++ b/src/adapter/properties.rs @@ -466,7 +466,6 @@ pub(crate) fn resolve_associated_constant_property<'a>( } } - // const example from rustdocs // {"id" :"0:9:xx","crate_id":0, // "name" :"PUB_CONST_NAME", @@ -490,19 +489,43 @@ pub(crate) fn resolve_constant_property<'a>( property_name: &str, ) -> ContextOutcomeIterator<'a, Vertex<'a>, FieldValue> { match property_name { - "type_" => resolve_property_with(contexts, field_property!(as_item, inner, { - let ItemEnum::Constant(c) = &inner else {unreachable!("expected to have a Constant")}; - // c.type_ .clone().into()}),), // Type→FieldValue not implemented, use ↓ json - serde_json::to_string(&c.type_.clone()).unwrap().into()}),), - "expr" => resolve_property_with(contexts, field_property!(as_item, inner, { - let ItemEnum::Constant(c) = &inner else {unreachable!("expected to have a Constant")}; - c.expr .clone().into()}),), - "value" => resolve_property_with(contexts, field_property!(as_item, inner, { - let ItemEnum::Constant(c) = &inner else {unreachable!("expected to have a Constant")}; - c.value .clone().into()}),), - "is_literal" => resolve_property_with(contexts, field_property!(as_item, inner, { - let ItemEnum::Constant(c) = &inner else {unreachable!("expected to have a Constant")}; - c.is_literal .into()}),), + "type_" => resolve_property_with( + contexts, + field_property!(as_item, inner, { + let ItemEnum::Constant(c) = &inner else { + unreachable!("expected to have a Constant") + }; + // c.type_ .clone().into()}),), // Type→FieldValue not implemented, use ↓ json + serde_json::to_string(&c.type_.clone()).unwrap().into() + }), + ), + "expr" => resolve_property_with( + contexts, + field_property!(as_item, inner, { + let ItemEnum::Constant(c) = &inner else { + unreachable!("expected to have a Constant") + }; + c.expr.clone().into() + }), + ), + "value" => resolve_property_with( + contexts, + field_property!(as_item, inner, { + let ItemEnum::Constant(c) = &inner else { + unreachable!("expected to have a Constant") + }; + c.value.clone().into() + }), + ), + "is_literal" => resolve_property_with( + contexts, + field_property!(as_item, inner, { + let ItemEnum::Constant(c) = &inner else { + unreachable!("expected to have a Constant") + }; + c.is_literal.into() + }), + ), _ => unreachable!("Constant property {property_name}"), } } From 024f7b70818139d613eb1cb013bd21818e9c07b7 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Sat, 7 Oct 2023 00:26:58 +0700 Subject: [PATCH 07/23] Update src/rustdoc_schema.graphql Co-authored-by: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com> --- src/rustdoc_schema.graphql | 1 + 1 file changed, 1 insertion(+) diff --git a/src/rustdoc_schema.graphql b/src/rustdoc_schema.graphql index a313e4a..9f40170 100644 --- a/src/rustdoc_schema.graphql +++ b/src/rustdoc_schema.graphql @@ -694,6 +694,7 @@ type Constant implements Item & Importable & GlobalValue { docs: String attrs: [String!]! visibility_limit: String! + # properties for Constant """ A type represented in the "raw" rustdoc JSON representation, see interface RawType. From 2e665e57bfeb4295e27cc9e634d70c1edf800a25 Mon Sep 17 00:00:00 2001 From: eugenesvk Date: Sat, 7 Oct 2023 00:49:10 +0700 Subject: [PATCH 08/23] squeeze doc comment --- src/rustdoc_schema.graphql | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/rustdoc_schema.graphql b/src/rustdoc_schema.graphql index 9f40170..54c36b4 100644 --- a/src/rustdoc_schema.graphql +++ b/src/rustdoc_schema.graphql @@ -697,31 +697,31 @@ type Constant implements Item & Importable & GlobalValue { # properties for Constant """ - A type represented in the "raw" rustdoc JSON representation, see interface RawType. + A type represented in the "raw" rustdoc JSON representation (see `interface RawType`) Currently serialized as a serde_json String instead of a proper RawType ```rust - // // rustdocs type field - const MIN : usize = 16 ; // {"primitive":"usize"} - const LOG_AS : &'static str = "batch" ; // {"borrowed_ref":{"lifetime":"'static","mutable":false,"type":{"primitive":"str"}}} - const YEAR : Years = Years(42); // {"resolved_path":{"name":"Years","id":"0:3:1633","args":{"angle_bracketed":{"args":[],"bindings":[]}}}} + // // rustdocs type field + const MIN : usize = 16 ; // {"primitive":"usize"} + const LOG_AS: &str = "batch" ; // {"borrowed_ref":{"lifetime":null,"mutable":false,"type":{"primitive":"str"}}} + const YEAR : Years = Years(42); // {"resolved_path":{"name":"Years","id":"0:3:1633","args":{"angle_bracketed":{"args":[],"bindings":[]}}}} ``` """ type_: String """ The expression/value of the constant, if any, as a Rust literal, expression, or `"_"`, as well as its literal flag. For example: ```rust - // // is_literal value expr - const MIN : usize = 16 ; // true `"16usize"` `16` - const MIN_SIZE : usize = MIN ; // false `"16usize"` "MIN", referring to the other constant's name - const LOG_AS : &'static str = "batch" ; // true None `"\"batch\""` including escaped quotes - const YEAR : Years = Years(42); // false None "_" - const EXPR_2_2 : i32 = 2 + 2 ; // false `"4i32" "_" - const FN_FIVE : i32 = five() ; // false `"5i32" "_" - const fn five() -> i32 { 5 } ; + // // is_literal value expr + const MIN : usize = 16 ; // true `"16usize"` `16` + const MIN_SIZE: usize = MIN ; // false `"16usize"` "MIN", referring to the other constant's name + const LOG_AS : &str = "batch" ; // true None `"\"batch\""` including escaped quotes + const YEAR : Years = Years(42); // false None "_" + const EXPR_2_2: i32 = 2 + 2 ; // false `"4i32" "_" + const FN_FIVE : i32 = five() ; // false `"5i32" "_" + const fn five() -> i32 { 5 }; struct Years(i32); ``` - If the constant is set to be equal to another constant, the expr holds the name of that other constant, while the value holds the value of that other constant. - If the constant is set by evaluating a `const` expression, such as `2 + 2` or a `const fn` call, rustdoc's current behavior is to show an expr of `"_"` (the value is evaluated) + If the constant is set to be equal to another constant, `expr` holds the name of that other constant, while `value` holds the value of that other constant. + If the constant is set by evaluating a `const` expression, such as `2 + 2` or a `const fn` call, rustdoc's current behavior is to show an `expr` of `"_"` (`value` is evaluated) """ expr: String value: String From 278eceac3f8ff63ad33fbc5efa504e413b6e6286 Mon Sep 17 00:00:00 2001 From: eugenesvk Date: Sat, 7 Oct 2023 00:49:07 +0700 Subject: [PATCH 09/23] add a doc comment note referencing a more comprehensive example to avoid repeating said example --- src/rustdoc_schema.graphql | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/rustdoc_schema.graphql b/src/rustdoc_schema.graphql index 54c36b4..9ab1163 100644 --- a/src/rustdoc_schema.graphql +++ b/src/rustdoc_schema.graphql @@ -724,7 +724,13 @@ type Constant implements Item & Importable & GlobalValue { If the constant is set by evaluating a `const` expression, such as `2 + 2` or a `const fn` call, rustdoc's current behavior is to show an `expr` of `"_"` (`value` is evaluated) """ expr: String + """ + See `expr` docs for a comprehensive example also covering `value` + """ value: String + """ + See `expr` docs for a comprehensive example also covering `is_literal` + """ is_literal: Boolean # edges from Item From 7dcafb57db6d376df08de3c9cdb6ac0c4c0708b2 Mon Sep 17 00:00:00 2001 From: eugenesvk Date: Sat, 7 Oct 2023 01:53:33 +0700 Subject: [PATCH 10/23] add type_ field to AssociatedConstant property resolution function (temporary in the json string format) --- src/adapter/properties.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/adapter/properties.rs b/src/adapter/properties.rs index 541dd4a..bf30640 100644 --- a/src/adapter/properties.rs +++ b/src/adapter/properties.rs @@ -462,6 +462,16 @@ pub(crate) fn resolve_associated_constant_property<'a>( default.clone().into() }), ), + "type_" => resolve_property_with( + contexts, + field_property!(as_item, inner, { + let ItemEnum::AssocConst { type_, .. } = &inner else { + unreachable!("expected to have a AssocConst") + }; + // type_.clone().into() // Type→FieldValue not implemented, use ↓ json + serde_json::to_string(&type_.clone()).unwrap().into() + }), + ), _ => unreachable!("AssociatedConstant property {property_name}"), } } From 097a7f173552fbe2a326b0cbe34f83d2f3add8e7 Mon Sep 17 00:00:00 2001 From: eugenesvk Date: Sat, 7 Oct 2023 01:58:24 +0700 Subject: [PATCH 11/23] add type_ field to AssociatedConstant property rustdoc schema --- src/rustdoc_schema.graphql | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/rustdoc_schema.graphql b/src/rustdoc_schema.graphql index 9ab1163..fbebb13 100644 --- a/src/rustdoc_schema.graphql +++ b/src/rustdoc_schema.graphql @@ -916,14 +916,36 @@ type AssociatedConstant implements Item { # properties for AssociatedConstant """ + A type represented in the "raw" rustdoc JSON representation (see `interface RawType`) + Currently serialized as a serde_json String instead of a proper RawType + ```rust + const fn five() -> i32 { 5 }; + struct Years(i32); + trait BatchIterator { // rustdocs type field + const MIN : usize = 16 ; // {"primitive":"usize"} + const MIN_SIZE: usize = MIN ; // {"primitive":"usize"} + const LOG_AS : &'static str = "batch" ; // {"borrowed_ref":{"lifetime":null,"mutable":false,"type":{"primitive":"str"}}} + const YEAR : Years = Years(42); // {"resolved_path":{"name":"Years","id":"0:3:1633","args":{"angle_bracketed":{"args":[],"bindings":[]}}}} + const EXPR2_2 : i32 = 2+2 ; // {"primitive":"i32"} + const FN_FIVE : i32 = five() ; // {"primitive":"i32"} + } + ``` + """ + type_: String + """ The default value of the constant, if any, as a Rust literal, expression, or `"_"`. For example: ```rust - trait BatchIterator { - const SIZE: usize = 16; // `"16"` is the default - const LOG_AS: &'static str = "batch"; // `"\"batch\""` is the default, including escaped quotes - const MIN_SIZE: usize = MIN; // "MIN" is the default, referring to the other constant's name + const fn five() -> i32 { 5 }; + struct Years(i32); + trait BatchIterator { // rustdocs default field + const MIN : usize = 16 ; // `"16"` + const MIN_SIZE: usize = MIN ; // `"MIN"`, referring to the other constant's name + const LOG_AS : &'static str = "batch" ; // `"\"batch\""`, including escaped quotes + const EXPR2_2 : i32 = 2+2 ; // `"_"` + const FN_FIVE : i32 = five() ; // `"_"` + const YEAR : Years = Years(42); // `"_"` } ``` From 2cdd8c087719b38c275deff422f561b26a774ebb Mon Sep 17 00:00:00 2001 From: eugenesvk Date: Sat, 7 Oct 2023 03:37:19 +0700 Subject: [PATCH 12/23] split schema docs so that each Constant property is documented --- src/rustdoc_schema.graphql | 52 +++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/src/rustdoc_schema.graphql b/src/rustdoc_schema.graphql index fbebb13..0b1af69 100644 --- a/src/rustdoc_schema.graphql +++ b/src/rustdoc_schema.graphql @@ -708,28 +708,56 @@ type Constant implements Item & Importable & GlobalValue { """ type_: String """ - The expression/value of the constant, if any, as a Rust literal, expression, or `"_"`, as well as its literal flag. For example: + The expression of the constant, if any, as a Rust literal or `"_"`. For example: ```rust - // // is_literal value expr - const MIN : usize = 16 ; // true `"16usize"` `16` - const MIN_SIZE: usize = MIN ; // false `"16usize"` "MIN", referring to the other constant's name - const LOG_AS : &str = "batch" ; // true None `"\"batch\""` including escaped quotes - const YEAR : Years = Years(42); // false None "_" - const EXPR_2_2: i32 = 2 + 2 ; // false `"4i32" "_" - const FN_FIVE : i32 = five() ; // false `"5i32" "_" + // // expr + const MIN : usize = 16 ; // 16 + const MIN_SIZE: usize = MIN ; // "MIN", referring to the other constant's name + const LOG_AS : &str = "batch" ; // "\"batch\"", including escaped quotes + const YEAR : Years = Years(42); // "_" + const EXPR_2_2: i32 = 2 + 2 ; // "_" + const FN_FIVE : i32 = five() ; // "_" const fn five() -> i32 { 5 }; struct Years(i32); ``` - If the constant is set to be equal to another constant, `expr` holds the name of that other constant, while `value` holds the value of that other constant. - If the constant is set by evaluating a `const` expression, such as `2 + 2` or a `const fn` call, rustdoc's current behavior is to show an `expr` of `"_"` (`value` is evaluated) + If the constant is set: + + - to be equal to another constant, `expr` holds the name of that other constant. + - by evaluating a `const` expression, such as `2 + 2` or a `const fn` call, `expr` is `"_"` instead of including the full expression. """ expr: String """ - See `expr` docs for a comprehensive example also covering `value` + The value of the constant, if any, as a Rust literal. For example: + ```rust + // // value + const MIN : usize = 16 ; // "16usize" + const MIN_SIZE: usize = MIN ; // "16usize" + const LOG_AS : &str = "batch" ; // None + const YEAR : Years = Years(42); // None + const EXPR_2_2: i32 = 2 + 2 ; // "4i32" + const FN_FIVE : i32 = five() ; // "5i32" + const fn five() -> i32 { 5 }; + struct Years(i32); + ``` + If the If the constant is set: + + - to be equal to another constant, `value` holds the value of that other constant. + - by evaluating a `const` expression, such as `2 + 2` or a `const fn` call, `value` is evaluated """ value: String """ - See `expr` docs for a comprehensive example also covering `is_literal` + The literal flag of the constant. For example: + ```rust + // // is_literal + const MIN : usize = 16 ; // true + const MIN_SIZE: usize = MIN ; // false + const LOG_AS : &str = "batch" ; // true + const YEAR : Years = Years(42); // false + const EXPR_2_2: i32 = 2 + 2 ; // false + const FN_FIVE : i32 = five() ; // false + const fn five() -> i32 { 5 }; + struct Years(i32); + ``` """ is_literal: Boolean From 14be2e18811cdd41d173762f66b27e891ad7455a Mon Sep 17 00:00:00 2001 From: eugenesvk Date: Sat, 7 Oct 2023 03:37:40 +0700 Subject: [PATCH 13/23] update schema doc comment --- src/rustdoc_schema.graphql | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/rustdoc_schema.graphql b/src/rustdoc_schema.graphql index 0b1af69..ba5032d 100644 --- a/src/rustdoc_schema.graphql +++ b/src/rustdoc_schema.graphql @@ -697,7 +697,7 @@ type Constant implements Item & Importable & GlobalValue { # properties for Constant """ - A type represented in the "raw" rustdoc JSON representation (see `interface RawType`) + A type represented in the "raw" rustdoc JSON representation (see `interface RawType`)
Currently serialized as a serde_json String instead of a proper RawType ```rust // // rustdocs type field @@ -944,7 +944,7 @@ type AssociatedConstant implements Item { # properties for AssociatedConstant """ - A type represented in the "raw" rustdoc JSON representation (see `interface RawType`) + A type represented in the "raw" rustdoc JSON representation (see `interface RawType`)
Currently serialized as a serde_json String instead of a proper RawType ```rust const fn five() -> i32 { 5 }; @@ -977,14 +977,12 @@ type AssociatedConstant implements Item { } ``` - If the associated constant is on a type's inherent impl, the default is always required to be set. + If the associated constant is on a type's inherent impl, `default` is always required to be set. - If the associated constant is set to be equal to another constant, the default holds the name - of that other constant. + If the associated constant is set - If the associated constant is set by evaluating a `const` expression, such as `2 + 2` or - a `const fn` call, rustdoc's current behavior is to show a default value of `"_"` - instead of evaluating the constant value or including the full expression. + - to be equal to another constant, `default` holds the name of that other constant. + - by evaluating a `const` expression, such as `2 + 2` or a `const fn` call, `default` is `"_"` instead of evaluating the constant value or including the full expression. """ default: String From 48c1d3dbd467a005e4906f7a1d99732dcef41d45 Mon Sep 17 00:00:00 2001 From: eugenesvk Date: Sat, 7 Oct 2023 16:20:41 +0700 Subject: [PATCH 14/23] update schema docs trait name --- src/rustdoc_schema.graphql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rustdoc_schema.graphql b/src/rustdoc_schema.graphql index ba5032d..ebb86d2 100644 --- a/src/rustdoc_schema.graphql +++ b/src/rustdoc_schema.graphql @@ -949,7 +949,7 @@ type AssociatedConstant implements Item { ```rust const fn five() -> i32 { 5 }; struct Years(i32); - trait BatchIterator { // rustdocs type field + trait MyTrait { // rustdocs type field const MIN : usize = 16 ; // {"primitive":"usize"} const MIN_SIZE: usize = MIN ; // {"primitive":"usize"} const LOG_AS : &'static str = "batch" ; // {"borrowed_ref":{"lifetime":null,"mutable":false,"type":{"primitive":"str"}}} @@ -967,7 +967,7 @@ type AssociatedConstant implements Item { ```rust const fn five() -> i32 { 5 }; struct Years(i32); - trait BatchIterator { // rustdocs default field + trait MyTrait { // rustdocs default field const MIN : usize = 16 ; // `"16"` const MIN_SIZE: usize = MIN ; // `"MIN"`, referring to the other constant's name const LOG_AS : &'static str = "batch" ; // `"\"batch\""`, including escaped quotes From f52b2aee362b60f4f01b2889fa1a676ff77a3645 Mon Sep 17 00:00:00 2001 From: eugenesvk Date: Sat, 7 Oct 2023 16:24:33 +0700 Subject: [PATCH 15/23] fix a typo in schema docs --- src/rustdoc_schema.graphql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rustdoc_schema.graphql b/src/rustdoc_schema.graphql index ebb86d2..f6d8ca1 100644 --- a/src/rustdoc_schema.graphql +++ b/src/rustdoc_schema.graphql @@ -739,7 +739,7 @@ type Constant implements Item & Importable & GlobalValue { const fn five() -> i32 { 5 }; struct Years(i32); ``` - If the If the constant is set: + If the constant is set: - to be equal to another constant, `value` holds the value of that other constant. - by evaluating a `const` expression, such as `2 + 2` or a `const fn` call, `value` is evaluated From 9a48e868a82467b79a490f2f1f738cc886075a4e Mon Sep 17 00:00:00 2001 From: eugenesvk Date: Sat, 7 Oct 2023 16:40:42 +0700 Subject: [PATCH 16/23] remove type Assoc/Const property until trustfall adds proper support for custom scalar types --- src/adapter/properties.rs | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/src/adapter/properties.rs b/src/adapter/properties.rs index bf30640..534e086 100644 --- a/src/adapter/properties.rs +++ b/src/adapter/properties.rs @@ -462,16 +462,6 @@ pub(crate) fn resolve_associated_constant_property<'a>( default.clone().into() }), ), - "type_" => resolve_property_with( - contexts, - field_property!(as_item, inner, { - let ItemEnum::AssocConst { type_, .. } = &inner else { - unreachable!("expected to have a AssocConst") - }; - // type_.clone().into() // Type→FieldValue not implemented, use ↓ json - serde_json::to_string(&type_.clone()).unwrap().into() - }), - ), _ => unreachable!("AssociatedConstant property {property_name}"), } } @@ -487,6 +477,7 @@ pub(crate) fn resolve_associated_constant_property<'a>( // "deprecation" : null, // "inner" : { ← &inner will be set to ItemEnum::Constant(c), where c=struct rustdoc_types::Constant // "constant" : { rustdoc_types::Constant has type_/expr/value/is_literal fields (docs.rs/rustdoc-types/latest/rustdoc_types/struct.Constant.html) +// "type" : currently not supported as a property pending custom scalar type support in Trustfall (https://github.com/obi1kenobi/trustfall-rustdoc-adapter/pull/280#discussion_r1348950734) // "type" :{"primitive":"i32"}, or ↓ or any of docs.rs/rustdoc-types/latest/rustdoc_types/enum.Type.html // "type" :{"borrowed_ref":{"lifetime": "'static","mutable": false,"type":{"primitive": "str"}}}, // "type" :{"resolved_path":{"name":"Years","id": "0:3:1633","args":{"angle_bracketed":{"args":[],"bindings":[]}}}}, @@ -499,16 +490,6 @@ pub(crate) fn resolve_constant_property<'a>( property_name: &str, ) -> ContextOutcomeIterator<'a, Vertex<'a>, FieldValue> { match property_name { - "type_" => resolve_property_with( - contexts, - field_property!(as_item, inner, { - let ItemEnum::Constant(c) = &inner else { - unreachable!("expected to have a Constant") - }; - // c.type_ .clone().into()}),), // Type→FieldValue not implemented, use ↓ json - serde_json::to_string(&c.type_.clone()).unwrap().into() - }), - ), "expr" => resolve_property_with( contexts, field_property!(as_item, inner, { From 151ac1adf99d59cd90b44130faa5e66591f16d66 Mon Sep 17 00:00:00 2001 From: eugenesvk Date: Sat, 7 Oct 2023 16:40:56 +0700 Subject: [PATCH 17/23] remove type Assoc/Const property from schema --- src/rustdoc_schema.graphql | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/src/rustdoc_schema.graphql b/src/rustdoc_schema.graphql index f6d8ca1..4fc7e8a 100644 --- a/src/rustdoc_schema.graphql +++ b/src/rustdoc_schema.graphql @@ -697,17 +697,6 @@ type Constant implements Item & Importable & GlobalValue { # properties for Constant """ - A type represented in the "raw" rustdoc JSON representation (see `interface RawType`)
- Currently serialized as a serde_json String instead of a proper RawType - ```rust - // // rustdocs type field - const MIN : usize = 16 ; // {"primitive":"usize"} - const LOG_AS: &str = "batch" ; // {"borrowed_ref":{"lifetime":null,"mutable":false,"type":{"primitive":"str"}}} - const YEAR : Years = Years(42); // {"resolved_path":{"name":"Years","id":"0:3:1633","args":{"angle_bracketed":{"args":[],"bindings":[]}}}} - ``` - """ - type_: String - """ The expression of the constant, if any, as a Rust literal or `"_"`. For example: ```rust // // expr @@ -944,23 +933,6 @@ type AssociatedConstant implements Item { # properties for AssociatedConstant """ - A type represented in the "raw" rustdoc JSON representation (see `interface RawType`)
- Currently serialized as a serde_json String instead of a proper RawType - ```rust - const fn five() -> i32 { 5 }; - struct Years(i32); - trait MyTrait { // rustdocs type field - const MIN : usize = 16 ; // {"primitive":"usize"} - const MIN_SIZE: usize = MIN ; // {"primitive":"usize"} - const LOG_AS : &'static str = "batch" ; // {"borrowed_ref":{"lifetime":null,"mutable":false,"type":{"primitive":"str"}}} - const YEAR : Years = Years(42); // {"resolved_path":{"name":"Years","id":"0:3:1633","args":{"angle_bracketed":{"args":[],"bindings":[]}}}} - const EXPR2_2 : i32 = 2+2 ; // {"primitive":"i32"} - const FN_FIVE : i32 = five() ; // {"primitive":"i32"} - } - ``` - """ - type_: String - """ The default value of the constant, if any, as a Rust literal, expression, or `"_"`. For example: From dc87d614a4e2fb3b844eee3a566ce9b406782d64 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Mon, 9 Oct 2023 02:56:25 +0700 Subject: [PATCH 18/23] fix a typo in schema docs Co-authored-by: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com> --- src/rustdoc_schema.graphql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rustdoc_schema.graphql b/src/rustdoc_schema.graphql index 4fc7e8a..fccbd5b 100644 --- a/src/rustdoc_schema.graphql +++ b/src/rustdoc_schema.graphql @@ -951,7 +951,7 @@ type AssociatedConstant implements Item { If the associated constant is on a type's inherent impl, `default` is always required to be set. - If the associated constant is set + If the associated constant is set: - to be equal to another constant, `default` holds the name of that other constant. - by evaluating a `const` expression, such as `2 + 2` or a `const fn` call, `default` is `"_"` instead of evaluating the constant value or including the full expression. From 08591043ebe3a0e2587540361ae0f38bc7aa4d9b Mon Sep 17 00:00:00 2001 From: Evgeny Date: Mon, 9 Oct 2023 03:06:53 +0700 Subject: [PATCH 19/23] dep: remove serde_json since Constant property Type was removed Co-authored-by: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com> --- Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index e8a919f..aef380b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,6 @@ readme = "./README.md" [dependencies] trustfall = "0.6.0" rustdoc-types = "0.22.0" -serde_json = "1.0.85" [dev-dependencies] anyhow = "1.0.58" From 4950f7c2a53b54f1c4c0538577deb67f9a281c93 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Mon, 9 Oct 2023 03:16:32 +0700 Subject: [PATCH 20/23] remove unstable rustdocs example for Constant properties Co-authored-by: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com> --- src/adapter/properties.rs | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/src/adapter/properties.rs b/src/adapter/properties.rs index 534e086..3c4de81 100644 --- a/src/adapter/properties.rs +++ b/src/adapter/properties.rs @@ -466,25 +466,6 @@ pub(crate) fn resolve_associated_constant_property<'a>( } } -// const example from rustdocs -// {"id" :"0:9:xx","crate_id":0, -// "name" :"PUB_CONST_NAME", -// "span" :{"filename":"","begin":[5,0],"end":[5,68]}, -// "visibility" :"public", -// "docs" :null, -// "links" :{}, -// "attrs" :[], -// "deprecation" : null, -// "inner" : { ← &inner will be set to ItemEnum::Constant(c), where c=struct rustdoc_types::Constant -// "constant" : { rustdoc_types::Constant has type_/expr/value/is_literal fields (docs.rs/rustdoc-types/latest/rustdoc_types/struct.Constant.html) -// "type" : currently not supported as a property pending custom scalar type support in Trustfall (https://github.com/obi1kenobi/trustfall-rustdoc-adapter/pull/280#discussion_r1348950734) -// "type" :{"primitive":"i32"}, or ↓ or any of docs.rs/rustdoc-types/latest/rustdoc_types/enum.Type.html -// "type" :{"borrowed_ref":{"lifetime": "'static","mutable": false,"type":{"primitive": "str"}}}, -// "type" :{"resolved_path":{"name":"Years","id": "0:3:1633","args":{"angle_bracketed":{"args":[],"bindings":[]}}}}, -// "expr" : "_", -// "value" : null, -// "is_literal" : false}} -// }, pub(crate) fn resolve_constant_property<'a>( contexts: ContextIterator<'a, Vertex<'a>>, property_name: &str, From c0748bc7bacf5fd8b583ba37c9f9d3645aa57bd8 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Mon, 9 Oct 2023 03:18:37 +0700 Subject: [PATCH 21/23] Update formatting of the schema docs Co-authored-by: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com> --- src/rustdoc_schema.graphql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/rustdoc_schema.graphql b/src/rustdoc_schema.graphql index fccbd5b..0825619 100644 --- a/src/rustdoc_schema.graphql +++ b/src/rustdoc_schema.graphql @@ -954,7 +954,8 @@ type AssociatedConstant implements Item { If the associated constant is set: - to be equal to another constant, `default` holds the name of that other constant. - - by evaluating a `const` expression, such as `2 + 2` or a `const fn` call, `default` is `"_"` instead of evaluating the constant value or including the full expression. + - by evaluating a `const` expression, such as `2 + 2` or a `const fn` call, + `default` is `"_"` instead of evaluating the constant value or including the full expression. """ default: String From e0ced166db323cffd179c95a45f97510d6eca626 Mon Sep 17 00:00:00 2001 From: eugenesvk Date: Mon, 9 Oct 2023 03:34:53 +0700 Subject: [PATCH 22/23] Update AssociatedConstant example to refer to a const generic --- src/rustdoc_schema.graphql | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/rustdoc_schema.graphql b/src/rustdoc_schema.graphql index 0825619..bc01cf8 100644 --- a/src/rustdoc_schema.graphql +++ b/src/rustdoc_schema.graphql @@ -939,13 +939,13 @@ type AssociatedConstant implements Item { ```rust const fn five() -> i32 { 5 }; struct Years(i32); - trait MyTrait { // rustdocs default field - const MIN : usize = 16 ; // `"16"` - const MIN_SIZE: usize = MIN ; // `"MIN"`, referring to the other constant's name - const LOG_AS : &'static str = "batch" ; // `"\"batch\""`, including escaped quotes - const EXPR2_2 : i32 = 2+2 ; // `"_"` - const FN_FIVE : i32 = five() ; // `"_"` - const YEAR : Years = Years(42); // `"_"` + trait MyTrait { // rustdocs default field + const NUM : i32 = 16 ; // 16 + const MIN_SIZE: usize = MIN ; // "MIN", referring to the other constant's name + const LOG_AS : &'static str = "batch" ; // "\"batch\"", including escaped quotes + const EXPR2_2 : i32 = 2+2 ; // "_" + const FN_FIVE : i32 = five() ; // "_" + const YEAR : Years = Years(42); // "_" } ``` From 0f7583172cb6e17267a9d4149249a31d71d26190 Mon Sep 17 00:00:00 2001 From: eugenesvk Date: Mon, 9 Oct 2023 04:17:34 +0700 Subject: [PATCH 23/23] Update Constant test with extra properties expr/value/is_literal --- src/adapter/tests.rs | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/adapter/tests.rs b/src/adapter/tests.rs index 962eb48..63cd664 100644 --- a/src/adapter/tests.rs +++ b/src/adapter/tests.rs @@ -171,6 +171,9 @@ fn rustdoc_finds_consts() { item { ... on Constant { name @output + expr @output + value @output + is_literal @output importable_path { path @output @@ -190,24 +193,48 @@ fn rustdoc_finds_consts() { struct Output { name: String, path: Vec, + expr: String, + value: Option, + is_literal: bool, + } + #[derive(Debug, PartialOrd, Ord, PartialEq, Eq, serde::Deserialize)] + struct OutputSimple { + name: String, + path: Vec, } let mut results: Vec<_> = trustfall::execute_query(&schema, adapter.clone(), query, variables.clone()) .expect("failed to run query") - .map(|row| row.try_into_struct().expect("shape mismatch")) + .map(|row| row.try_into_struct::().expect("shape mismatch")) .collect(); results.sort_unstable(); + // to compare to GlobalValue that doesn't Constant-specific properties + let mut results_simple: Vec<_> = + trustfall::execute_query(&schema, adapter.clone(), query, variables.clone()) + .expect("failed to run query") + .map(|row| { + row.try_into_struct::() + .expect("shape mismatch") + }) + .collect(); + results_simple.sort_unstable(); similar_asserts::assert_eq!( vec![ Output { name: "FIRST".into(), path: vec!["consts".into(), "FIRST".into()], + expr: "1".to_string(), + value: Some("1u32".to_string()), + is_literal: true, }, Output { name: "SECOND".into(), path: vec!["consts".into(), "inner".into(), "SECOND".into()], + expr: "2".to_string(), + value: Some("2i64".to_string()), + is_literal: true, }, ], results @@ -232,10 +259,13 @@ fn rustdoc_finds_consts() { let mut global_values_results: Vec<_> = trustfall::execute_query(&schema, adapter, global_values_query, variables) .expect("failed to run query") - .map(|row| row.try_into_struct().expect("shape mismatch")) + .map(|row| { + row.try_into_struct::() + .expect("shape mismatch") + }) .collect(); global_values_results.sort_unstable(); - assert_eq!(results, global_values_results); + assert_eq!(results_simple, global_values_results); } #[test]