Use a structured magnet URL type
This will be required to parse them and submit them to bitmagnet.
This commit is contained in:
parent
90c3fc5ee3
commit
70684eb2a2
5 changed files with 70 additions and 32 deletions
|
|
@ -1,7 +1,8 @@
|
|||
pub type Magnet = String;
|
||||
use color_eyre::eyre::{eyre, Result};
|
||||
use magnet_url::Magnet;
|
||||
|
||||
/// Extract magnet links from text
|
||||
pub fn extract_magnet_links(text: &str) -> Vec<Magnet> {
|
||||
pub fn extract_magnet_links(text: &str) -> Result<Vec<Magnet>> {
|
||||
let mut links = Vec::new();
|
||||
|
||||
let mut start_idx = 0;
|
||||
|
|
@ -13,11 +14,14 @@ pub fn extract_magnet_links(text: &str) -> Vec<Magnet> {
|
|||
.map(|e| start + e)
|
||||
.unwrap_or(text.len());
|
||||
|
||||
links.push(text[start..end].to_string());
|
||||
let link = &text[start..end];
|
||||
let magnet = Magnet::new(link)
|
||||
.map_err(|e| eyre!("Failed to parse magnet link '{}': {:?}", link, e))?;
|
||||
links.push(magnet);
|
||||
start_idx = end;
|
||||
}
|
||||
|
||||
links
|
||||
Ok(links)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
@ -30,7 +34,7 @@ mod tests {
|
|||
|
||||
let links = extract_magnet_links(text);
|
||||
|
||||
assert!(links.is_empty());
|
||||
assert!(links.unwrap().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -39,67 +43,85 @@ mod tests {
|
|||
|
||||
let links = extract_magnet_links(text);
|
||||
|
||||
assert!(links.is_empty());
|
||||
assert!(links.unwrap().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_single_magnet_link() {
|
||||
let text = "Here is a magnet link: magnet:?xt=urn:btih:example";
|
||||
|
||||
let links = extract_magnet_links(text);
|
||||
let links = extract_magnet_links(text).unwrap();
|
||||
|
||||
assert_eq!(links.len(), 1);
|
||||
assert_eq!(links[0], "magnet:?xt=urn:btih:example");
|
||||
assert_eq!(
|
||||
links[0],
|
||||
Magnet::new_no_validation("magnet:?xt=urn:btih:example")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_multiple_magnet_links() {
|
||||
let text = "First link: magnet:?xt=urn:btih:example1 and second link: magnet:?xt=urn:btih:example2";
|
||||
|
||||
let links = extract_magnet_links(text);
|
||||
let links = extract_magnet_links(text).unwrap();
|
||||
|
||||
assert_eq!(links.len(), 2);
|
||||
assert_eq!(links[0], "magnet:?xt=urn:btih:example1");
|
||||
assert_eq!(links[1], "magnet:?xt=urn:btih:example2");
|
||||
assert_eq!(
|
||||
links[0],
|
||||
Magnet::new_no_validation("magnet:?xt=urn:btih:example1")
|
||||
);
|
||||
assert_eq!(
|
||||
links[1],
|
||||
Magnet::new_no_validation("magnet:?xt=urn:btih:example2")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_magnet_link_at_beginning() {
|
||||
let text = "magnet:?xt=urn:btih:example at the beginning";
|
||||
|
||||
let links = extract_magnet_links(text);
|
||||
let links = extract_magnet_links(text).unwrap();
|
||||
|
||||
assert_eq!(links.len(), 1);
|
||||
assert_eq!(links[0], "magnet:?xt=urn:btih:example");
|
||||
assert_eq!(
|
||||
links[0],
|
||||
Magnet::new_no_validation("magnet:?xt=urn:btih:example")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_magnet_link_at_end() {
|
||||
let text = "Link at the end: magnet:?xt=urn:btih:example";
|
||||
|
||||
let links = extract_magnet_links(text);
|
||||
let links = extract_magnet_links(text).unwrap();
|
||||
|
||||
assert_eq!(links.len(), 1);
|
||||
assert_eq!(links[0], "magnet:?xt=urn:btih:example");
|
||||
assert_eq!(
|
||||
links[0],
|
||||
Magnet::new_no_validation("magnet:?xt=urn:btih:example")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_magnet_link_without_whitespace() {
|
||||
let text = "Text containing a link:magnet:?xt=urn:btih:example";
|
||||
|
||||
let links = extract_magnet_links(text);
|
||||
let links = extract_magnet_links(text).unwrap();
|
||||
|
||||
assert_eq!(links.len(), 1);
|
||||
assert_eq!(links[0], "magnet:?xt=urn:btih:example");
|
||||
assert_eq!(
|
||||
links[0],
|
||||
Magnet::new_no_validation("magnet:?xt=urn:btih:example")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_complex_magnet_link() {
|
||||
let text = "Complex link: magnet:?xt=urn:btih:a1b2c3d4e5f6g7h8i9j0&dn=example+file&tr=udp%3A%2F%2Ftracker.example.com%3A80";
|
||||
|
||||
let links = extract_magnet_links(text);
|
||||
let links = extract_magnet_links(text).unwrap();
|
||||
|
||||
assert_eq!(links.len(), 1);
|
||||
assert_eq!(links[0], "magnet:?xt=urn:btih:a1b2c3d4e5f6g7h8i9j0&dn=example+file&tr=udp%3A%2F%2Ftracker.example.com%3A80");
|
||||
assert_eq!(links[0], Magnet::new_no_validation("magnet:?xt=urn:btih:a1b2c3d4e5f6g7h8i9j0&dn=example+file&tr=udp%3A%2F%2Ftracker.example.com%3A80"));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue