Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to github actions #7

Merged
merged 9 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/workflows/audit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Audit
on:
push:
paths:
- '**/Cargo.toml'
- '**/Cargo.lock'
jobs:
security_audit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/audit-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
58 changes: 58 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

env:
CARGO_TERM_COLOR: always

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly
components: rustfmt, clippy
override: true
- uses: actions-rs/cargo@v1
with:
command: check
- name: Generate clippy annotations
uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --all-targets --all-features
- name: Fail on clippy warnings
uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --all-targets --all-features -- -D warnings
- uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python: [3.7, 3.8, 3.9]
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
- uses: messense/maturin-action@v1
with:
maturin-version: latest
- uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python }}
- run: pip install tox
- run: tox -e py
38 changes: 0 additions & 38 deletions .travis.yml

This file was deleted.

6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,14 @@ license = "MIT"
keywords = ["cbor", "serialization", "deserialization", "python"]
description = "A Python CBOR (de)serialization module, powered by Rust"

[badges]
travis-ci = { repository = "mjkoo/cbors", branch = "master" }
codecov = { repository = "mjkoo/cbors", branch = "master" }

[lib]
name = "cbors"
crate-type = ["cdylib", "rlib"]

[dependencies]
pyo3 = { version = "0.12", features = ["extension-module"] }
serde = { version = "1.0", features = ["derive"] }
serde_cbor = "0.9"
serde_cbor = "0.10"

