commit
875fda42ff
@ -0,0 +1,42 @@
|
||||
|
||||
name: Build and Test
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main, develop ]
|
||||
pull_request:
|
||||
branches: [ main, develop ]
|
||||
|
||||
env:
|
||||
CARGO_TERM_COLOR: always
|
||||
|
||||
jobs:
|
||||
build-and-test:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
- name: rustup
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
toolchain: stable
|
||||
- name: Cache build data
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
path: |
|
||||
target
|
||||
~/.cargo/
|
||||
key: ${{ runner.os }}-cargo-${{ hashFiles('Cargo.lock') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-cargo-
|
||||
|
||||
- name: Fast Fail Check
|
||||
run: cargo check --verbose
|
||||
|
||||
- name: Build
|
||||
run: cargo build --verbose --all-features
|
||||
|
||||
- name: Run tests
|
||||
run: cargo test --verbose --all-features
|
@ -1,18 +1,64 @@
|
||||
use thiserror::Error;
|
||||
use std::{
|
||||
error::Error as StdError,
|
||||
fmt::{Display, Formatter, Result as FmtResult},
|
||||
};
|
||||
|
||||
pub type YoutubeResult<T> = Result<T, YoutubeError>;
|
||||
use reqwest::Error as ReqwestError;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum YoutubeError {
|
||||
#[error(transparent)]
|
||||
Reqwest(#[from] reqwest::Error),
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[error("Parse Error: {0}")]
|
||||
ParseError(String),
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
Reqwest(ReqwestError),
|
||||
Parse(Parsing),
|
||||
}
|
||||
|
||||
impl From<&str> for YoutubeError {
|
||||
fn from(s: &str) -> Self {
|
||||
Self::ParseError(s.to_string())
|
||||
impl Display for Error {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
|
||||
use Error::*;
|
||||
match self {
|
||||
Reqwest(e) => e.fmt(f),
|
||||
Parse(_) => write!(f, "parse error"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl StdError for Error {
|
||||
fn source(&self) -> Option<&(dyn StdError + 'static)> {
|
||||
use Error::*;
|
||||
match self {
|
||||
Reqwest(e) => e.source(),
|
||||
Parse(e) => Some(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Parsing> for Error {
|
||||
fn from(s: Parsing) -> Self {
|
||||
Self::Parse(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ReqwestError> for Error {
|
||||
fn from(e: ReqwestError) -> Self {
|
||||
Self::Reqwest(e)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Parsing {
|
||||
MissingElement(String),
|
||||
MissingAttribute(String),
|
||||
}
|
||||
|
||||
impl Display for Parsing {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
|
||||
use Parsing::*;
|
||||
match self {
|
||||
MissingAttribute(s) => write!(f, "missing attribute: {}", s),
|
||||
MissingElement(s) => write!(f, "missing element: {}", s),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl StdError for Parsing {}
|
||||
|
@ -0,0 +1 @@
|
||||
mod endpoints;
|
@ -0,0 +1,10 @@
|
||||
use crate::get_video_information;
|
||||
|
||||
#[tokio::test]
|
||||
async fn invalid_url_is_err() {
|
||||
assert!(
|
||||
get_video_information("https://www.youtube.com/watch?v=FFFFFFFFFFF")
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
use crate::endpoints::get_video_information;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_video_information() {
|
||||
let information = get_video_information("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(information.id, "dQw4w9WgXcQ".to_string());
|
||||
assert_eq!(
|
||||
information.url,
|
||||
"https://www.youtube.com/watch?v=dQw4w9WgXcQ".to_string()
|
||||
);
|
||||
assert_eq!(information.uploader, "RickAstleyVEVO".to_string());
|
||||
assert_eq!(
|
||||
information.title,
|
||||
"Rick Astley - Never Gonna Give You Up (Video)".to_string()
|
||||
);
|
||||
assert_eq!(
|
||||
information.thumbnail,
|
||||
Some("https://i.ytimg.com/vi/dQw4w9WgXcQ/maxresdefault.jpg".to_string())
|
||||
);
|
||||
|
||||
assert!(
|
||||
get_video_information("https://www.youtube.com/watch?v=FFFFFFFFFFF")
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
}
|
@ -1,2 +0,0 @@
|
||||
#[cfg(test)]
|
||||
mod endpoints_test;
|
Loading…
Reference in New Issue