Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XMLError to anyhow Error #10

Merged
merged 2 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "upnp-client"
version = "0.1.8"
version = "0.1.9"
edition = "2021"
repository = "https://github.com/tsirysndr/upnp-client-rs"
license = "MIT"
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ mod types;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let devices = discover_pnp_locations();
let devices = discover_pnp_locations().await?;
tokio::pin!(devices);

while let Some(device) = devices.next().await {
Expand Down Expand Up @@ -107,7 +107,7 @@ const KODI_MEDIA_RENDERER: &str = "Kodi - Media Renderer";

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let devices = discover_pnp_locations();
let devices = discover_pnp_locations().await?;
tokio::pin!(devices);

let mut kodi_device: Option<Device> = None;
Expand All @@ -120,7 +120,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}

let kodi_device = kodi_device.unwrap();
let device_client = DeviceClient::new(&kodi_device.location).connect().await?;
let device_client = DeviceClient::new(&kodi_device.location)?.connect().await?;
let media_renderer = MediaRendererClient::new(device_client);

let options = LoadOptions {
Expand Down Expand Up @@ -164,4 +164,4 @@ See the [examples](./examples) directory for more examples.
- [UPnP AV ContentDirectory v3 Service](http://upnp.org/specs/av/UPnP-av-ContentDirectory-v3-Service.pdf)

### License
MIT
MIT
32 changes: 13 additions & 19 deletions src/device_client.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
use std::{
collections::HashMap,
env,
net::TcpListener,
sync::{
Arc,
},
time::Duration,
};
use std::{collections::HashMap, env, net::TcpListener, sync::Arc, time::Duration};

use crate::{
parser::{
Expand Down Expand Up @@ -110,17 +102,17 @@ impl DeviceClient {

for (name, value) in params {
let mut param = XMLElement::new(name.as_str());
param.add_text(value)?;
action.add_child(param)?;
param.add_text(value).map_err(|e| anyhow!("{:?}", e))?;
action.add_child(param).map_err(|e| anyhow!("{:?}", e))?;
}

body.add_child(action)?;
envelope.add_child(body)?;
body.add_child(action).map_err(|e| anyhow!("{:?}", e))?;
envelope.add_child(body).map_err(|e| anyhow!("{:?}", e))?;

xml.set_root_element(envelope);

let mut writer: Vec<u8> = Vec::new();
xml.generate(&mut writer)?;
xml.generate(&mut writer).map_err(|e| anyhow!("{:?}", e))?;
let xml = String::from_utf8(writer)?;

let soap_action = format!("\"{}#{}\"", service.service_type, action_name);
Expand All @@ -136,10 +128,7 @@ impl DeviceClient {
.send()
.await
.map_err(|e| anyhow!(e.to_string()))?;
res
.body_string()
.await
.map_err(|e| anyhow!(e.to_string()))
res.body_string().await.map_err(|e| anyhow!(e.to_string()))
}

async fn get_service_description(&self, service_id: &str) -> Result<Service> {
Expand All @@ -148,7 +137,12 @@ impl DeviceClient {
.services
.iter()
.find(|s| s.service_id == service_id)
.ok_or_else(|| anyhow!("Service with requested service_id {} does not exist", service_id))?;
.ok_or_else(|| {
anyhow!(
"Service with requested service_id {} does not exist",
service_id
)
})?;
return Ok(service.clone());
}
Err(anyhow!("Device not connected"))
Expand Down
28 changes: 20 additions & 8 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ fn parse_attribute(xml_root: &str, xml_name: &str) -> Result<String> {
Some(element) => {
return Ok(element.text().to_string());
}
None => {
Ok("".to_string())
}
None => Ok("".to_string()),
}
}
None => Ok("".to_string()),
Expand Down Expand Up @@ -373,7 +371,10 @@ pub fn parse_current_play_mode(xml_root: &str) -> Result<Option<String>> {
let parser = EventReader::from_str(xml_root);
let mut current_play_mode: Option<String> = None;
for e in parser.into_iter().flatten() {
if let XmlEvent::StartElement { name, attributes, .. } = e {
if let XmlEvent::StartElement {
name, attributes, ..
} = e
{
if name.local_name == "CurrentPlayMode" {
for attr in attributes {
if attr.name.local_name == "val" {
Expand All @@ -390,7 +391,10 @@ pub fn parse_transport_state(xml_root: &str) -> Result<Option<String>> {
let parser = EventReader::from_str(xml_root);
let mut transport_state: Option<String> = None;
for e in parser.into_iter().flatten() {
if let XmlEvent::StartElement { name, attributes, .. } = e {
if let XmlEvent::StartElement {
name, attributes, ..
} = e
{
if name.local_name == "TransportState" {
for attr in attributes {
if attr.name.local_name == "val" {
Expand All @@ -407,7 +411,10 @@ pub fn parse_av_transport_uri_metadata(xml_root: &str) -> Result<Option<String>>
let parser = EventReader::from_str(xml_root);
let mut av_transport_uri_metadata: Option<String> = None;
for e in parser.into_iter().flatten() {
if let XmlEvent::StartElement { name, attributes, .. } = e {
if let XmlEvent::StartElement {
name, attributes, ..
} = e
{
if name.local_name == "AVTransportURIMetaData" {
for attr in attributes {
if attr.name.local_name == "val" {
Expand All @@ -424,7 +431,10 @@ pub fn parse_current_track_metadata(xml_root: &str) -> Result<Option<String>> {
let parser = EventReader::from_str(xml_root);
let mut current_track_metadata: Option<String> = None;
for e in parser.into_iter().flatten() {
if let XmlEvent::StartElement { name, attributes, .. } = e {
if let XmlEvent::StartElement {
name, attributes, ..
} = e
{
if name.local_name == "CurrentTrackMetaData" {
for attr in attributes {
if attr.name.local_name == "val" {
Expand Down Expand Up @@ -778,7 +788,9 @@ mod tests {
</device>
</root>"#;

let result = parse_services("http://xxxxxx:1337/", XML_ROOT).await;
let result = parse_services("http://xxxxxx:1337/", XML_ROOT)
.await
.unwrap();
assert_eq!(result.len(), 0);
}
}