arib-cli 0.1.0

Reads the signalling of ARIB broadcasts and descrambles them, as an example of the arib crate
//! What the commands print with `--json`, with the IDs as numbers and the rest as it is printed.

use serde_json::{Value, json};

use crate::component::Stream;
use crate::si::{Event, Network, Service};

pub fn network(network: &Network) -> Value {
    json!({
        "id": network.id,
        "name": network.name,
        "streams": network.streams.iter().map(|stream| json!({
            "id": stream.id,
            "original_network_id": stream.original_network_id,
            "service_ids": stream.service_ids,
        })).collect::<Vec<_>>(),
    })
}

pub fn service(service: &Service) -> Value {
    json!({
        "id": service.id,
        "type": service.service_type,
        "name": service.name,
        "provider": service.provider,
    })
}

pub fn stream(stream: &Stream, details: &[String]) -> Value {
    json!({
        "id": stream.id,
        "component_tag": stream.component_tag,
        "kind": stream.kind.to_string(),
        "codec": stream.codec,
        "details": details,
    })
}

pub fn event(event: &Event) -> Value {
    json!({
        "id": event.id,
        // The times of ARIB are in JST.
        "start_time": event
            .start_time
            .map(|time| time.format("%Y-%m-%dT%H:%M:%S+09:00").to_string()),
        "duration": event.duration.map(|duration| duration.num_seconds()),
        "name": event.name,
        "text": event.text,
        "genres": event.genres.iter().map(ToString::to_string).collect::<Vec<_>>(),
        "scrambled": event.scrambled,
        "details": event.details.iter().map(|(description, item)| json!({
            "description": description,
            "item": item,
        })).collect::<Vec<_>>(),
        "components": event.components.iter().map(|component| json!({
            "component_tag": component.component_tag,
            "details": component.details,
        })).collect::<Vec<_>>(),
    })
}