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

feat(parser): remove the impl Display for generated nodes #330

Merged
merged 6 commits into from
Oct 28, 2022

Conversation

goto-bus-stop
Copy link
Member

As mentioned in #329 (comment)

The Display impls for generated nodes returned the source text for that node. That's not a super common operation but it was very easy to access. It's also a very different operation from eg. let content: String = node.string_value().into() which returns the content of a string: node.string_value().to_string() returned the string as it was written in the source code, quotes and escapes and all.

Now .to_string() is replaced by a .source_string() method. It allocates a new String (just like .to_string() did). A syntax node can represent multiple slices (I think to support different structures like Ropes as input?), so slicing the original source isn't actually possible.

@goto-bus-stop goto-bus-stop requested a review from lrlna October 27, 2022 17:17
@@ -26,7 +26,7 @@ impl From<Description> for String {
#[cfg(feature = "parser-impl")]
impl From<apollo_parser::ast::Description> for Description {
fn from(desc: apollo_parser::ast::Description) -> Self {
Description(StringValue::from(desc.to_string()))
Description(desc.string_value().map(|s| s.into()).unwrap_or_else(|| StringValue::Line(Default::default())))
Copy link
Member Author

Choose a reason for hiding this comment

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

I think this was passing the quoted string instead of the string contents previously, turning "text" into """"text""""

Copy link
Member

Choose a reason for hiding this comment

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

ah, yea the original line should have been desc.text().to_string(), since .text() strips away the extra quotation marks around string values. So that's something to keep in mind with this PR - source_string() for string values that have quotes around them should have those quotes stripped.

#[cfg(feature = "parser-impl")]
impl From<apollo_parser::ast::StringValue> for StringValue {
fn from(val: apollo_parser::ast::StringValue) -> Self {
Self::from(Into::<String>::into(val))
Copy link
Member Author

Choose a reason for hiding this comment

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

This Into::<String>::into() business is a bit awkward, i wonder if we could impl From<StringValue> for String instead? or provide a separate named method that returns the unquoted contents of a StringValue?

Copy link
Member

Choose a reason for hiding this comment

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

We already have impl Into<String> for ast::StringValue here.Froms are usually nicer, but that requires us to create a syntax node with an unknown source, which perhaps is a bit too tricky? What do you think?

Copy link
Member

@lrlna lrlna left a comment

Choose a reason for hiding this comment

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

I think we need a few tests to go along with this in the parser side of things. I mostly want to make sure we know if this is going to be a breaking change, for example #98 still works etc. i think a few tests for a some of node conversions, as well as making sure we can still properly display and have the source string of the whole tree (and it's equivalent to the original input). What do you think?

@goto-bus-stop
Copy link
Member Author

Maybe the impl Into<String> for StringValue should be a separate method too. The Into trait takes ownership of the value it converts, but our impl Into<String> doesn't require ownership, it can work on a reference. In bc09fa0 (#330) I had to move the .source_string() test above the .into() test because of this. We could still provide a From or Into impl that calls that separate method internally.

Probably better for a separate PR though 😇

@lrlna
Copy link
Member

lrlna commented Oct 28, 2022

hmmm you mean the other way around? Our Into<String> does not work on references, and only works on owned values:

impl Into<String> for ast::StringValue {
    fn into(self) -> String {
      //
    }
}

In your test you can't use the val after you've converted it, since it moved.

(separate PR is good!)

@goto-bus-stop
Copy link
Member Author

Yes i mean that's what it does, but the implementation of the function doesn't actually require ownership, it only takes ownership to satisfy the trait. The same feature could be implemented as a fn contents(&self) -> String on StringValue without having to consume the value.

@goto-bus-stop goto-bus-stop changed the title Remove the impl Display for generated nodes feat(parser): remove the impl Display for generated nodes Oct 28, 2022
@goto-bus-stop goto-bus-stop merged commit 2991951 into main Oct 28, 2022
@goto-bus-stop goto-bus-stop deleted the no-display branch October 28, 2022 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants