| Crates.io | magnet_derive |
| lib.rs | magnet_derive |
| version | 0.8.0 |
| created_at | 2018-04-10 22:58:40.243524+00 |
| updated_at | 2019-01-02 10:55:20.677689+00 |
| description | Magnet, a JSON/BSON schema generator |
| homepage | https://h2co3.github.io/magnet/ |
| repository | https://github.com/H2CO3/magnet.git |
| max_upload_size | |
| id | 60016 |
| size | 35,784 |
These two related crates, magnet_derive and magnet_schema help you define (and, in most cases, automatically derive) MongoDB-flavored JSON schemas for your domain model types. Currently, the primary use case for this library is to make it easy to validate serializeable types when using Avocado or the MongoDB Rust driver.
The defined BsonSchema trait defines a single function, bson_schema, which should/will return a Bson Document that is a valid JSON schema describing the structure of the implementing type. Example:
#[macro_use]
extern crate serde_derive;
extern crate serde;
#[macro_use]
extern crate bson;
#[macro_use]
extern crate magnet_derive;
extern crate magnet_schema;
extern crate mongodb;
use std::collections::HashSet;
use magnet_schema::BsonSchema;
use mongodb::{ Client, ThreadedClient, CommandType };
use mongodb::db::{ ThreadedDatabase };
#[derive(BsonSchema)]
struct Person {
name: String,
nicknames: HashSet<String>,
age: usize,
contact: Option<Contact>,
}
#[derive(BsonSchema, Serialize, Deserialize)]
#[serde(tag = "type", content = "value")]
enum Contact {
Email(String),
Phone(u64),
}
fn main() {
let schema = Person::bson_schema();
let spec = doc! {
"create": "Person",
"validator": { "$jsonSchema": schema },
};
let client = Client::connect("localhost", 27017).expect("can't connect to mongod");
let db = client.db("Example");
db.command(spec, CommandType::CreateCollection, None).expect("network error");
// etc.
}
For milestones and custom #[attributes], please see the documentation.
BsonSchema for VecDeque, BinaryHeap, LinkedList, Range, RangeInclusive, and PhantomDataEq + Hash and Ord bounds on map keys and set elements where appropriateuuid dependency to 0.7.1, and include v4 and serde featuresurl dependency to 1.7.2impl BsonSchema for arrays of size 2N between 128 and 65536; and sizes 1.5 * 2N between 96 and 1536.syn::Generics::split_for_implmagnet_schema as wellimpl (which would result in a compiler error)bson to 0.13.0 and require its u2i feature. This version fixes a
bug where unit struct were serialized as 1-element arrays. The u2i feature
allows unsigned integers (within the appropriate range) to be serialized as
signed integers.Option<enum> was not allowed to be null/None by the
generated BSON schemaimpl BsonSchema for Documentimpl BsonSchema for ObjectIdDisplay bound for HashMap/BTreeMap keys, use ToString insteadproc_macro2 dependency so that we can use TokenStream::default()#[magnet(rename = "...")] attributeUnorderedDoc::eq(), assert_doc_eq! and assert_doc_ne! no longer clone their argumentssyn and quote dependenciesbson dependencyOption::bson_schema() didn't handle the bsonType field, so Option<integer> wasn't allowed to be null. This has been corrected.Bson::I64 for representing array lengths / collection countsimpl BsonSchema for { HashMap, BTreeMap } now has a less stringent trait bound on the key. It is now Display instead of AsRef<str>.#[magnet(min_incl = "...", min_excl = "...", max_incl = "...", max_excl = "...")] attributes on struct fields (named as well as newtype and tuple)enums, respecting Serde's tagging conventions (untagged/external/internal/adjacent), except newtype variants around other (inner) enums#[serde(rename_all = "...")] and #[serde(rename = "...")] attributes