/// Deserializes JSON document into the appropriate metadata container.
///
/// The type resolution is done by the unique schema ID.
Deserializes JSON document into the appropriate metadata container.
The type resolution is done by the unique schema ID.
Deserializes JSON document into the appropriate metadata container . The type resolution is done by the unique schema ID .
pub fn from_reader<R: Read>(mut source: R) -> Result<Metadata> {
    let mut buf = String::new();
    source.read_to_string(&mut buf)?;
    let envelope = Envelope::<Data>::new(buf.as_bytes())?;
    match SchemaId::from(&envelope.schema_id) {
        Some(schema_id) => {
            let metadata = match schema_id {
                SchemaId::PhysicalDeviceV1 => {
                    let e = Envelope::<PhysicalDeviceV1>::new(buf.as_bytes())?;
                    e.validate()?;
                    Metadata::PhysicalDeviceV1(e.data)
                }
                SchemaId::ProductBundleV1 => {
                    let e = Envelope::<ProductBundleV1>::new(buf.as_bytes())?;
                    e.validate()?;
                    Metadata::ProductBundleV1(e.data)
                }
                SchemaId::ProductBundleContainerV1 => {
                    let e = Envelope::<ProductBundleContainerV1>::new(buf.as_bytes())?;
                    e.validate()?;
                    Metadata::ProductBundleContainerV1(e.data)
                }
                SchemaId::ProductBundleContainerV2 => {
                    let e = Envelope::<ProductBundleContainerV2>::new(buf.as_bytes())?;
                    e.validate()?;
                    Metadata::ProductBundleContainerV2(e.data)
                }
                SchemaId::VirtualDeviceV1 => {
                    let e = Envelope::<VirtualDeviceV1>::new(buf.as_bytes())?;
                    e.validate()?;
                    Metadata::VirtualDeviceV1(e.data)
                }
            };
            Ok(metadata)
        }
        None => Err(anyhow!(
            "Unknown schema id {} for type {:?}.",
            &envelope.schema_id,
            envelope.data.kind
        )),
    }
}
pub fn from_reader < R : Read > ( mut source : R ) -> Result < Metadata > { let mut buf = String :: new ( ) ; source . read_to_string ( & mut buf ) ? ; let envelope = Envelope :: < Data > :: new ( buf . as_bytes ( ) ) ? ; match SchemaId :: from ( & envelope . schema_id ) { Some ( schema_id ) => { let metadata = match schema_id { SchemaId :: PhysicalDeviceV1 => { let e = Envelope :: < PhysicalDeviceV1 > :: new ( buf . as_bytes ( ) ) ? ; e . validate ( ) ? ; Metadata :: PhysicalDeviceV1 ( e . data ) } SchemaId :: ProductBundleV1 => { let e = Envelope :: < ProductBundleV1 > :: new ( buf . as_bytes ( ) ) ? ; e . validate ( ) ? ; Metadata :: ProductBundleV1 ( e . data ) } SchemaId :: ProductBundleContainerV1 => { let e = Envelope :: < ProductBundleContainerV1 > :: new ( buf . as_bytes ( ) ) ? ; e . validate ( ) ? ; Metadata :: ProductBundleContainerV1 ( e . data ) } SchemaId :: ProductBundleContainerV2 => { let e = Envelope :: < ProductBundleContainerV2 > :: new ( buf . as_bytes ( ) ) ? ; e . validate ( ) ? ; Metadata :: ProductBundleContainerV2 ( e . data ) } SchemaId :: VirtualDeviceV1 => { let e = Envelope :: < VirtualDeviceV1 > :: new ( buf . as_bytes ( ) ) ? ; e . validate ( ) ? ; Metadata :: VirtualDeviceV1 ( e . data ) } } ; Ok ( metadata ) } None => Err ( anyhow ! ( " " envelope schema_id envelope data kind ) ) , } }
Deserializes JSON document into the appropriate metadata container.
Deserializes JSON document into the appropriate metadata container .
source
R