rsget_lib/
lib.rs

1#![allow(clippy::new_ret_no_self)]
2#![deny(rust_2018_idioms)]
3
4#[macro_use]
5extern crate log;
6#[macro_use]
7extern crate serde_derive;
8
9use crate::utils::error::RsgetError;
10use crate::utils::error::StreamError;
11
12use std::boxed::Box;
13use std::io::Write;
14
15use stream_lib::Stream;
16use stream_lib::StreamType;
17
18use reqwest::Client as ReqwestClient;
19
20pub trait Streamable {
21    /// Creates a new streamable
22    fn new(url: String) -> Result<Box<Self>, StreamError>
23    where
24        Self: Sized + Sync;
25    /// Returns the title of the stream if possible
26    fn get_title(&self) -> Option<String>;
27    /// Returns the author of the stream if possible
28    fn get_author(&self) -> Option<String>;
29    /// Returns if the stream is online
30    fn is_online(&self) -> bool;
31    /// Gets the url of the stream
32    fn get_stream(&self) -> Result<StreamType, StreamError>;
33    /// Returns what extension the stream should be
34    fn get_ext(&self) -> String;
35    /// Gets the default name of the stream
36    fn get_default_name(&self) -> String;
37    fn get_reqwest_client(&self) -> &ReqwestClient {
38        Box::leak(Box::new(ReqwestClient::new()))
39    }
40    /// Downloads the stream to a file
41    fn download(&self, writer: Box<dyn Write>) -> Result<u64, StreamError> {
42        if !self.is_online() {
43            Err(StreamError::Rsget(RsgetError::Offline))
44        } else {
45            let stream = Stream::new(self.get_stream()?);
46            Ok(stream.write_file(self.get_reqwest_client(), writer)?)
47        }
48    }
49}
50
51// impl<S> Streamable for Box<S>
52// where S: Streamable
53// { }
54
55pub mod plugins;
56pub mod utils;