Mountain/ApplicationState/DTO/
OutputChannelStateDTO.rs1use serde::{Deserialize, Serialize};
15
16const MAX_CHANNEL_NAME_LENGTH:usize = 128;
18
19const MAX_LANGUAGE_ID_LENGTH:usize = 128;
21
22const MAX_BUFFER_SIZE:usize = 10_000_000;
26
27#[derive(Serialize, Deserialize, Clone, Debug, Default)]
30#[serde(rename_all = "camelCase")]
31pub struct OutputChannelStateDTO {
32 #[serde(skip_serializing_if = "String::is_empty")]
34 pub Name:String,
35
36 #[serde(skip_serializing_if = "Option::is_none")]
38 pub LanguageIdentifier:Option<String>,
39
40 #[serde(skip_serializing_if = "String::is_empty")]
42 pub Buffer:String,
43
44 pub IsVisible:bool,
46}
47
48impl OutputChannelStateDTO {
49 pub fn Create(Name:&str, LanguageIdentifier:Option<String>) -> Result<Self, String> {
58 if Name.len() > MAX_CHANNEL_NAME_LENGTH {
60 return Err(format!(
61 "Channel name exceeds maximum length of {} bytes",
62 MAX_CHANNEL_NAME_LENGTH
63 ));
64 }
65
66 if let Some(LangID) = &LanguageIdentifier {
68 if LangID.len() > MAX_LANGUAGE_ID_LENGTH {
69 return Err(format!(
70 "Language identifier exceeds maximum length of {} bytes",
71 MAX_LANGUAGE_ID_LENGTH
72 ));
73 }
74 }
75
76 Ok(Self { Name:Name.to_string(), LanguageIdentifier, Buffer:String::new(), IsVisible:false })
77 }
78
79 pub fn Append(&mut self, Content:&str) -> Result<(), String> {
87 let NewSize = self.Buffer.len() + Content.len();
88
89 if NewSize > MAX_BUFFER_SIZE {
90 return Err(format!("Buffer would exceed maximum size of {} bytes", MAX_BUFFER_SIZE));
91 }
92
93 self.Buffer.push_str(Content);
94
95 Ok(())
96 }
97
98 pub fn Clear(&mut self) { self.Buffer.clear(); }
100
101 pub fn GetBufferSize(&self) -> usize { self.Buffer.len() }
103
104 pub fn GetFormattedBufferSize(&self) -> String { FormatBytes(self.Buffer.len()) }
106
107 pub fn SetVisibility(&mut self, IsVisible:bool) { self.IsVisible = IsVisible; }
112}
113
114fn FormatBytes(Bytes:usize) -> String {
116 const UNITS:&[&str] = &["B", "KB", "MB", "GB"];
117
118 if Bytes == 0 {
119 return "0 B".to_string();
120 }
121
122 let mut Size = Bytes as f64;
123
124 let mut MutIndex = 0usize;
125
126 while Size >= 1024.0 && MutIndex < UNITS.len() - 1 {
127 Size /= 1024.0;
128
129 MutIndex += 1;
130 }
131
132 format!("{:.2} {}", Size, UNITS[MutIndex])
133}