Skip to content

Commit

Permalink
feat: add conversion from string for MimeType
Browse files Browse the repository at this point in the history
  • Loading branch information
wash2 committed Mar 25, 2024
1 parent 3118f11 commit 301d803
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/dnd/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use crate::Clipboard;

impl Clipboard {
/// Set up DnD operations for the Clipboard
pub fn init_dnd() {}

/// Start a DnD operation on the given surface with some data
pub fn start_dnd() {}

/// End the current DnD operation, if there is one
pub fn end_dnd() {}


}
39 changes: 39 additions & 0 deletions src/mime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ impl Default for MimeType {
}
}

impl From<Cow<'static, str>> for MimeType {
fn from(value: Cow<'static, str>) -> Self {
if let Some(pos) = ALLOWED_TEXT_MIME_TYPES.into_iter().position(|allowed| allowed == value)
{
MimeType::Text(match pos {
0 => Text::TextPlainUtf8,
1 => Text::Utf8String,
2 => Text::TextPlain,
_ => unreachable!(),
})
} else {
MimeType::Other(value)
}
}
}

impl AsRef<str> for MimeType {
fn as_ref(&self) -> &str {
match self {
Expand Down Expand Up @@ -120,3 +136,26 @@ impl std::fmt::Display for MimeType {
pub fn normalize_to_lf(text: String) -> String {
text.replace("\r\n", "\n").replace('\r', "\n")
}

#[cfg(test)]
mod tests {
use std::borrow::Cow;

use crate::mime::{MimeType, ALLOWED_TEXT_MIME_TYPES};

#[test]
fn test_from_str() {
assert_eq!(
MimeType::from(Cow::Borrowed(ALLOWED_TEXT_MIME_TYPES[0])),
MimeType::Text(crate::mime::Text::TextPlainUtf8)
);
assert_eq!(
MimeType::from(Cow::Borrowed(ALLOWED_TEXT_MIME_TYPES[1])),
MimeType::Text(crate::mime::Text::Utf8String)
);
assert_eq!(
MimeType::from(Cow::Borrowed(ALLOWED_TEXT_MIME_TYPES[2])),
MimeType::Text(crate::mime::Text::TextPlain)
);
}
}

0 comments on commit 301d803

Please sign in to comment.