Skip to content

Commit

Permalink
more tests #25 + better docs #31
Browse files Browse the repository at this point in the history
  • Loading branch information
Luro02 committed Oct 3, 2019
1 parent b197d5f commit 6b717f9
Show file tree
Hide file tree
Showing 41 changed files with 2,224 additions and 645 deletions.
59 changes: 3 additions & 56 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,81 +4,28 @@ name: Rust
on: [push, pull_request]

jobs:
build:
name: Build
runs-on: ${{ matrix.os }}
strategy:
matrix:
rust:
- stable
os:
- ubuntu-latest
# execute cargo build
steps:
- uses: actions/checkout@v1
- uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust }}
override: true
- uses: actions-rs/cargo@v1
with:
command: build
arguments: --all-features

test:
name: Test
runs-on: ${{ matrix.os }}
strategy:
matrix:
rust:
- stable
os:
- ubuntu-latest
# execute cargo test
steps:
- uses: actions/checkout@v1
- uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust }}
override: true
- uses: actions-rs/cargo@v1
with:
command: test

rustfmt:
name: Rustfmt
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- stable
steps:
- uses: actions/checkout@v1
- uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust }}
override: true
toolchain: stable
- run: rustup component add rustfmt
- uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check

clippy:
name: Clippy
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- stable
steps:
- uses: actions/checkout@v1
- uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust }}
override: true
toolchain: stable
- run: rustup component add clippy
- uses: actions-rs/cargo@v1
with:
command: clippy
args: -- -D warnings
# args: -- -D warnings
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/target/
**/*.rs.bk
Cargo.lock
tarpaulin-report.html
50 changes: 22 additions & 28 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
language: rust
sudo: required

cache: cargo

before_cache: |
cargo install cargo-tarpaulin
cargo install cargo-update
cargo install-update --all
# before_cache:
# - rm -rf /home/travis/.cargo/registry


rust:
- stable
- beta
Expand All @@ -8,32 +20,14 @@ matrix:
allow_failures:
- rust: nightly

env:
global:
- RUSTFLAGS="-C link-dead-code"

addons:
apt:
packages:
- libcurl4-openssl-dev
- libelf-dev
- libdw-dev
- cmake
- gcc
- binutils-dev
- libiberty-dev
script:
- cargo clean
- cargo build
- cargo test

after_success: |
wget https://github.com/SimonKagstrom/kcov/archive/master.tar.gz &&
tar xzf master.tar.gz &&
cd kcov-master &&
mkdir build &&
cd build &&
cmake .. &&
make &&
make install DESTDIR=../../kcov-build &&
cd ../.. &&
rm -rf kcov-master &&
for file in target/debug/hls_m3u8-*[^\.d]; do mkdir -p "target/cov/$(basename $file)"; ./kcov-build/usr/local/bin/kcov --exclude-pattern=/.cargo,/usr/lib --verify "target/cov/$(basename $file)" "$file"; done &&
bash <(curl -s https://codecov.io/bash) &&
echo "Uploaded code coverage"
# this does require a -Z flag for Doctests, which is unstable!
if [[ "$TRAVIS_RUST_VERSION" == nightly ]]; then
cargo tarpaulin --run-types Tests Doctests --out Xml
bash <(curl -s https://codecov.io/bash)
fi
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ edition = "2018"
categories = ["parser"]

[badges]
travis-ci = {repository = "sile/hls_m3u8"}
codecov = {repository = "sile/hls_m3u8"}
travis-ci = { repository = "sile/hls_m3u8" }
codecov = { repository = "sile/hls_m3u8" }

[dependencies]
failure = "0.1.5"
derive_builder = "0.7.2"
chrono = "0.4.9"
strum = { version = "0.16.0", features = ["derive"] }
derive_more = "0.15.0"

[dev-dependencies]
clap = "2"
28 changes: 25 additions & 3 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,14 @@ impl FromStr for AttributePairs {
let pair = split(line.trim(), '=');

if pair.len() < 2 {
return Err(Error::invalid_input());
continue;
}

let key = pair[0].to_uppercase();
let value = pair[1].to_string();
let key = pair[0].trim().to_uppercase();
let value = pair[1].trim().to_string();
if value.is_empty() {
continue;
}

result.insert(key.trim().to_string(), value.trim().to_string());
}
Expand Down Expand Up @@ -122,6 +125,11 @@ mod test {

let mut iterator = pairs.iter();
assert!(iterator.any(|(k, v)| k == "ABC" && v == "12.3"));

let mut pairs = AttributePairs::new();
pairs.insert("FOO".to_string(), "BAR".to_string());

assert_eq!("FOO=BAR,VAL".parse::<AttributePairs>().unwrap(), pairs);
}

#[test]
Expand All @@ -136,4 +144,18 @@ mod test {
let mut iterator = attrs.iter();
assert!(iterator.any(|(k, v)| k == "key_02" && v == "value_02"));
}

#[test]
fn test_into_iter() {
let mut map = HashMap::new();
map.insert("k".to_string(), "v".to_string());

let mut attrs = AttributePairs::new();
attrs.insert("k".to_string(), "v".to_string());

assert_eq!(
attrs.into_iter().collect::<Vec<_>>(),
map.into_iter().collect::<Vec<_>>()
);
}
}
14 changes: 6 additions & 8 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::error;
use std::fmt;

use failure::{Backtrace, Context, Fail};
Expand Down Expand Up @@ -118,13 +117,6 @@ impl From<Context<ErrorKind>> for Error {
}

impl Error {
pub(crate) fn unknown<T>(value: T) -> Self
where
T: error::Error,
{
Self::from(ErrorKind::UnknownError(value.to_string()))
}

pub(crate) fn missing_value<T: ToString>(value: T) -> Self {
Self::from(ErrorKind::MissingValue(value.to_string()))
}
Expand Down Expand Up @@ -218,3 +210,9 @@ impl From<::chrono::ParseError> for Error {
Error::chrono(value)
}
}

impl From<::strum::ParseError> for Error {
fn from(value: ::strum::ParseError) -> Self {
Error::custom(value) // TODO!
}
}
10 changes: 9 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
#![forbid(unsafe_code)]
#![warn(
//clippy::pedantic,
clippy::nursery,
clippy::cargo
)]
#![warn(missing_docs)]
#![allow(clippy::multiple_crate_versions)]
#![warn(
missing_docs,
missing_copy_implementations,
missing_debug_implementations,
trivial_casts, // TODO (needed?)
trivial_numeric_casts
)]
//! [HLS] m3u8 parser/generator.
//!
//! [HLS]: https://tools.ietf.org/html/rfc8216
Expand Down
5 changes: 2 additions & 3 deletions src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ impl FromStr for Lines {
for l in input.lines() {
let line = l.trim();

// ignore empty lines
if line.len() == 0 {
if line.is_empty() {
continue;
}

Expand All @@ -39,7 +38,7 @@ impl FromStr for Lines {
continue;
} else if line.starts_with("#EXT") {
Line::Tag(line.parse()?)
} else if line.starts_with("#") {
} else if line.starts_with('#') {
continue; // ignore comments
} else {
// stream inf line needs special treatment
Expand Down
19 changes: 9 additions & 10 deletions src/master_playlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl MasterPlaylistBuilder {
let required_version = self.required_version();
let specified_version = self
.version_tag
.unwrap_or(required_version.into())
.unwrap_or_else(|| required_version.into())
.version();

if required_version > specified_version {
Expand Down Expand Up @@ -164,7 +164,7 @@ impl MasterPlaylistBuilder {
.flatten(),
)
.max()
.unwrap_or(ProtocolVersion::latest())
.unwrap_or_else(ProtocolVersion::latest)
}

fn validate_stream_inf_tags(&self) -> crate::Result<()> {
Expand All @@ -188,24 +188,23 @@ impl MasterPlaylistBuilder {
}
}
match t.closed_captions() {
Some(&ClosedCaptions::GroupId(ref group_id)) => {
&Some(ClosedCaptions::GroupId(ref group_id)) => {
if !self.check_media_group(MediaType::ClosedCaptions, group_id) {
return Err(Error::unmatched_group(group_id));
}
}
Some(&ClosedCaptions::None) => {
&Some(ClosedCaptions::None) => {
has_none_closed_captions = true;
}
None => {}
}
}
if has_none_closed_captions {
if !value
if has_none_closed_captions
&& !value
.iter()
.all(|t| t.closed_captions() == Some(&ClosedCaptions::None))
{
return Err(Error::invalid_input());
}
.all(|t| t.closed_captions() == &Some(ClosedCaptions::None))
{
return Err(Error::invalid_input());
}
}
Ok(())
Expand Down
8 changes: 4 additions & 4 deletions src/media_playlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl MediaPlaylistBuilder {
let required_version = self.required_version();
let specified_version = self
.version_tag
.unwrap_or(required_version.into())
.unwrap_or_else(|| required_version.into())
.version();

if required_version > specified_version {
Expand Down Expand Up @@ -109,7 +109,7 @@ impl MediaPlaylistBuilder {
}
};

if !(rounded_segment_duration <= max_segment_duration) {
if rounded_segment_duration > max_segment_duration {
return Err(Error::custom(format!(
"Too large segment duration: actual={:?}, max={:?}, target_duration={:?}, uri={:?}",
segment_duration,
Expand All @@ -122,7 +122,7 @@ impl MediaPlaylistBuilder {
// CHECK: `#EXT-X-BYTE-RANGE`
if let Some(tag) = s.byte_range_tag() {
if tag.to_range().start().is_none() {
let last_uri = last_range_uri.ok_or(Error::invalid_input())?;
let last_uri = last_range_uri.ok_or_else(Error::invalid_input)?;
if last_uri != s.uri() {
return Err(Error::invalid_input());
}
Expand Down Expand Up @@ -200,7 +200,7 @@ impl MediaPlaylistBuilder {
.unwrap_or(ProtocolVersion::V1)
}))
.max()
.unwrap_or(ProtocolVersion::latest())
.unwrap_or_else(ProtocolVersion::latest)
}

/// Adds a media segment to the resulting playlist.
Expand Down
2 changes: 1 addition & 1 deletion src/tags/basic/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl ExtXVersion {
/// ProtocolVersion::V6
/// );
/// ```
pub const fn version(&self) -> ProtocolVersion {
pub const fn version(self) -> ProtocolVersion {
self.0
}
}
Expand Down
Loading

0 comments on commit 6b717f9

Please sign in to comment.