[package.metadata.maturin]
classifier = [
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# cbo-rs
[![Build Status](https://travis-ci.org/mjkoo/cbors.svg?branch=master)](https://travis-ci.org/mjkoo/cbors)
[![codecov](https://codecov.io/gh/mjkoo/cbors/branch/master/graph/badge.svg)](https://codecov.io/gh/mjkoo/cbors)
![CI](https://github.com/mjkoo/cbors/actions/workflows/ci.yml/badge.svg?branch=master)
[![PyPI](https://img.shields.io/pypi/v/cbors.svg)](https://pypi.org/project/cbors/)

A Python CBOR (de)serialization module, powered by Rust.
Expand Down
2 changes: 0 additions & 2 deletions codecov.yml

This file was deleted.

50 changes: 0 additions & 50 deletions docker/Dockerfile.test

This file was deleted.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[build-system]
requires = ["maturin"]
requires = ["maturin>=0.11,<0.12"]
build-backend = "maturin"
71 changes: 15 additions & 56 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,24 @@ use pyo3::prelude::*;
use pyo3::types::{PyAny, PyByteArray, PyBytes, PyDict, PyList};
use pyo3::{wrap_pyfunction, AsPyPointer, FromPyObject, PyObject, ToPyObject};
use serde::{Deserialize, Serialize};
use serde_cbor::{ObjectKey, Value};

/// A simplified CBOR value containing only types useful for keys.
#[derive(Debug, Clone, Eq, Ord, PartialOrd, PartialEq, Deserialize, Serialize)]
struct CborObjectKey(ObjectKey);
use serde_cbor::value::Value;

/// An enum over all possible CBOR types.
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
struct CborValue(Value);

impl<'source> FromPyObject<'source> for CborObjectKey {
fn extract(ob: &'source PyAny) -> PyResult<Self> {
if ob.as_ptr() == unsafe { pyo3::ffi::Py_None() } {
Ok(CborObjectKey(ObjectKey::Null))
} else if let Ok(b) = ob.extract::<bool>() {
Ok(CborObjectKey(ObjectKey::Bool(b)))
} else if let Ok(i) = ob.extract::<i64>() {
Ok(CborObjectKey(ObjectKey::Integer(i)))
} else if let Ok(s) = ob.extract::<String>() {
Ok(CborObjectKey(ObjectKey::String(s)))
} else if let Ok(b) = ob.downcast::<PyByteArray>() {
Ok(CborObjectKey(ObjectKey::Bytes(b.to_vec())))
} else if let Ok(b) = ob.downcast::<PyBytes>() {
Ok(CborObjectKey(ObjectKey::Bytes(b.as_bytes().to_vec())))
} else {
Err(PyTypeError::new_err(format!(
"Value not convertable to cbor object key: {}",
ob.to_string()
)))
}
}
}

impl<'source> FromPyObject<'source> for CborValue {
fn extract(ob: &'source PyAny) -> PyResult<Self> {
if ob.as_ptr() == unsafe { pyo3::ffi::Py_None() } {
Ok(CborValue(Value::Null))
} else if let Ok(b) = ob.extract::<bool>() {
Ok(CborValue(Value::Bool(b)))
} else if let Ok(u) = ob.extract::<u64>() {
Ok(CborValue(Value::U64(u)))
} else if let Ok(i) = ob.extract::<i64>() {
Ok(CborValue(Value::I64(i)))
} else if let Ok(i) = ob.extract::<i128>() {
Ok(CborValue(Value::Integer(i)))
} else if let Ok(f) = ob.extract::<f64>() {
Ok(CborValue(Value::F64(f)))
Ok(CborValue(Value::Float(f)))
} else if let Ok(s) = ob.extract::<String>() {
Ok(CborValue(Value::String(s)))
Ok(CborValue(Value::Text(s)))
} else if let Ok(b) = ob.downcast::<PyByteArray>() {
Ok(CborValue(Value::Bytes(b.to_vec())))
} else if let Ok(b) = ob.downcast::<PyBytes>() {
Expand All @@ -66,10 +37,10 @@ impl<'source> FromPyObject<'source> for CborValue {
.collect::<PyResult<Vec<_>>>()?,
)))
} else if let Ok(d) = ob.downcast::<PyDict>() {
Ok(CborValue(Value::Object(
Ok(CborValue(Value::Map(
d.into_iter()
.map(|(k, v)| {
let ck: CborObjectKey = k.extract()?;
let ck: CborValue = k.extract()?;
let cv: CborValue = v.extract()?;
Ok((ck.0, cv.0))
})
Expand All @@ -84,38 +55,26 @@ impl<'source> FromPyObject<'source> for CborValue {
}
}

impl ToPyObject for CborObjectKey {
fn to_object(&self, py: Python) -> PyObject {
match &self.0 {
ObjectKey::Null => py.None(),
ObjectKey::Bool(b) => b.to_object(py),
ObjectKey::Integer(i) => i.to_object(py),
ObjectKey::String(s) => s.to_object(py),
ObjectKey::Bytes(v) => PyBytes::new(py, &v).into(),
}
}
}

impl ToPyObject for CborValue {
fn to_object(&self, py: Python) -> PyObject {
match &self.0 {
Value::Null => py.None(),
Value::Bool(b) => b.to_object(py),
Value::U64(u) => u.to_object(py),
Value::I64(i) => i.to_object(py),
Value::F64(f) => f.to_object(py),
Value::String(s) => s.to_object(py),
Value::Bytes(v) => PyBytes::new(py, &v).into(),
Value::Integer(i) => i.to_object(py),
Value::Float(f) => f.to_object(py),
Value::Text(s) => s.to_object(py),
Value::Bytes(v) => PyBytes::new(py, v).into(),
Value::Array(a) => a
.iter()
.map(|x| CborValue(x.clone()))
.collect::<Vec<_>>()
.to_object(py),
Value::Object(d) => d
Value::Map(d) => d
.iter()
.map(|(k, v)| (CborObjectKey(k.clone()), CborValue(v.clone())))
.map(|(k, v)| (CborValue(k.clone()), CborValue(v.clone())))
.collect::<BTreeMap<_, _>>()
.to_object(py),
_ => py.None(),
}
}
}
Expand Down
14 changes: 3 additions & 11 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
[tox]
envlist = py{35,36,37,38}-{cov,nocov}
envlist = py{37,38,39}
skipsdist = true

[testenv]
whitelist_externals =
maturin
rustup
cov: bash
cov: kcov
deps =
pytest
hypothesis
passenv = TOXENV CI TRAVIS TRAVIS_* CODECOV_*
passenv= CARGO_INCREMENTAL RUSTFLAGS RUSTDOCFLAGS
commands_pre =
rustup override set nightly
maturin develop
commands_post =
rustup override unset
commands =
nocov: pytest -v
cov: kcov --include-pattern=.rs --include-path={toxinidir} {envdir}/cov {envpython} -m pytest -v
cov: bash -c "bash <(curl -s https://codecov.io/bash) -s {envdir}/cov"
pytest -v