Skip to content
This repository was archived by the owner on May 7, 2024. It is now read-only.

Commit e4881d9

Browse files
committed
test release
1 parent cb66533 commit e4881d9

File tree

9 files changed

+177
-24
lines changed

9 files changed

+177
-24
lines changed

.semaphore/semaphore.yml

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Use the latest stable version of Semaphore 2.0 YML syntax:
2+
version: v1.0
3+
4+
# Name of your pipeline. In this example we connect two pipelines with
5+
# a promotion, so it helps to differentiate what's the job of each.
6+
name: Serde Rustler Semaphore pipeline
7+
8+
# An agent defines the environment in which your code runs.
9+
# It is a combination of one of available machine types and operating
10+
# system images. See:
11+
# https://docs.semaphoreci.com/article/20-machine-types
12+
# https://docs.semaphoreci.com/article/32-ubuntu-1804-image
13+
agent:
14+
machine:
15+
type: e1-standard-2
16+
os_image: ubuntu1804
17+
18+
# Blocks make up the structure of a pipeline and are executed sequentially.
19+
# Each block has a task that defines one or many parallel jobs. Jobs define
20+
# the commands to execute.
21+
# See https://docs.semaphoreci.com/article/62-concepts
22+
blocks:
23+
- name: Set up
24+
task:
25+
jobs:
26+
- name: compile and build plts
27+
commands:
28+
# Checkout code from Git repository. This step is mandatory if the
29+
# job is to work with your code.
30+
- checkout
31+
32+
- bin/setup_ci_elixir
33+
- sem-version elixir 1.8.1
34+
35+
# Restore dependencies from cache, command won't fail if it's
36+
# missing. More on caching:
37+
# - https://docs.semaphoreci.com/article/54-toolbox-reference#cache
38+
# - https://docs.semaphoreci.com/article/87-language-elixir
39+
- cache restore mix-deps-$SEMAPHORE_GIT_BRANCH-$(checksum mix.lock),mix-deps-$SEMAPHORE_GIT_BRANCH,mix-deps-master
40+
- cache restore mix-build-$SEMAPHORE_GIT_BRANCH-$(checksum mix.lock),mix-build-$SEMAPHORE_GIT_BRANCH,mix-build-master
41+
42+
- mix deps.get
43+
- mix compile
44+
- cd serde_rustler_tests/
45+
- MIX_ENV=test mix compile
46+
47+
# Store deps after compilation, otherwise rebar3 deps (that is, most
48+
# Erlang deps) won't be cached:
49+
- cache store mix-deps-$SEMAPHORE_GIT_BRANCH-$(checksum mix.lock) deps
50+
- cache store mix-build-$SEMAPHORE_GIT_BRANCH-$(checksum mix.lock) _build
51+
52+
- name: Analyze code
53+
task:
54+
# Commands in prologue run at the beginning of each parallel job.
55+
# https://docs.semaphoreci.com/article/50-pipeline-yaml
56+
prologue:
57+
commands:
58+
- checkout
59+
- bin/setup_ci_elixir
60+
- sem-version elixir 1.8.1
61+
- cache restore mix-deps-$SEMAPHORE_GIT_BRANCH-$(checksum mix.lock)
62+
- cache restore mix-build-$SEMAPHORE_GIT_BRANCH-$(checksum mix.lock)
63+
# This block contains 3 parallel jobs:
64+
jobs:
65+
- name: credo
66+
commands:
67+
- mix credo -a
68+
69+
- name: formatter
70+
commands:
71+
- mix format --check-formatted
72+
73+
- name: Run tests
74+
task:
75+
prologue:
76+
commands:
77+
- checkout
78+
- bin/setup_ci_elixir
79+
- sem-version elixir 1.8.1
80+
- cache restore mix-deps-$SEMAPHORE_GIT_BRANCH-$(checksum mix.lock)
81+
- cache restore mix-build-$SEMAPHORE_GIT_BRANCH-$(checksum mix.lock)
82+
83+
jobs:
84+
- name: ex_unit
85+
# Define an environment variable
86+
# See https://docs.semaphoreci.com/article/66-environment-variables-and-secrets
87+
# env_vars:
88+
commands:
89+
- mix test
90+

README.md

Whitespace-only changes.

mix.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ defmodule SerdeRustler.Mixfile do
44
use Mix.Project
55

