Rust examples

Simple model

A simple model with some scalar properties.
{
  "definitions": {
    "Student": {
      "description": "A simple student struct",
      "type": "struct",
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "age": {
          "type": "integer"
        }
      }
    }
  },
  "root": "Student"
}

student.rs

use serde::{Serialize, Deserialize};

// A simple student struct
#[derive(Serialize, Deserialize)]
pub struct Student {
    #[serde(rename = "firstName")]
    first_name: Option<String>,

    #[serde(rename = "lastName")]
    last_name: Option<String>,

    #[serde(rename = "age")]
    age: Option<u64>,

}

Model with inheritance

A student struct which extends from a different struct.
{
  "definitions": {
    "Human": {
      "type": "struct",
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "age": {
          "type": "integer"
        }
      }
    },
    "Student": {
      "parent": {
        "type": "reference",
        "target": "Human"
      },
      "type": "struct",
      "properties": {
        "studentId": {
          "type": "string"
        }
      }
    }
  },
  "root": "Student"
}

human.rs

use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
pub struct Human {
    #[serde(rename = "firstName")]
    first_name: Option<String>,

    #[serde(rename = "lastName")]
    last_name: Option<String>,

    #[serde(rename = "age")]
    age: Option<u64>,

}

student.rs

use serde::{Serialize, Deserialize};
use human::Human;
#[derive(Serialize, Deserialize)]
pub struct Student {
    #[serde(rename = "firstName")]
    first_name: Option<String>,

    #[serde(rename = "lastName")]
    last_name: Option<String>,

    #[serde(rename = "age")]
    age: Option<u64>,

    #[serde(rename = "studentId")]
    student_id: Option<String>,

}

Model with reference

A student struct which reference a faculty struct.
{
  "definitions": {
    "Student": {
      "description": "A student struct with an assigned faculty",
      "type": "struct",
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "age": {
          "type": "integer"
        },
        "faculty": {
          "type": "reference",
          "target": "Faculty"
        }
      }
    },
    "Faculty": {
      "type": "struct",
      "properties": {
        "name": {
          "type": "string"
        }
      }
    }
  },
  "root": "Student"
}

student.rs

use serde::{Serialize, Deserialize};
use faculty::Faculty;

// A student struct with an assigned faculty
#[derive(Serialize, Deserialize)]
pub struct Student {
    #[serde(rename = "firstName")]
    first_name: Option<String>,

    #[serde(rename = "lastName")]
    last_name: Option<String>,

    #[serde(rename = "age")]
    age: Option<u64>,

    #[serde(rename = "faculty")]
    faculty: Option<Faculty>,

}

faculty.rs

use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
pub struct Faculty {
    #[serde(rename = "name")]
    name: Option<String>,

}

Map with string values

A student struct which contains a map with arbitrary string values.
{
  "definitions": {
    "Student": {
      "type": "struct",
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "age": {
          "type": "integer"
        },
        "properties": {
          "type": "map",
          "schema": {
            "type": "string"
          }
        }
      }
    }
  },
  "root": "Student"
}

student.rs

use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
pub struct Student {
    #[serde(rename = "firstName")]
    first_name: Option<String>,

    #[serde(rename = "lastName")]
    last_name: Option<String>,

    #[serde(rename = "age")]
    age: Option<u64>,

    #[serde(rename = "properties")]
    properties: Option<HashMap<String, String>>,

}

Model with discriminator

A struct which uses a discriminator mapping.
{
  "definitions": {
    "Human": {
      "type": "struct",
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "location": {
          "type": "reference",
          "target": "Location"
        }
      }
    },
    "Location": {
      "type": "struct",
      "base": true,
      "properties": {
        "type": {
          "type": "string"
        }
      },
      "discriminator": "type",
      "mapping": {
        "Web": "web",
        "World": "world"
      }
    },
    "Web": {
      "parent": {
        "type": "reference",
        "target": "Location"
      },
      "type": "struct",
      "properties": {
        "url": {
          "type": "string"
        }
      }
    },
    "World": {
      "parent": {
        "type": "reference",
        "target": "Location"
      },
      "type": "struct",
      "properties": {
        "lat": {
          "type": "string"
        },
        "long": {
          "type": "string"
        }
      }
    }
  },
  "root": "Human"
}

human.rs

use serde::{Serialize, Deserialize};
use location::Location;
#[derive(Serialize, Deserialize)]
pub struct Human {
    #[serde(rename = "firstName")]
    first_name: Option<String>,

    #[serde(rename = "lastName")]
    last_name: Option<String>,

    #[serde(rename = "location")]
    location: Option<Location>,

}

location.rs

use serde::{Serialize, Deserialize};
use web::Web;
use world::World;
#[derive(Serialize, Deserialize)]
pub struct Location {
    #[serde(rename = "type")]
    _type: Option<String>,

}

web.rs

use serde::{Serialize, Deserialize};
use location::Location;
#[derive(Serialize, Deserialize)]
pub struct Web {
    #[serde(rename = "type")]
    _type: Option<String>,

    #[serde(rename = "url")]
    url: Option<String>,

}

world.rs

use serde::{Serialize, Deserialize};
use location::Location;
#[derive(Serialize, Deserialize)]
pub struct World {
    #[serde(rename = "type")]
    _type: Option<String>,

    #[serde(rename = "lat")]
    lat: Option<String>,

    #[serde(rename = "long")]
    long: Option<String>,

}

Model with generics

A struct which uses generics.
{
  "definitions": {
    "Student": {
      "type": "object",
      "properties": {
        "matricleNumber": {
          "type": "integer"
        }
      }
    },
    "StudentMap": {
      "type": "struct",
      "parent": {
        "type": "reference",
        "target": "Map",
        "template": {
          "T": "Student"
        }
      }
    },
    "Map": {
      "type": "object",
      "properties": {
        "totalResults": {
          "type": "integer"
        },
        "entries": {
          "type": "array",
          "schema": {
            "type": "generic",
            "name": "T"
          }
        }
      }
    }
  },
  "root": "StudentMap"
}

student.rs

use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
pub struct Student {
    #[serde(rename = "matricleNumber")]
    matricle_number: Option<u64>,

}

student_map.rs

use serde::{Serialize, Deserialize};
use map::Map;
use student::Student;
#[derive(Serialize, Deserialize)]
pub struct StudentMap {
    #[serde(rename = "totalResults")]
    total_results: Option<u64>,

    #[serde(rename = "entries")]
    entries: Option<Vec<Student>>,

}

map.rs

use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
pub struct Map {
    #[serde(rename = "totalResults")]
    total_results: Option<u64>,

    #[serde(rename = "entries")]
    entries: Option<Vec<T>>,

}

Import other TypeSchema specification

A struct which references an external TypeSchema.
{
  "import": {
    "my_ns": "file:///generic.json"
  },
  "definitions": {
    "Faculty": {
      "type": "object",
      "properties": {
        "description": {
          "type": "string"
        },
        "students": {
          "type": "array",
          "schema": {
            "type": "reference",
            "target": "my_ns:StudentMap"
          }
        }
      }
    }
  },
  "root": "Faculty"
}

faculty.rs

use serde::{Serialize, Deserialize};
use student_map::StudentMap;
#[derive(Serialize, Deserialize)]
pub struct Faculty {
    #[serde(rename = "description")]
    description: Option<String>,

    #[serde(rename = "students")]
    students: Option<Vec<StudentMap>>,

}

Edit this page