Skip to content

Commit 84d6812

Browse files
committed
Fix property value decoding
1 parent 8a05a78 commit 84d6812

File tree

6 files changed

+299
-17
lines changed

6 files changed

+299
-17
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
main
5+
----
6+
7+
- Fix property value decoding #19

Cargo.lock

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ better-panic = "0.3.0"
1010
clap = { version = "4.5.35", features = ["derive"] }
1111
crossterm = "0.28.1"
1212
log = "0.4.27"
13+
pretty_assertions = "1.4.1"
1314
ratatui = "0.29.0"
1415
serde = { version = "1.0.219", features = ["derive"] }
1516
simple-logging = "2.0.2"

src/dbgp/client.rs

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ fn parse_source(element: &Element) -> Result<String, anyhow::Error> {
307307
fn parse_context_get(element: &mut Element) -> Result<ContextGetResponse, anyhow::Error> {
308308
let mut properties: Vec<Property> = vec![];
309309
while let Some(mut child) = element.take_child("property") {
310+
let encoding = child.attributes.get("encoding").map(|s| s.to_string());
310311
let p = Property {
311312
name: child.attributes
312313
.get("name")
@@ -329,14 +330,18 @@ fn parse_context_get(element: &mut Element) -> Result<ContextGetResponse, anyhow
329330
size: child.attributes.get("size").map(|s| s.parse::<u32>().unwrap()),
330331
key: child.attributes.get("key").map(|name| name.to_string()),
331332
address: child.attributes.get("address").map(|name| name.to_string()),
332-
encoding: child.attributes.get("encoding").map(|s| s.to_string()),
333+
encoding: encoding.clone(),
333334
children: parse_context_get(&mut child).unwrap().properties,
334335
value: match child.children.first() {
335-
Some(XMLNode::CData(cdata)) => Some(String::from_utf8(
336-
general_purpose::STANDARD.decode(
337-
cdata
338-
).unwrap_or(vec![])
339-
).unwrap_or("".to_string())),
336+
Some(XMLNode::CData(cdata)) => Some(
337+
match encoding {
338+
Some(encoding) => match encoding.as_str() {
339+
"base64" => String::from_utf8(general_purpose::STANDARD.decode(cdata).unwrap()).unwrap(),
340+
_ => cdata.to_string(),
341+
},
342+
_ => cdata.to_string(),
343+
}
344+
),
340345
_ => None,
341346
}
342347
};
@@ -396,6 +401,7 @@ fn parse_continuation_response(
396401
#[cfg(test)]
397402
mod test {
398403
use super::*;
404+
use pretty_assertions::{assert_eq, assert_ne};
399405

400406
#[test]
401407
fn test_parse_xml() -> Result<(), anyhow::Error> {
@@ -486,7 +492,17 @@ function call_function(string $hello) {
486492
#[test]
487493
fn test_parse_context_get() -> Result<(), anyhow::Error> {
488494
let result = parse_xml(
489-
r#"<response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="context_get" transaction_id="4" context="0"><property name="$bar" fullname="$bar" type="string" size="3" encoding="base64"><![CDATA[Zm9v]]></property><property name="$true" fullname="$true" type="bool"><![CDATA[1]]></property><property name="$this" fullname="$this" type="object" classname="Foo" children="1" numchildren="2" page="0" pagesize="32"><property name="true" fullname="$this-&gt;true" facet="public" type="bool"><![CDATA[1]]></property><property name="bar" fullname="$this-&gt;bar" facet="public" type="string" size="3" encoding="base64"><![CDATA[Zm9v]]></property></property></response>"#,
495+
r#"
496+
<response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="context_get" transaction_id="4" context="0">
497+
<property name="$bar" fullname="$bar" type="string" size="3" encoding="base64"><![CDATA[Zm9v]]></property>
498+
<property name="$float" fullname="$float" type="float"><![CDATA[123.4]]></property>
499+
<property name="$int" fullname="$int" type="int"><![CDATA[123]]></property>
500+
<property name="$true" fullname="$true" type="bool"><![CDATA[1]]></property>
501+
<property name="$this" fullname="$this" type="object" classname="Foo" children="1" numchildren="2" page="0" pagesize="32">
502+
<property name="true" fullname="$this-&gt;true" facet="public" type="bool"><![CDATA[1]]></property>
503+
<property name="bar" fullname="$this-&gt;bar" facet="public" type="string" size="3" encoding="base64"><![CDATA[Zm9v]]></property>
504+
</property>
505+
</response>"#,
490506
)?;
491507

492508
match result {
@@ -510,6 +526,36 @@ function call_function(string $hello) {
510526
encoding: Some("base64".to_string()),
511527
value: Some("foo".to_string()),
512528
},
529+
Property {
530+
name: "$float".to_string(),
531+
fullname: "$float".to_string(),
532+
classname: None,
533+
page: None,
534+
pagesize: None,
535+
property_type: "float".to_string(),
536+
facet: None,
537+
size: None,
538+
children: vec![],
539+
key: None,
540+
address: None,
541+
encoding: None,
542+
value: Some("123.4".to_string()),
543+
},
544+
Property {
545+
name: "$int".to_string(),
546+
fullname: "$int".to_string(),
547+
classname: None,
548+
page: None,
549+
pagesize: None,
550+
property_type: "int".to_string(),
551+
facet: None,
552+
size: None,
553+
children: vec![],
554+
key: None,
555+
address: None,
556+
encoding: None,
557+
value: Some("123".to_string()),
558+
},
513559
Property {
514560
name: "$true".to_string(),
515561
fullname: "$true".to_string(),
@@ -523,7 +569,7 @@ function call_function(string $hello) {
523569
key: None,
524570
address: None,
525571
encoding: None,
526-
value: Some("".to_string()),
572+
value: Some("1".to_string()),
527573
},
528574
Property {
529575
name: "$this".to_string(),
@@ -548,7 +594,7 @@ function call_function(string $hello) {
548594
key: None,
549595
address: None,
550596
encoding: None,
551-
value: Some("".to_string()),
597+
value: Some("1".to_string()),
552598
},
553599
Property {
554600
name: "bar".to_string(),

src/view/context.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
use ratatui::{layout::Rect, style::{Color, Style}, text::{Line, Span}, widgets::{Paragraph, Wrap}, Frame};
2-
3-
use crate::{app::App, dbgp::client::Property, event::input::AppEvent};
4-
51
use super::View;
2+
use crate::app::App;
3+
use crate::dbgp::client::Property;
4+
use crate::event::input::AppEvent;
5+
use ratatui::layout::Rect;
6+
use ratatui::style::Color;
7+
use ratatui::style::Style;
8+
use ratatui::text::Line;
9+
use ratatui::text::Span;
10+
use ratatui::widgets::Paragraph;
11+
use ratatui::widgets::Wrap;
12+
use ratatui::Frame;
613

7-
pub struct ContextComponent {
8-
}
14+
pub struct ContextComponent {}
915

1016
impl View for ContextComponent {
1117
fn handle(_app: &App, event: AppEvent) -> Option<AppEvent> {
@@ -24,7 +30,12 @@ impl View for ContextComponent {
2430
let mut lines: Vec<Line> = vec![];
2531
draw_properties(&context.properties, &mut lines, 0);
2632

27-
frame.render_widget(Paragraph::new(lines).wrap(Wrap{trim: false}).scroll((app.session_view.context_scroll, 0)), area);
33+
frame.render_widget(
34+
Paragraph::new(lines)
35+
.wrap(Wrap { trim: false })
36+
.scroll((app.session_view.context_scroll, 0)),
37+
area,
38+
);
2839
}
2940
}
3041

@@ -35,7 +46,10 @@ pub fn draw_properties(properties: &Vec<Property>, lines: &mut Vec<Line>, level:
3546
Span::raw(" ".repeat(level)),
3647
Span::styled(property.name.to_string(), Style::default().fg(Color::White)),
3748
Span::raw(" ".to_string()),
38-
Span::styled(property.property_type.to_string(), Style::default().fg(Color::Blue)),
49+
Span::styled(
50+
property.property_type.to_string(),
51+
Style::default().fg(Color::Blue),
52+
),
3953
Span::raw(" = ".to_string()),
4054
match property.property_type.as_str() {
4155
"bool" => Span::styled(value, Style::default().fg(Color::LightRed)),

0 commit comments

Comments
 (0)