devtools/actors/
long_string.rs1use malloc_size_of_derive::MallocSizeOf;
5use serde::Serialize;
6use serde_json::{Map, Value};
7
8use crate::StreamId;
9use crate::actor::{Actor, ActorError, ActorRegistry};
10use crate::protocol::ClientRequest;
11
12const INITIAL_LENGTH: usize = 500;
13
14#[derive(MallocSizeOf)]
15pub(crate) struct LongStringActor {
16 name: String,
17 full_string: String,
18}
19
20#[derive(Clone, Serialize)]
21#[serde(rename_all = "camelCase")]
22pub(crate) struct LongStringObj {
23 #[serde(rename = "type")]
24 type_: String,
25 actor: String,
26 length: usize,
27 initial: String,
28}
29
30#[derive(Serialize)]
31#[serde(rename_all = "camelCase")]
32struct SubstringReply {
33 from: String,
34 substring: String,
35}
36
37impl Actor for LongStringActor {
38 fn name(&self) -> String {
39 self.name.clone()
40 }
41
42 fn handle_message(
43 &self,
44 request: ClientRequest,
45 _registry: &ActorRegistry,
46 msg_type: &str,
47 msg: &Map<String, Value>,
48 _id: StreamId,
49 ) -> Result<(), ActorError> {
50 match msg_type {
51 "substring" => {
52 let start = msg.get("start").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
53 let end = msg
54 .get("end")
55 .and_then(|v| v.as_u64())
56 .unwrap_or(self.full_string.len() as u64) as usize;
57 let substring: String = self
58 .full_string
59 .chars()
60 .skip(start)
61 .take(end - start)
62 .collect();
63 let reply = SubstringReply {
64 from: self.name(),
65 substring,
66 };
67 request.reply_final(&reply)?
68 },
69 _ => return Err(ActorError::UnrecognizedPacketType),
70 }
71 Ok(())
72 }
73}
74
75impl LongStringActor {
76 pub fn register(registry: &ActorRegistry, full_string: String) -> String {
77 let name = registry.new_name::<Self>();
78 let actor = Self {
79 name: name.clone(),
80 full_string,
81 };
82 registry.register::<Self>(actor);
83 name
84 }
85
86 pub fn long_string_obj(&self) -> LongStringObj {
87 LongStringObj {
88 type_: "longString".to_string(),
89 actor: self.name.clone(),
90 length: self.full_string.len(),
91 initial: self.full_string.chars().take(INITIAL_LENGTH).collect(),
92 }
93 }
94}