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

replace xml-rs with quick-xml #22

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Expand Up @@ -15,7 +15,7 @@ edition = '2018'
default-target = "x86_64-pc-windows-msvc"

[dependencies]
xml-rs = "0.8.4"
quick-xml = "0.23.0"
strum = { version = "0.22.0", features = ["derive"] }

[target.'cfg(target_env = "msvc")'.dependencies.windows]
Expand Down
6 changes: 3 additions & 3 deletions examples/without_library.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// How to create a toast without using this library

extern crate xml;
extern crate quick_xml;
use quick_xml::escape::escape;
use std::path::Path;
use xml::escape::escape_str_attribute;

// You need to have the windows crate in your Cargo.toml
// with the following features:
Expand Down Expand Up @@ -44,7 +44,7 @@ fn do_toast() -> windows::runtime::Result<()> {
<audio src="ms-winsoundevent:Notification.SMS" />
<!-- <audio silent="true" /> -->
</toast>"#,
escape_str_attribute(&Path::new("C:\\path_to_image_in_toast.jpg").display().to_string()),
std::str::from_utf8(escape(Path::new("C:\\path_to_image_in_toast.jpg").display().to_string().as_bytes()).as_ref()).expect("valid utf8"),
))).expect("the xml is malformed");

// Create the toast and attach event listeners
Expand Down
14 changes: 12 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//!
//! * Windows 8.1 only supports a single image, the last image (icon, hero, image) will be the one on the toast

extern crate quick_xml;
/// for xml schema details check out:
///
/// * https://docs.microsoft.com/en-us/uwp/schemas/tiles/toastschema/root-elements
Expand All @@ -28,7 +29,6 @@

/// For actions look at https://docs.microsoft.com/en-us/dotnet/api/microsoft.toolkit.uwp.notifications.toastactionscustom?view=win-comm-toolkit-dotnet-7.0
extern crate windows;
extern crate xml;

#[macro_use]
extern crate strum;
Expand All @@ -40,7 +40,8 @@ use windows::{

use std::path::Path;

use xml::escape::escape_str_attribute;
use quick_xml::escape::escape as escape_attr;
use std::borrow::Cow;
mod windows_check;

pub use windows::runtime::{Error, HSTRING, Result};
Expand Down Expand Up @@ -337,6 +338,15 @@ impl Toast {
}
}

fn escape_str_attribute(s: &str) -> Cow<'_, str> {
let escaped = escape_attr(s.as_bytes());
match escaped {
Cow::Borrowed(_) => Cow::Borrowed(s),
// This is safe because we know that the string was valid UTF-8
Cow::Owned(e) => Cow::Owned(String::from_utf8(e).unwrap()),
}
}

#[cfg(test)]
mod tests {
use crate::*;
Expand Down