icu4x icon indicating copy to clipboard operation
icu4x copied to clipboard

Add `schemars` support to ZeroVec

Open sffc opened this issue 3 years ago • 1 comments

Currently, the source of truth for ICU4X data file schemas are the data struct definitions in .rs files scattered throughout the repo. We may want to use schemars to generate JSON Schema files for ICU4X data structs in order to do the following:

  1. Use them as input to Postcard interpreters
  2. Potential future uses for interop with other libraries

This can be done as an optional schemars feature on zerovec, as discussed here: https://github.com/GREsau/schemars/issues/156

sffc avatar Jun 26 '22 23:06 sffc

Hello, I have been able to successfully create JsonSchema for ZeroVec (wrote a test to check). Shall I move on and implement for VarZeroVec & ZeroMap too?

Here is how I checked:

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(JsonSchema)]
    pub struct DataStruct<'data> {
        #[cfg_attr(feature = "serde", serde(borrow))]
        nums: ZeroVec<'data, u32>,

        #[cfg_attr(feature = "serde", serde(borrow))]
        chars: ZeroVec<'data, char>,
    }
    #[test]
    fn check_schema(){
        let gen = SchemaGenerator::default();
        let schema = gen.into_root_schema_for::<DataStruct>();
        let schema_json = serde_json::to_string_pretty(&schema).expect("Failed to serialize schema");
        let parsed_schema: Value = serde_json::from_str(&schema_json).expect("Failed to parse schema JSON");

        // Check for the existence of "ZeroVec<Character>" and "ZeroVec<uint32>" in `definitions``
        let definitions = parsed_schema.get("definitions").expect("No definitions found in schema");
        assert!(definitions.get("ZeroVec<Character>").is_some(), "Definition for ZeroVec<Character> not found");
        assert!(definitions.get("ZeroVec<uint32>").is_some(), "Definition for ZeroVec<uint32> not found");
    }

ashu26jha avatar Apr 09 '24 06:04 ashu26jha