Skip to content

Commit 38208be

Browse files
aahanaggarwalfacebook-github-bot
authored andcommitted
Update rust schema gen to work better with optionals and enums
Summary: - For enums, Glean expects the integer representation, which we can accomplish by adding a #repr tag on top of the enum and asking serde to use that when serializing. - For optionals, Glean expects it not to exist in the JSON rather than just being `null` hence update this to add a directive for serde. Reviewed By: nhawkes Differential Revision: D78974870 fbshipit-source-id: 64dfe5f53c4db4b2f66def5319040fc4f4704530
1 parent 1766405 commit 38208be

1 file changed

Lines changed: 21 additions & 12 deletions

File tree

  • glean/schema/gen/Glean/Schema/Gen

glean/schema/gen/Glean/Schema/Gen/Rust.hs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ genNamespace namespaces version
9999
, "use serde::Deserialize;"
100100
, "use serde::Serialize;"
101101
, "use serde_json::Value;"
102+
, "use serde_repr::*;"
102103
, ""
103104
, "use crate::report::glean::schema::*;"
104105
] ++
@@ -187,10 +188,10 @@ rustName here (ns, name)
187188
| Text.null (underscored ns) = safe name
188189
| otherwise = underscored ns <> "::" <> safe name
189190

190-
191-
annotation :: Text
192-
annotation = "#[derive(Clone, Debug, Deserialize, Eq, Hash, "
193-
<> "PartialEq, Serialize)]"
191+
data ShouldUseRepr = UseRepr | NoRepr
192+
annotation :: ShouldUseRepr -> Text
193+
annotation UseRepr = "#[derive(Clone, Debug, Deserialize_repr, Eq, Hash, PartialEq, Serialize_repr)]"
194+
annotation NoRepr = "#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]"
194195

195196
wrapInBoxIf :: Bool -> Text -> Text
196197
wrapInBoxIf useBox text =
@@ -261,7 +262,7 @@ genPred here PredicateDef{..} = do
261262
let
262263
-- Predicate struct definition
263264
define = (:[]) $ myUnlines $ concat
264-
[ [ annotation ]
265+
[ [ annotation NoRepr ]
265266
, [ "pub struct " <> name <> " {" ]
266267
, indentLines $ catMaybes
267268
[ Just $ "pub id: " <> type_id <> ","
@@ -313,7 +314,8 @@ makeEnum :: Text -> [Name] -> M [Text]
313314
makeEnum name vals = do
314315
let
315316
variants = Text.unlines $ indentLines (map (<> ",") vals)
316-
declare = annotation <> newline
317+
declare = annotation UseRepr <> newline
318+
<> "#[repr(u8)]" <> newline
317319
<> "pub enum " <> name <> " {" <> newline <> variants <> "}"
318320
return [declare]
319321

@@ -328,15 +330,22 @@ genType here tref ty = addExtraDecls $ do
328330
RecordTy fields -> do
329331
fieldTexts <- forM fields $ \(FieldDef nm ty) -> do
330332
tyName <- withRecordFieldHint nm (rustTy here ty)
331-
return $ "pub " <> safe nm <> ": " <> tyName <> ","
333+
let isOption = case ty of MaybeTy _ -> True; _ -> False
334+
let fieldLine = "pub " <> safe nm <> ": " <> tyName <> ","
335+
return $ if isOption
336+
then [
337+
"#[serde(skip_serializing_if = \"Option::is_none\")]"
338+
, fieldLine
339+
]
340+
else [fieldLine]
332341
let
333342
define | null fields =
334-
annotation <> newline <>
343+
annotation NoRepr <> newline <>
335344
"pub struct " <> name <> " {}"
336345
| otherwise = myUnlines $ concat
337-
[ [ annotation ]
346+
[ [ annotation NoRepr ]
338347
, [ "pub struct " <> name <> " {" ]
339-
, indentLines fieldTexts
348+
, indentLines (concat fieldTexts)
340349
, [ "}" ]
341350
]
342351
return [define]
@@ -346,10 +355,10 @@ genType here tref ty = addExtraDecls $ do
346355
return $ safe nm <> "(" <> safe tyName <> "),"
347356
let
348357
define | null fields =
349-
annotation <> newline <>
358+
annotation NoRepr <> newline <>
350359
"pub enum " <> name <> " {}"
351360
| otherwise = myUnlines $ concat
352-
[ [ annotation ]
361+
[ [ annotation NoRepr ]
353362
, [ "pub enum " <> name <> " {" ]
354363
, indentLines variantTexts
355364
, [ "}" ]

0 commit comments

Comments
 (0)