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 = "PascalCase")]
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 if NewSize > MAX_BUFFER_SIZE {
89 return Err(format!("Buffer would exceed maximum size of {} bytes", MAX_BUFFER_SIZE));
90 }
91
92 self.Buffer.push_str(Content);
93 Ok(())
94 }
95
96 pub fn Clear(&mut self) { self.Buffer.clear(); }
98
99 pub fn GetBufferSize(&self) -> usize { self.Buffer.len() }
101
102 pub fn GetFormattedBufferSize(&self) -> String { FormatBytes(self.Buffer.len()) }
104
105 pub fn SetVisibility(&mut self, IsVisible:bool) { self.IsVisible = IsVisible; }
110}
111
112fn FormatBytes(Bytes:usize) -> String {
114 const UNITS:&[&str] = &["B", "KB", "MB", "GB"];
115
116 if Bytes == 0 {
117 return "0 B".to_string();
118 }
119
120 let mut Size = Bytes as f64;
121 let mut MutIndex = 0usize;
122
123 while Size >= 1024.0 && MutIndex < UNITS.len() - 1 {
124 Size /= 1024.0;
125 MutIndex += 1;
126 }
127
128 format!("{:.2} {}", Size, UNITS[MutIndex])
129}