66
@name :serde_rustler
7-
@version "0.0.1-dev"
7+
@version "0.0.1"
88
@description """
99
"""
10-
@github "https://github.com/datalove-app/serde_rustler"
10+
@github "https://github.com/sunny-g/serde_rustler"
1111
@files ["mix.exs", "mix.lock", "lib", "test", "README.md"]
1212
@maintainers ["Sunny G"]
1313
@licenses ["MIT"]

serde_rustler/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
[package]
22
name = "serde_rustler"
3+
description = "Serializer and Deserializer traits for Rustler NIFs"
34
version = "0.0.1"
45
authors = ["Sunny G <[email protected]>"]
6+
homepage = "https://github.com/sunny-g/serde_rustler"
7+
repository = "https://github.com/sunny-g/serde_rustler"
8+
categories = ["serde", "rustler", "elixir", "nif"]
59
readme = "README.md"
6-
categories = ["serde", "elixir"]
710
license = "MIT"
811
edition = "2018"
912

serde_rustler/README.md

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,71 @@
1-
# Nif for Elixir.Serde
1+
# serde_rustler
22

3-
## To build the NIF module:
3+
[Serde](https://serde.rs) [Serializer]() and [Deserializer]() traits for [Rustler](https://github.com/rusterlium/rustler) NIFs.
44

5-
- Make sure your projects `mix.exs` has the `:rustler` compiler listed in the `project` function: `compilers: [:rustler] ++ Mix.compilers()` If there already is a `:compilers` list, you should append `:rustler` to it.
6-
- Add your crate to the `rustler_crates` attribute in the `project function. [See here](https://hexdocs.pm/rustler/basics.html#crate-configuration).
7-
- Your NIF will now build along with your project.
5+
[Source](https://github.com/sunny-g/serde_rustler)
6+
<!-- [![Build Status](https://semaphoreci.com/api/v1/sunny-g/xdr/branches/master/badge.svg)](https://semaphoreci.com/sunny-g/xdr) -->
7+
[![Crates.io](https://img.shields.io/crates/v/serde_rustler.svg)](https://crates.io/crates/serde_rustler)
8+
[![Documentation](https://docs.rs/serde_rustler/badge.svg)](https://docs.rs/serde_rustler)
89

9-
## To load the NIF:
10+
`serde_rustler` provides [Serde](https://serde.rs) [Serializer]() and [Deserializer]() traits for [Rustler](https://github.com/rusterlium/rustler) types, so you can easily serialize and deserialize native Rust types directly to and from native Elixir terms within your [NIFs]().
1011

11-
```elixir
12-
defmodule Serde do
13-
use Rustler, otp_app: <otp-app>, crate: "serde_rustler"
12+
## Installation
1413

15-
# When your NIF is loaded, it will override this function.
16-
def add(_a, _b), do: :erlang.nif_error(:nif_not_loaded)
17-
end
14+
Install from [Crates.io](https://crates.io/crates/serde_rustler):
15+
16+
```toml
17+
[dependencies]
18+
serde_rustler = "0.0.1"
19+
```
20+
21+
## API Overview
22+
23+
```rust
1824
```
1925

20-
## Examples
26+
| Type Name | Serde Data Model | Default Elixir Term |
27+
|-----------|------------------|---------------------|
28+
| bool | `true` or `false` | `true` or `false` |
29+
| number | `i8`, `i16`, `i32`, `i64`, `u8`, `u16`, `u32`, `u64`, `f32`, `f64` | `number` |
30+
| char | `""` | `bitstring` |
31+
| string | `""` | `bitstring` |
32+
| byte array | `[u8]` | `<<_::_*8>>` |
33+
| option | `Some(T)` or `None` | `T` or `:nil` |
34+
| unit | `None` | `:nil` |
35+
| unit struct | `struct Unit` | `:nil` |
36+
| unit variant | `E::A` in `enum UnitVariant { A }` | *`:A` |
37+
| newtype struct | `struct Millimeters(u8)` | <sup>[1](#lossy)</sup>`{:Millimeters, u8}` |
38+
| newtype variant | `E::N` in `enum E { N(u8) }` | <sup>[1](#lossy)</sup>`{:N, u8}` |
39+
| seq | `Vec<T>` | `[T]` |
40+
| tuple | `(u8,)` | `{u8,}` |
41+
| tuple struct | `struct Rgb(u8, u8, u8)` | <sup>[1](#lossy)</sup>`{:Rgb, u8, u8, u8}` |
42+
| tuple variant | `E::T` in `enum E { T(u8, u8) }` | <sup>[1](#lossy)</sup>`{:T, u8, u8}` |
43+
| map | `HashMap<K, V>` | `%{}` |
44+
| struct | `struct Rgb { r: u8, g: u8, b: u8 }` | <sup>[1](#lossy)</sup>`%Rgb{ r: byte, g: byte, b: byte }` |
45+
| struct variant | `E::S` in `enum E { Rgb { r: u8, g: u8, b: u8 } }` | <sup>[1](#lossy)</sup>`%Rgb{ r: byte, g: byte, b: byte }` |
46+
47+
<a name="lossy">1</a>: If transcoding, the atom may not exist and will instead be serialized/deserialized to/from an Elixir bitstring. This means that may only be maps with a `__struct__` key and string value. These types are also lossy (newtype structs and variants are indistinguishable in Elixir terms), so deserialization hints may be required.
48+
49+
## Changelog
50+
51+
| Version | Change Summary |
52+
| ------- | ---------------|
53+
| [v0.0.1](https://crates.io/crates/serde_rustler/0.0.1) | initial release |
54+
55+
## Contributing
56+
57+
1. Fork it [https://github.com/your_username/xdr/fork](https://github.com/sunny-g/xdr/fork)
58+
2. Create your feature branch (`git checkout -b feature/fooBar`)
59+
3. Commit your changes (`git commit -am 'Add some fooBar'`)
60+
4. Push to the branch (`git push origin feature/fooBar`)
61+
5. Create a new Pull Request
62+
63+
## Maintainers
64+
65+
- Sunny G - [@sunny-g](https://github.com/sunny-g)
66+
67+
<!-- ## Contributors -->
68+
69+
## License
2170

22-
[This](https://github.com/hansihe/NifIo) is a complete example of a NIF written in Rust.
71+
MIT

serde_rustler/src/ser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{atoms, error::Error};
1+
use crate::{atoms, error::Error, util};
22
use rustler::{types::tuple, Encoder, Env, Term};
33
use serde::{
44
ser::{self, Serialize},
@@ -92,7 +92,7 @@ impl<'a> ser::Serializer for Serializer<'a> {
9292
}
9393

9494
fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> {
95-
Ok(v.encode(self.env))
95+
util::str_to_term(self.env, v)
9696
}
9797

9898
// TODO

serde_rustler/src/util.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use crate::error::Error;
2+
use rustler::{Encoder, Env, Term};
3+
4+
pub fn str_to_term<'a>(env: Env<'a>, string: &str) -> Result<Term<'a>, Error> {
5+
Ok(string.encode(env))
6+
}
7+
8+
pub fn term_to_str<'a>(term: Term<'a>) -> Result<&'a str, Error> {
9+
term.decode().or(Err(Error::InvalidStringable))
10+
}

serde_rustler_tests/mix.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ defmodule SerdeRustlerTests.Mixfile do
22
use Mix.Project
33

44
@name :serde_rustler_tests
5-
@version "0.0.1-dev"
5+
@version "0.0.1"
66
@description """
77
"""
88
@github "https://github.com/datalove-app/serde_rustler"
9-
@files ["mix.exs", "mix.lock", "lib", "test", "README.md"]
9+
@files ["mix.exs", "mix.lock", "lib", "native", "test", "README.md"]
1010
@maintainers ["Sunny G"]
1111
@licenses ["MIT"]
1212

@@ -53,8 +53,8 @@ defmodule SerdeRustlerTests.Mixfile do
5353
[ serde_rustler_tests:
5454
[ path: __DIR__ <> "/native/serde_rustler_tests",
5555
mode: (if Mix.env() == :prod, do: :release, else: :debug),
56-
# default_features: true,
57-
# features: [],
56+
default_features: true,
57+
features: [],
5858
]
5959
]
6060
end

serde_rustler_tests/native/serde_rustler_tests/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
[package]
22
name = "serde_rustler_tests"
3-
version = "0.1.0"
3+
version = "0.0.1"
44
authors = ["Sunny G <[email protected]>"]
55
edition = "2018"
6+
publish = false
67

78
[lib]
89
name = "serde_rustler_tests"

0 commit comments

Comments
 (0)