Multi-typed entities | schema-org-rust | Spatie       

 SPATIE  

schema-org-rust
===============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Schema-org-rust](https://spatie.be/docs/schema-org-rust/v1)  Multi-typed entities

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/schema-org-rust/v1) 

  Multi-typed entities    
- [ Introduction ](https://spatie.be/docs/schema-org-rust/v1/introduction)
- [ Support us ](https://spatie.be/docs/schema-org-rust/v1/support-us)
- [ Installation ](https://spatie.be/docs/schema-org-rust/v1/installation-and-setup)
- [ Usage ](https://spatie.be/docs/schema-org-rust/v1/usage)
- [ Graphs ](https://spatie.be/docs/schema-org-rust/v1/graphs)
- [ Multi-typed entities ](https://spatie.be/docs/schema-org-rust/v1/multi-typed-entities)
- [ Generating types ](https://spatie.be/docs/schema-org-rust/v1/generating-types)
- [ Testing ](https://spatie.be/docs/schema-org-rust/v1/testing)
- [ Changelog ](https://spatie.be/docs/schema-org-rust/v1/changelog)

 Multi-typed entities
====================

Schema.org permits a single entity to have several types. `MultiTypedEntity` merges component builders into one JSON-LD object with an `@type` array.

```
use spatie_schema_org::{JsonLd, MultiTypedEntity, Schema};

let mut room = MultiTypedEntity::new();
room.hotel_room().name("The Presidential Suite");
room.product().offers(
    Schema::offer().name("One Night").price(100000).price_currency("USD")
);
room.product().aggregate_rating(
    Schema::aggregate_rating().best_rating(5).worst_rating(4)
);

assert_eq!(room.to_value()["@type"], serde_json::json!(["HotelRoom", "Product"]));
assert_eq!(room.to_value()["offers"]["priceCurrency"], "USD");
```

Repeated calls to an accessor return the same component. Components merge in insertion order, with later components winning property conflicts. Each type appears once. An empty collection renders an empty `@type` array.

`add` rejects an existing type with `CollectionError::AlreadyExists`. `set` inserts or replaces a component, keeping its position. `has` checks membership. `get` returns a shared component handle or `CollectionError::NotFound`. `get_or_create` accepts a vocabulary type name and returns `CollectionError::InvalidType` for unknown names. `get_nodes` returns all component handles.

```
let mut entity = MultiTypedEntity::new();
entity.add(Schema::hotel_room().name("Suite"))?;
entity.set(Schema::product().name("Suite booking"));
assert!(entity.has("HotelRoom"));
assert_eq!(entity.to_value()["name"], "Suite booking");
entity.when(true, |entity| { entity.product().description("One night"); });
entity.set_nonce("your-csp-nonce");
assert!(entity.to_script().contains("nonce=\"your-csp-nonce\""));
```

Multi-typed entities can be nested in other builders. Nested output omits `@context`. `to_value`, `to_json`, `to_script`, `Display`, and Serde serialization work just as they do for a single type.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
