How to rename enum variants?
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 38/100
Research direction
Read typify-impl/src/util.rs around line 756 and the build.rs settings using TypeSpacePatch::with_rename. Reproduce the schema generation shown in the issue; done means the generated Operator enum has unique valid Rust variant names and the project builds.
Written by the indexing model from the issue text.
Description
Hey,
I tried to use typify to create types from my json schema. Currently, I'm creating my types via build.rs.
Here you see the result, which is created:
///xx
///
/// <details><summary>JSON schema</summary>
///
/// ```json
///{
/// "description": "xx",
/// "type": "string",
/// "enum": [
/// "=",
/// "!=",
/// "in",
/// "out",
/// "<",
/// ">",
/// "<=",
/// ">=",
/// "contains"
/// ],
/// "example": "="
///}
/// ```
/// </details>
#[derive(
::serde::Deserialize,
::serde::Serialize,
Clone,
Copy,
Debug,
Eq,
Hash,
Ord,
PartialEq,
PartialOrd
)]
pub enum Operator {
#[serde(rename = "=")]
X,
#[serde(rename = "!=")]
X,
#[serde(rename = "in")]
In,
#[serde(rename = "out")]
Out,
#[serde(rename = "<")]
X,
#[serde(rename = ">")]
X,
#[serde(rename = "<=")]
X,
#[serde(rename = ">=")]
X,
#[serde(rename = "contains")]
Contains,
}
As you see, my enum (which I sadly cannot change) has values like "=", "!=" etc., which are invalid rust identifier. Therefor, typify will replace it with an capital X. The issue is now, that my enum have multiple variants which called X. Therefor, the following won't work as designed and by build fails:
impl ::std::fmt::Display
for CrmDiscountCustomerConditionsSelectionsItemConditionsItemOperator {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
match *self {
Self::X => write!(f, "="),
Self::X => write!(f, "!="),
Self::In => write!(f, "in"),
Self::Out => write!(f, "out"),
Self::X => write!(f, "<"),
Self::X => write!(f, ">"),
Self::X => write!(f, "<="),
Self::X => write!(f, ">="),
Self::Contains => write!(f, "contains"),
}
}
}
How can I rename my enum variants? I tired the following:
let mut settings = TypeSpaceSettings::default();
settings.with_derive(String::from("PartialEq"));
settings.with_derive(String::from("Debug"));
settings.with_derive(String::from("Clone"));
settings.with_patch(
"Operator::=",
TypeSpacePatch::default().with_rename("OperatorEq".to_owned()),
);
let mut type_space = TypeSpace::new(&settings);
type_space.add_root_schema(schema).unwrap();
let output = prettyplease::unparse(&syn::parse2::<syn::File>(type_space.to_stream()).unwrap());
let mut out_file = std::path::Path::new(&out_dir).to_path_buf();
out_file.push("schema.rs");
std::fs::write(out_file, output).unwrap();
Thanks!
- Dominant language
- Rust
- Stars
- 898
- Forks
- 114
- Avg merge
- 4h 18m
- Merged PRs (30d)
- 14
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from oxidecomputer/typify
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
oxidecomputer/typify#1077 · 1 comment ·
-
Difficulty 3/5 1-2 days Newbie friendliness 55/100
oxidecomputer/typify#1075 · 1 comment ·
-
Difficulty 4/5 3-5 days Newbie friendliness 50/100
oxidecomputer/typify#1060 ·
-
Difficulty 5/5 Over a week Newbie friendliness 48/100
oxidecomputer/typify#1059 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 65/100
oxidecomputer/typify#1022 · 1 comment ·
All issues in oxidecomputer/typify
Similar issues
-
Browser (wasm) relay client cannot connect to relays whose URL has a trailing-dot FQDN hostname Open
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
n0-computer/iroh#4550 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
paritytech/zombienet-sdk#591 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
farion1231/cc-switch#7638 · 1 comment ·
-
onnx-ir re-exports ModelProto and GraphProto but not NodeProto, AttributeProto and AttributeType Open
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100