diff --git a/.github/workflows/master.yml b/.github/workflows/master.yml new file mode 100644 index 0000000..bd784e4 --- /dev/null +++ b/.github/workflows/master.yml @@ -0,0 +1,54 @@ +name: Master + +on: + push: + branches: + - master + +jobs: + build: + name: Run tests on ${{ matrix.target }} ${{ matrix.toolchain }} + runs-on: ubuntu-latest + strategy: + matrix: + toolchain: + - stable + - nightly + target: + - x86_64-unknown-linux-gnu + - x86_64-unknown-linux-musl + steps: + - uses: actions/checkout@v2 + - run: apt-get update + if: ${{ matrix.toolchain == "x86_64-unknown-linux-musl" }} + - run: apt-get install -y musl + if: ${{ matrix.toolchain == "x86_64-unknown-linux-musl" }} + - name: Cache cargo registry + uses: actions/cache@v1 + with: + path: ~/.cargo/registry + key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }} + - name: Cache cargo index + uses: actions/cache@v1 + with: + path: ~/.cargo/git + key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }} + - name: Cache cargo build + uses: actions/cache@v1 + with: + path: target + key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }} + - uses: actions-rs/toolchain@v1 + with: + toolchain: ${{ matrix.toolchain }} + target: ${{ matrix.target }} + - uses: actions-rs/cargo@v1 + with: + command: build + toolchain: ${{ matrix.toolchain }} + target: ${{ matrix.target }} + - uses: actions-rs/cargo@v1 + with: + command: test + toolchain: ${{ matrix.toolchain }} + target: ${{ matrix.target }} diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml new file mode 100644 index 0000000..9989647 --- /dev/null +++ b/.github/workflows/pr.yml @@ -0,0 +1,58 @@ +name: Pull Request + +on: + pull_request: + branches: + - master + +jobs: + build: + name: Run tests + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Cache cargo registry + uses: actions/cache@v1 + with: + path: ~/.cargo/registry + key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }} + - name: Cache cargo index + uses: actions/cache@v1 + with: + path: ~/.cargo/git + key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }} + - name: Cache cargo build + uses: actions/cache@v1 + with: + path: target + key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }} + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + - uses: actions-rs/cargo@v1 + with: + command: build + toolchain: stable + target: x86_64-unknown-linux-gnu + - uses: actions-rs/cargo@v1 + with: + command: test + toolchain: stable + target: x86_64-unknown-linux-gnu + + fmt: + name: Format check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + components: rustfmt + - uses: actions-rs/cargo@v1 + with: + command: fmt + args: --all -- --check diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml deleted file mode 100644 index 2524ddf..0000000 --- a/.github/workflows/rust.yml +++ /dev/null @@ -1,25 +0,0 @@ -name: Rust - -on: - push: - branches: - - master - pull_request: - branches: - - master - -jobs: - build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: actions-rs/cargo@v1 - with: - command: build - toolchain: stable - target: x86_64-unknown-linux-gnu - - uses: actions-rs/cargo@v1 - with: - command: test - toolchain: stable - target: x86_64-unknown-linux-gnu diff --git a/src/event/attributes.rs b/src/event/attributes.rs index 5116f12..9082091 100644 --- a/src/event/attributes.rs +++ b/src/event/attributes.rs @@ -44,10 +44,7 @@ pub trait AttributesWriter { } pub(crate) trait DataAttributesWriter { - fn set_datacontenttype( - &mut self, - datacontenttype: Option>, - ); + fn set_datacontenttype(&mut self, datacontenttype: Option>); fn set_dataschema(&mut self, dataschema: Option>); } @@ -169,10 +166,7 @@ impl AttributesWriter for Attributes { } impl DataAttributesWriter for Attributes { - fn set_datacontenttype( - &mut self, - datacontenttype: Option>, - ) { + fn set_datacontenttype(&mut self, datacontenttype: Option>) { match self { Attributes::V10(a) => a.set_datacontenttype(datacontenttype), } diff --git a/src/event/data.rs b/src/event/data.rs index 274f2cc..f8a2be9 100644 --- a/src/event/data.rs +++ b/src/event/data.rs @@ -24,8 +24,8 @@ impl Data { /// [`AsRef<[u8]>`]: https://doc.rust-lang.org/std/convert/trait.AsRef.html /// [`Data`]: enum.Data.html pub fn from_base64(i: I) -> Result - where - I: AsRef<[u8]>, + where + I: AsRef<[u8]>, { Ok(base64::decode(&i)?.into()) } @@ -68,7 +68,7 @@ impl TryFrom for String { match value { Data::String(s) => Ok(s), Data::Binary(v) => Ok(String::from_utf8(v)?), - Data::Json(s) => Ok(s.to_string()) + Data::Json(s) => Ok(s.to_string()), } } } diff --git a/src/event/event.rs b/src/event/event.rs index 2386b9a..b2e873e 100644 --- a/src/event/event.rs +++ b/src/event/event.rs @@ -1,8 +1,11 @@ -use super::{Attributes, AttributesReader, AttributesWriter, Data, ExtensionValue, SpecVersion, AttributesV10}; +use super::{ + Attributes, AttributesReader, AttributesV10, AttributesWriter, Data, ExtensionValue, + SpecVersion, +}; +use crate::event::attributes::DataAttributesWriter; use chrono::{DateTime, FixedOffset}; use delegate::delegate; -use std::convert::{TryFrom}; -use crate::event::attributes::DataAttributesWriter; +use std::convert::TryFrom; /// Data structure that represents a [CloudEvent](https://github.com/cloudevents/spec/blob/master/spec.md). /// It provides methods to get the attributes through [`AttributesReader`] @@ -74,7 +77,7 @@ impl Default for Event { fn default() -> Self { Event { attributes: Attributes::V10(AttributesV10::default()), - data: None + data: None, } } } @@ -94,15 +97,18 @@ impl Event { /// let mut e = Event::default(); /// e.write_data("application/json", None, json!({})) /// ``` - pub fn write_data, D: Into>(&mut self, content_type: S, schema: Option, value: D) { + pub fn write_data, D: Into>( + &mut self, + content_type: S, + schema: Option, + value: D, + ) { self.attributes.set_datacontenttype(Some(content_type)); self.attributes.set_dataschema(schema); self.data = Some(value.into()); } - pub fn get_data>( - &self, - ) -> Option { + pub fn get_data>(&self) -> Option { match self.data.as_ref() { Some(d) => Some(T::from(d.clone())), None => None, @@ -128,8 +134,6 @@ impl Event { } } - - #[cfg(test)] mod tests { use super::*; @@ -140,17 +144,11 @@ mod tests { "hello": "world" }); - let mut e = Event::default(); - e.write_data( - "application/json", - None, - expected_data.clone() - ); - + let mut e = Event::default(); + e.write_data("application/json", None, expected_data.clone()); let data: serde_json::Value = e.try_get_data().unwrap().unwrap(); assert_eq!(expected_data, data); assert_eq!("application/json", e.get_datacontenttype().unwrap()) } - } diff --git a/src/event/v10/attributes.rs b/src/event/v10/attributes.rs index bbc7c3e..82ab779 100644 --- a/src/event/v10/attributes.rs +++ b/src/event/v10/attributes.rs @@ -1,9 +1,9 @@ +use crate::event::attributes::DataAttributesWriter; use crate::event::{AttributesReader, AttributesWriter, ExtensionValue, SpecVersion}; use chrono::{DateTime, FixedOffset}; -use std::collections::HashMap; -use crate::event::attributes::DataAttributesWriter; -use uuid::Uuid; use hostname::get_hostname; +use std::collections::HashMap; +use uuid::Uuid; pub struct Attributes { id: String, @@ -109,10 +109,7 @@ impl AttributesWriter for Attributes { } impl DataAttributesWriter for Attributes { - fn set_datacontenttype( - &mut self, - datacontenttype: Option>, - ) { + fn set_datacontenttype(&mut self, datacontenttype: Option>) { self.datacontenttype = datacontenttype.map(Into::into) } @@ -131,7 +128,7 @@ impl Default for Attributes { dataschema: None, subject: None, time: None, - extensions: HashMap::new() + extensions: HashMap::new(), } } }