Skip to content

Commit

Permalink
feat: add __new__ for XID class (#9)
Browse files Browse the repository at this point in the history
Co-authored-by: A.Shpak <aleksander.shpak.als@gmail.com>
  • Loading branch information
shpaker and A.Shpak authored May 12, 2024
1 parent 7045141 commit 14addfd
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 48 deletions.
47 changes: 4 additions & 43 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This file is autogenerated by maturin v1.5.1
# To update, run
#
# maturin generate-ci github --pytest
# maturin generate-ci github
#
name: CI

Expand Down Expand Up @@ -45,37 +45,14 @@ jobs:
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.platform.target }}
args: --release --out dist --interpreter python3.10
args: --release --out dist --find-interpreter
sccache: 'true'
manylinux: auto
- name: Upload wheels
uses: actions/upload-artifact@v4
with:
name: wheels-linux-${{ matrix.platform.target }}
path: dist
- name: pytest
if: ${{ startsWith(matrix.platform.target, 'x86_64') }}
shell: bash
run: |
set -e
pip install epyxid --find-links dist --force-reinstall
pip install pytest
pytest
- name: pytest
if: ${{ !startsWith(matrix.platform.target, 'x86') && matrix.platform.target != 'ppc64' }}
uses: uraimo/run-on-arch-action@v2.5.0
with:
arch: ${{ matrix.platform.target }}
distro: ubuntu22.04
githubToken: ${{ github.token }}
install: |
apt-get update
apt-get install -y --no-install-recommends python3 python3-pip
pip3 install -U pip pytest
run: |
set -e
pip3 install epyxid --find-links dist --force-reinstall
pytest

windows:
runs-on: ${{ matrix.platform.runner }}
Expand All @@ -96,21 +73,13 @@ jobs:
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.platform.target }}
args: --release --out dist --interpreter python3.10
args: --release --out dist --find-interpreter
sccache: 'true'
- name: Upload wheels
uses: actions/upload-artifact@v4
with:
name: wheels-windows-${{ matrix.platform.target }}
path: dist
- name: pytest
if: ${{ !startsWith(matrix.platform.target, 'aarch64') }}
shell: bash
run: |
set -e
pip install epyxid --find-links dist --force-reinstall
pip install pytest
pytest

macos:
runs-on: ${{ matrix.platform.runner }}
Expand All @@ -130,21 +99,13 @@ jobs:
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.platform.target }}
args: --release --out dist --interpreter python3.10
args: --release --out dist --find-interpreter
sccache: 'true'
- name: Upload wheels
uses: actions/upload-artifact@v4
with:
name: wheels-macos-${{ matrix.platform.target }}
path: dist
- name: pytest
if: ${{ !startsWith(matrix.platform.target, 'aarch64') }}
shell: bash
run: |
set -e
pip install epyxid --find-links dist --force-reinstall
pip install pytest
pytest

sdist:
runs-on: ubuntu-latest
Expand Down
4 changes: 4 additions & 0 deletions epyxid.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime
from typing import Optional, Union

__version__: str

Expand All @@ -8,6 +9,9 @@ class XID:
Globally unique sortable id.
"""

def __new__(cls, value: Optional[Union[str, bytes]] = None):
pass

def as_bytes(self) -> bytes:
"""
The binary representation of the id.
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
extern crate core;

use pyo3::prelude::{pymodule, wrap_pyfunction, Bound, PyModule, PyResult, Python};

use crate::errors::XIDError;
Expand Down
27 changes: 25 additions & 2 deletions src/wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
use std::hash::{DefaultHasher, Hash, Hasher};

use pyo3::types::{PyBytes, PyDateTime};
use pyo3::{pyclass, pymethods, Bound, PyResult, Python};
use crate::utils::{xid_create, xid_from_bytes, xid_from_str};

use pyo3::types::{PyBytes, PyDateTime, PyString};
use pyo3::{pyclass, pymethods, Bound, FromPyObject, PyResult, Python};
use xid::Id;

#[derive(FromPyObject)]
enum XIDReprTypes<'a> {
#[pyo3(transparent, annotation = "str")]
String(&'a PyString),
#[pyo3(transparent, annotation = "bytes")]
Bytes(&'a PyBytes),
}

#[pyclass]
pub struct XID(pub Id);

#[pymethods]
impl XID {
#[new]
fn new<'p>(py: Python<'p>, data: Option<XIDReprTypes>) -> PyResult<XID> {
match data {
None => xid_create(),
Some(repr_value) => match repr_value {
XIDReprTypes::String(value) => xid_from_str(value.to_str().unwrap()),
XIDReprTypes::Bytes(value) => {
xid_from_bytes(PyBytes::new_bound(py, value.as_bytes()))
}
},
}
}

fn as_bytes<'p>(&self, _py: Python<'p>) -> Bound<'p, PyBytes> {
PyBytes::new_bound(_py, self.0.as_bytes())
}
Expand Down
31 changes: 28 additions & 3 deletions test_xid.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
from epyxid import xid_from_bytes, xid_from_str, xid_create
from pytest import raises
from typing import Union, Optional

from epyxid import xid_from_bytes, xid_from_str, xid_create, XID
from pytest import raises, param, mark

XID_STR = '9m4e2mr0ui3e8a215n4g'
XID_BYTES = bytes([0x4d, 0x88, 0xe1, 0x5b, 0x60, 0xf4, 0x86, 0xe4, 0x28, 0x41, 0x2d, 0xc9])


def test_create() -> None:
def test_create_func() -> None:
xid = xid_create()
assert xid is not None


def test_create_cls() -> None:
xid = XID()
assert xid is not None


@mark.parametrize(
('value',),
[
param(None),
param(XID_BYTES),
param(XID_STR),
],
)
def test_create_cls_param(value: Optional[Union[str, bytes]], ) -> None:
xid = XID(value)
assert xid is not None


def test_create_cls_error() -> None:
with raises(TypeError):
xid = XID(42)


def test_from_str_valid() -> None:
xid = xid_from_str(XID_STR)
assert bytes(xid) == XID_BYTES
Expand Down

0 comments on commit 14addfd

Please sign in to comment.