mcfunction_debugger/dap/
codec.rs

1// Mcfunction-Debugger is a debugger for Minecraft's *.mcfunction files that does not require any
2// Minecraft mods.
3//
4// © Copyright (C) 2021-2024 Adrodoc <adrodoc55@googlemail.com> & Skagaros <skagaros@gmail.com>
5//
6// This file is part of Mcfunction-Debugger.
7//
8// Mcfunction-Debugger is free software: you can redistribute it and/or modify it under the terms of
9// the GNU General Public License as published by the Free Software Foundation, either version 3 of
10// the License, or (at your option) any later version.
11//
12// Mcfunction-Debugger is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License along with Mcfunction-Debugger.
17// If not, see <http://www.gnu.org/licenses/>.
18
19use bytes::{Buf, BytesMut};
20use debug_adapter_protocol::ProtocolMessage;
21use std::{collections::BTreeMap, io};
22use tokio_util::codec::{Decoder, Encoder};
23
24pub struct ProtocolMessageEncoder;
25impl Encoder<ProtocolMessage> for ProtocolMessageEncoder {
26    type Error = std::io::Error;
27
28    fn encode(&mut self, item: ProtocolMessage, dst: &mut BytesMut) -> Result<(), Self::Error> {
29        const HEADER_PREFIX: &str = "Content-Length: ";
30        const HEADER_DELIMITER: &str = "\r\n\r\n";
31        let json = serde_json::to_string(&item).unwrap();
32        let content_length = json.len().to_string();
33        dst.reserve(
34            HEADER_PREFIX.len() + content_length.len() + HEADER_DELIMITER.len() + json.len(),
35        );
36        dst.extend_from_slice(HEADER_PREFIX.as_bytes());
37        dst.extend_from_slice(content_length.as_bytes());
38        dst.extend_from_slice(HEADER_DELIMITER.as_bytes());
39        dst.extend_from_slice(json.as_bytes());
40        Ok(())
41    }
42}
43
44pub struct ProtocolMessageDecoder;
45impl Decoder for ProtocolMessageDecoder {
46    type Item = ProtocolMessage;
47    type Error = io::Error;
48
49    fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
50        let string = std::str::from_utf8(src).map_err(|e| invalid_data(e))?;
51        if let Some((header_len, content_length)) = read_header(string)? {
52            let message_len = header_len + content_length;
53            if string.len() < message_len {
54                Ok(None)
55            } else {
56                let content = &string[header_len..message_len];
57                let message = serde_json::from_str(content)?;
58                src.advance(message_len);
59                Ok(message)
60            }
61        } else {
62            Ok(None)
63        }
64    }
65}
66
67const CONTENT_LENGTH: &str = "Content-Length";
68
69fn read_header(string: &str) -> Result<Option<(usize, usize)>, io::Error> {
70    const HEADER_DELIMITER: &str = "\r\n\r\n";
71    let header_end = if let Some(header_end) = string.find(HEADER_DELIMITER) {
72        header_end
73    } else {
74        return Ok(None);
75    };
76    let mut header = BTreeMap::new();
77
78    for line in string[..header_end].split("\r\n") {
79        let (key, value) = line.split_once(": ").ok_or_else(|| {
80            invalid_data(format!(
81                "Key and value of header field not seperated by a colon and a space: '{}'",
82                line
83            ))
84        })?;
85        header.insert(key, value);
86    }
87    let content_length = get_content_length(&header)?;
88    Ok(Some((header_end + HEADER_DELIMITER.len(), content_length)))
89}
90
91fn get_content_length(header: &BTreeMap<&str, &str>) -> io::Result<usize> {
92    let content_length = &header
93        .get(CONTENT_LENGTH)
94        .ok_or_else(|| invalid_data(format!("Missing header '{}'", CONTENT_LENGTH)))?;
95    let content_length = content_length.parse().map_err(|_| {
96        invalid_data(format!(
97            "Header '{}' does not have usize value: {}",
98            CONTENT_LENGTH, content_length
99        ))
100    })?;
101    Ok(content_length)
102}
103
104fn invalid_data<E>(error: E) -> io::Error
105where
106    E: Into<Box<dyn std::error::Error + Send + Sync>>,
107{
108    io::Error::new(io::ErrorKind::InvalidData, error)
109}