Skip to content

Commit 246ace5

Browse files
committed
First commit. Basic functionality already provided
0 parents  commit 246ace5

File tree

12 files changed

+674
-0
lines changed

12 files changed

+674
-0
lines changed

.formatter.exs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Used by "mix format"
2+
[
3+
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
4+
]

.gitignore

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# The directory Mix will write compiled artifacts to.
2+
/_build/
3+
4+
# If you run "mix test --cover", coverage assets end up here.
5+
/cover/
6+
7+
# The directory Mix downloads your dependencies sources to.
8+
/deps/
9+
10+
# Where 3rd-party dependencies like ExDoc output generated docs.
11+
/doc/
12+
13+
# Ignore .fetch files in case you like to edit your project deps locally.
14+
/.fetch
15+
16+
# If the VM crashes, it generates a dump, let's ignore it too.
17+
erl_crash.dump
18+
19+
# Also ignore archive artifacts (built via "mix archive.build").
20+
*.ez
21+
22+
# Ignore package tarball (built via "mix hex.build").
23+
qr_coder-*.tar
24+
25+
# Rust stuff
26+
/native/qr_coder/target/
27+
/priv/native/*.so
28+
29+
.DS_Store

README.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# QRCoder
2+
3+
An Elixir Rust NIF to create SVG QR codes.
4+
It's a wrapper around [qrcode-rust](https://github.com/kennytm/qrcode-rust).
5+
6+
## Installation
7+
8+
You can add this package to your project dependencies:
9+
10+
```elixir
11+
# mix.exs
12+
def deps do
13+
[
14+
{:qr_coder, git: "https://github.com/pggalaviz/qrcoder.git"}
15+
]
16+
end
17+
```
18+
## Usage
19+
20+
You can then call the function:
21+
22+
```elixir
23+
{:ok, svg} = QRCoder.generate_svg("Elixir is awesome")
24+
```
25+
This will return a `tuple` where **svg** is black & white svg binary.
26+
27+
You can change the dark color by providing a valid HEX color:
28+
29+
```elixir
30+
{:ok, svg} = QRCoder.generate_svg("Elixir is awesome", "#d5dae6")
31+
```
32+
33+
You can then save it:
34+
35+
```elixir
36+
{:ok, svg} = QRCoder.generate_svg("Elixir is awesome")
37+
File..write("/your/path/qrcode.svg", svg)
38+
```
39+
40+
Or just return the binary:
41+
42+
```elixir
43+
# inside a Phoenix controller
44+
{:ok, svg} = QRCoder.generate_svg("Elixir is awesome")
45+
46+
conn
47+
|> put_resp_header("content-type", "image/svg+xml")
48+
|> put_resp_header("cache-control", "private")
49+
|> send_resp(200, svg)
50+
```

config/config.exs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This file is responsible for configuring your application
2+
# and its dependencies with the aid of the Mix.Config module.
3+
use Mix.Config
4+
5+
# This configuration is loaded before any dependency and is restricted
6+
# to this project. If another project depends on this project, this
7+
# file won't be loaded nor affect the parent project. For this reason,
8+
# if you want to provide default values for your application for
9+
# 3rd-party users, it should be done in your "mix.exs" file.
10+
11+
# You can configure your application as:
12+
#
13+
# config :qr_coder, key: :value
14+
#
15+
# and access this configuration in your application as:
16+
#
17+
# Application.get_env(:qr_coder, :key)
18+
#
19+
# You can also configure a 3rd-party app:
20+
#
21+
# config :logger, level: :info
22+
#
23+
24+
# It is also possible to import configuration files, relative to this
25+
# directory. For example, you can emulate configuration per environment
26+
# by uncommenting the line below and defining dev.exs, test.exs and such.
27+
# Configuration from the imported file will override the ones defined
28+
# here (which is why it is important to import them last).
29+
#
30+
# import_config "#{Mix.env()}.exs"

lib/qr_coder.ex

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
defmodule QRCoder do
2+
@moduledoc """
3+
Generates an svg binary with given data.
4+
"""
5+
6+
use Rustler, otp_app: :qr_coder, crate: :qr_coder
7+
8+
@spec generate_svg(String.t()) :: {:ok, String.t()} | {:error, :invalid}
9+
def generate_svg(bin) when is_binary(bin) do
10+
generate_svg(bin, "#000000")
11+
end
12+
13+
def generate_svg(_), do: {:error, :invalid}
14+
15+
@doc """
16+
Generate SVG QRCode
17+
## Example
18+
iex> QRCoder.generate_svg("Elixir is awesome")
19+
{:ok, "<svg>...</svg>"}
20+
"""
21+
@spec generate_svg(String.t(), String.t()) :: {:ok, String.t()}
22+
def generate_svg(_, _), do: exit(:nif_not_loaded)
23+
end

mix.exs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
defmodule QRCoder.MixProject do
2+
use Mix.Project
3+
4+
def project do
5+
[
6+
app: :qr_coder,
7+
version: "0.1.0",
8+
elixir: "~> 1.7",
9+
build_embedded: Mix.env() == :prod,
10+
start_permanent: Mix.env() == :prod,
11+
compilers: [:rustler] ++ Mix.compilers(),
12+
rustler_crates: rustler_crates(),
13+
deps: deps()
14+
]
15+
end
16+
17+
# Run "mix help compile.app" to learn about applications.
18+
def application do
19+
[
20+
extra_applications: [:logger]
21+
]
22+
end
23+
24+
# Run "mix help deps" to learn about dependencies.
25+
defp deps do
26+
[
27+
{:rustler, "~> 0.18.0"}
28+
]
29+
end
30+
31+
defp rustler_crates do
32+
[
33+
qr_coder: [
34+
path: "native/qr_coder",
35+
mode: if(Mix.env() == :prod, do: :release, else: :debug)
36+
]
37+
]
38+
end
39+
end

mix.lock

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
%{
2+
"rustler": {:hex, :rustler, "0.18.0", "db4bd0c613d83a1badc31be90ddada6f9821de29e4afd15c53a5da61882e4f2d", [:mix], [], "hexpm"},
3+
}

0 commit comments

Comments
 (0)