Skip to content

Conversation

RaulTrombin
Copy link
Member

Details:
Add json schema feature for PingProtocol definitions,

tested on PingViewerNext:
image
image

Details of jsonschema:
message_schema.json

...
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Messages",
  "oneOf": [
    {
      "type": "object",
      "properties": {
        "Bluebps": {
          "$ref": "#/$defs/Messages"
        }
      },
      "additionalProperties": false,
      "required": [
        "Bluebps"
      ]
    },
    {
      "type": "object",
      "properties": {
        "Omniscan450": {
          "$ref": "#/$defs/Messages2"
        }
      },
      "additionalProperties": false,
      "required": [
        "Omniscan450"
      ]
    },
    {
      "type": "object",
      "properties": {
        "Ping1D": {
          "$ref": "#/$defs/Messages3"
        }
      },
      "additionalProperties": false,
      "required": [
        "Ping1D"
      ]
    },
    ...

@joaoantoniocardoso
Copy link
Member

"$ref": "#/$defs/Messages"
"$ref": "#/$defs/Messages2"
"$ref": "#/$defs/Messages3"
...

Coudln't this have a better naming, maybe something like "BluebpsMessage", etc?

@RaulTrombin
Copy link
Member Author

RaulTrombin commented Jun 18, 2025

"$ref": "#/$defs/Messages"
"$ref": "#/$defs/Messages2"
"$ref": "#/$defs/Messages3"
...

Coudln't this have a better naming, maybe something like "BluebpsMessage", etc?

I didn't notice the numerations. In fact, we have a set of Messages for each structure, so schemars automatically indexed it, which was smart haha.

https://docs.bluerobotics.com/ping-rs/bluerobotics_ping/ping1d/enum.Messages.html

Let me check if we can change just the ref IDs to something from our builder/macro.

@RaulTrombin
Copy link
Member Author

@joaoantoniocardoso seems it breaks all other generated refs.
image

@RaulTrombin
Copy link
Member Author

@joaoantoniocardoso did it, just let me organize it

image

@RaulTrombin
Copy link
Member Author

message_schema.json

Copy link
Member

@joaoantoniocardoso joaoantoniocardoso left a comment

Choose a reason for hiding this comment

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

Nice job, I have just some small (and optional) suggestions :)

fn save_schema(filename: &str) {
let schema = schema_for!(Messages);
let schema_json = serde_json::to_string_pretty(&schema).unwrap();
let mut file = File::create(filename).unwrap();
Copy link
Member

Choose a reason for hiding this comment

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

We could use tmp dir here (not tested):

        let mut path = std::env::temp_dir();
        path.join(filename);
        let mut file = std::fs::File::create(&path).unwrap();

But I see that for manual inspection, it can be useful to create it in an more easily accessible place. In this case, you can add a "tests/tmp" folder to ".gitignore" and use it instead (not tested):

        let mut path = std::fs::Path::new("tests/tmp").unwrap();
        create_dir_all(path).unwrap();
        path.join(filename);
        let mut file = std::fs::File::create(&path).unwrap();

obviously, the load_schema function would have to adapt.

Copy link
Member Author

Choose a reason for hiding this comment

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

Approach 2 seems good, we can improve this afterward with some WASM example

@RaulTrombin
Copy link
Member Author

ping @patrickelectric

@RaulTrombin RaulTrombin merged commit eb427ad into bluerobotics:master Jun 18, 2025
9 of 10 checks passed
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