-
Notifications
You must be signed in to change notification settings - Fork 115
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
Support #[serde(with = "...")]
for struct fields
#280
Conversation
#[serde(with = "...")]
#[serde(with = "...")]
for struct fields
Huh, this is much less complicated than I thought it might end up being, nice!
Hm. I guess we could at support for them for variants, or leave this edge-case for now. Will have to look into how complicated supporting these attributes for variants would be first, though I think it might be pretty straight forward.
I think that's supported in doctests with "```compile_fail ....`". Not super clean, but it's simple, so maybe that's enough? |
It is, but I'm not sure if a doctest in the |
That might require two new PRs, one for |
Agreed! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't find the time to take a closer look earlier, sorry about that.
Everything looks good to me here! I was still wishing that we could actually support #[serde(with = "..")]
, but this is still better than the status quo and is actually robust. I'm sold!
Good PR, Please release a new version that includes these updates. I would really like to use them in my project. Thank you! |
Goal
Ensure the
#[serde(with = "...")]
attribute is properly handled in a way that prevents users from accidentally generating wrong TS type definitions by forgetting to add#[ts(as = "...")]
/#[ts(type = "...")]
,Why?
The
#[serde(with = "...")]
attribute changes how the type is serialized, therefore, it changes what type will be received by TypeScript, so the type generated by default will most likely be incorrect. The user must use#[ts(as = "...")]
or#[ts(type = "...")]
to ensure their type definition matches the type generated by serdeWhy not make
#[serde(with = "...")]
an alias for#[ts(as = "...")]
#[ts(as = "...")]
only accepts types, where as#[serde(with = "...")]
accepts either a type or a path to a module containing both aserialize
and adeserialize
function, so the attributes are not compatible and thus cannot just alias each otherCloses #275
Changes
When the
serde-compat
feature is enabled, using#[serde(with = "...")]
will demand the use of either#[ts(as = "...")]
or#[ts(type = "...")]
, resulting in a compiler error if neither is usedChecklist
Todo
#[serde(with = "...")]
is a valid variant attribute, but I'm not yet clear on how to implement it, as neither#[ts(as = "...")]
or#[ts(type = "...")]
are valid variant attributes.#[ts(as = "...")]
and#[ts(type = "...")]
on enum variants #284compiletest_rs
ortrybuild
?)