devtools/
resource.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use serde::Serialize;
6
7use crate::protocol::JsonPacketStream;
8
9pub enum ResourceArrayType {
10    Available,
11    Updated,
12}
13
14#[derive(Serialize)]
15pub(crate) struct ResourceAvailableReply<T: Serialize> {
16    pub from: String,
17    #[serde(rename = "type")]
18    pub type_: String,
19    pub array: Vec<(String, Vec<T>)>,
20}
21
22pub(crate) trait ResourceAvailable {
23    fn actor_name(&self) -> String;
24
25    fn resource_array<T: Serialize, S: JsonPacketStream>(
26        &self,
27        resource: T,
28        resource_type: String,
29        array_type: ResourceArrayType,
30        stream: &mut S,
31    ) {
32        self.resources_array(vec![resource], resource_type, array_type, stream);
33    }
34
35    fn resources_array<T: Serialize, S: JsonPacketStream>(
36        &self,
37        resources: Vec<T>,
38        resource_type: String,
39        array_type: ResourceArrayType,
40        stream: &mut S,
41    ) {
42        let msg = ResourceAvailableReply::<T> {
43            from: self.actor_name(),
44            type_: match array_type {
45                ResourceArrayType::Available => "resources-available-array".to_string(),
46                ResourceArrayType::Updated => "resources-updated-array".to_string(),
47            },
48            array: vec![(resource_type, resources)],
49        };
50
51        let _ = stream.write_json_packet(&msg);
52    }
53}