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

setup linter #3

Merged
merged 3 commits into from
Jul 20, 2024
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
4 changes: 4 additions & 0 deletions .github/workflows/elixir.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ jobs:
run: mix deps.get
- name: Run tests
run: mix test
- name: format check
run: mix format --check-formatted
- name: Run credo
run: mix credo
25 changes: 25 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
on:
push:
branches:
- main
paths:
- 'native/**'
pull_request:
paths:
- 'native/**'
workflow_dispatch:


jobs:
lint:
name: Rust linter
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Run clippy
run: cargo clippy
working-directory: native/yex
- name: format check
run: cargo fmt --check
working-directory: native/yex
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ y_ex-*.tar
# Temporary files, for example, from tests.
/tmp/

priv/native/*.so
priv/native/*.so

checksum-*.exs
15 changes: 15 additions & 0 deletions lib/array.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
defmodule Yex.Array do
@moduledoc """
A shareable Array-like type that supports efficient insert/delete of elements at any position.
"""
defstruct [
:reference
]
Expand Down Expand Up @@ -45,6 +48,18 @@ defmodule Yex.Array do
end

defmodule Yex.ArrayPrelim do
@moduledoc """
A preliminary array. It can be used to early initialize the contents of a Array.

## Examples
iex> doc = Yex.Doc.new()
iex> map = Yex.Doc.get_map(doc, "map")
iex> Yex.Map.set(map, "key", Yex.ArrayPrelim.from(["Hello", "World"]))
iex> {:ok, %Yex.Array{} = array} = Yex.Map.get(map, "key")
iex> Yex.Array.get(array, 1)
{:ok, "World"}

"""
defstruct [
:list
]
Expand Down
3 changes: 3 additions & 0 deletions lib/doc.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
defmodule Yex.Doc do
defmodule Options do
@moduledoc """
Document options.
"""
defstruct client_id: 0,
guid: nil,
collection_id: nil,
Expand Down
14 changes: 14 additions & 0 deletions lib/map.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
defmodule Yex.Map do
@moduledoc """
A shareable Map type.
"""
defstruct [
:reference
]
Expand Down Expand Up @@ -33,6 +36,17 @@ defmodule Yex.Map do
end

defmodule Yex.MapPrelim do
@moduledoc """
A preliminary map. It can be used to early initialize the contents of a Map.

## Examples
iex> doc = Yex.Doc.new()
iex> array = Yex.Doc.get_array(doc, "array")
iex> Yex.Array.insert(array, 0, Yex.MapPrelim.from(%{ "key" => "value" }))
iex> {:ok, %Yex.Map{} = map} = Yex.Array.get(array, 0)
iex> Yex.Map.get(map, "key")
{:ok, "value"}
"""
defstruct [
:map
]
Expand Down
6 changes: 3 additions & 3 deletions lib/nif.ex
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
defmodule Yex.Nif do
@moduledoc false
# Do not use directly

version = Mix.Project.config()[:version]

use RustlerPrecompiled,
otp_app: :y_ex,
crate: "yex",
base_url:
"https://github.com/satoren/y_ex/releases/download/v#{version}",
base_url: "https://github.com/satoren/y_ex/releases/download/v#{version}",
force_build: System.get_env("RUSTLER_PRECOMPILATION_YEX_BUILD") in ["1", "true"],
version: version


def doc_new(), do: :erlang.nif_error(:nif_not_loaded)
def doc_with_options(_option), do: :erlang.nif_error(:nif_not_loaded)
def doc_get_or_insert_text(_doc, _name), do: :erlang.nif_error(:nif_not_loaded)
Expand Down
4 changes: 3 additions & 1 deletion lib/text.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
defmodule Yex.Text do
@moduledoc """
A shareable type that is optimized for shared editing on text.
"""
defstruct [
:reference
]
Expand Down Expand Up @@ -37,7 +40,6 @@ defmodule Yex.Text do
end

def to_json(%__MODULE__{} = _text) do
# todo: need to implement
raise "Not implemented"
end
end
5 changes: 3 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Yex.MixProject do
use Mix.Project

@version "0.0.3"
@version "0.0.4"
@repo "https://github.com/satoren/y_ex"

@description """
Expand Down Expand Up @@ -51,7 +51,8 @@ defmodule Yex.MixProject do
[
{:rustler, ">= 0.0.0", optional: true},
{:rustler_precompiled, "~> 0.7"},
{:ex_doc, "~> 0.29", only: :dev, runtime: false}
{:ex_doc, "~> 0.29", only: :dev, runtime: false},
{:credo, "~> 1.7", only: [:dev, :test], runtime: false}
]
end
end
3 changes: 3 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
%{
"bunt": {:hex, :bunt, "1.0.0", "081c2c665f086849e6d57900292b3a161727ab40431219529f13c4ddcf3e7a44", [:mix], [], "hexpm", "dc5f86aa08a5f6fa6b8096f0735c4e76d54ae5c9fa2c143e5a1fc7c1cd9bb6b5"},
"castore": {:hex, :castore, "1.0.8", "dedcf20ea746694647f883590b82d9e96014057aff1d44d03ec90f36a5c0dc6e", [:mix], [], "hexpm", "0b2b66d2ee742cb1d9cb8c8be3b43c3a70ee8651f37b75a8b982e036752983f1"},
"credo": {:hex, :credo, "1.7.7", "771445037228f763f9b2afd612b6aa2fd8e28432a95dbbc60d8e03ce71ba4446", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "8bc87496c9aaacdc3f90f01b7b0582467b69b4bd2441fe8aae3109d843cc2f2e"},
"earmark_parser": {:hex, :earmark_parser, "1.4.41", "ab34711c9dc6212dda44fcd20ecb87ac3f3fce6f0ca2f28d4a00e4154f8cd599", [:mix], [], "hexpm", "a81a04c7e34b6617c2792e291b5a2e57ab316365c2644ddc553bb9ed863ebefa"},
"ex_doc": {:hex, :ex_doc, "0.34.2", "13eedf3844ccdce25cfd837b99bea9ad92c4e511233199440488d217c92571e8", [:mix], [{:earmark_parser, "~> 1.4.39", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "5ce5f16b41208a50106afed3de6a2ed34f4acfd65715b82a0b84b49d995f95c1"},
"file_system": {:hex, :file_system, "1.0.0", "b689cc7dcee665f774de94b5a832e578bd7963c8e637ef940cd44327db7de2cd", [:mix], [], "hexpm", "6752092d66aec5a10e662aefeed8ddb9531d79db0bc145bb8c40325ca1d8536d"},
"jason": {:hex, :jason, "1.4.3", "d3f984eeb96fe53b85d20e0b049f03e57d075b5acda3ac8d465c969a2536c17b", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "9a90e868927f7c777689baa16d86f4d0e086d968db5c05d917ccff6d443e58a3"},
"makeup": {:hex, :makeup, "1.1.2", "9ba8837913bdf757787e71c1581c21f9d2455f4dd04cfca785c70bbfff1a76a3", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cce1566b81fbcbd21eca8ffe808f33b221f9eee2cbc7a1706fc3da9ff18e6cac"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.2", "627e84b8e8bf22e60a2579dad15067c755531fea049ae26ef1020cad58fe9578", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "41193978704763f6bbe6cc2758b84909e62984c7752b3784bd3c218bb341706b"},
Expand Down
2 changes: 1 addition & 1 deletion native/yex/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn decode<'a>(term: Term<'a>) -> NifResult<Any> {
return Ok(Any::from(a));
}

return Err(rustler::Error::BadArg);
Err(rustler::Error::BadArg)
}

pub type NifAny = NifWrap<Any>;
Expand Down
6 changes: 3 additions & 3 deletions native/yex/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ pub struct NifArray {
impl NifArray {
pub fn new(doc: ResourceArc<DocResource>, array: ArrayRef) -> Self {
NifArray {
doc: doc,
doc,
reference: ResourceArc::new(array.into()),
}
}

pub fn insert(&self, index: u32, input: NifYInput) -> Result<(), NifError> {
if let Some(mut txn) = self.doc.current_transaction.borrow_mut().as_mut() {
self.reference.insert(&mut txn, index, input);
if let Some(txn) = self.doc.current_transaction.borrow_mut().as_mut() {
self.reference.insert(txn, index, input);
Ok(())
} else {
let mut txn = self.doc.0.doc.transact_mut();
Expand Down
12 changes: 6 additions & 6 deletions native/yex/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ impl From<NifOptions> for Options {
} else {
uuid_v4()
};
return Options {
Options {
client_id: w.client_id,
guid: guid,
guid,
collection_id: w.collection_id,
offset_kind: offset_kind,
offset_kind,
skip_gc: w.skip_gc,
auto_load: w.auto_load,
should_load: w.should_load,
};
}
}
}

Expand All @@ -107,7 +107,7 @@ impl NifDoc {
NifDoc {
reference: ResourceArc::new(
DocInner {
doc: doc,
doc,
current_transaction: RefCell::new(None),
}
.into(),
Expand Down Expand Up @@ -148,7 +148,7 @@ impl NifDoc {
Ok(())
}

pub fn commit_transaction(&self) -> () {
pub fn commit_transaction(&self) {
*self.reference.current_transaction.borrow_mut() = None;
}

Expand Down
4 changes: 2 additions & 2 deletions native/yex/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ pub struct NifError {

impl From<yrs::doc::TransactionAcqError> for NifError {
fn from(w: yrs::doc::TransactionAcqError) -> NifError {
return NifError {
NifError {
reason: atoms::transaction_acq_error(),
message: w.to_string(),
};
}
}
}
39 changes: 18 additions & 21 deletions native/yex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,17 @@ fn doc_with_options(option: NifOptions) -> NifDoc {
}

#[rustler::nif]
fn doc_get_or_insert_text<'a>(env: Env<'a>, doc: NifDoc, name: &str) -> NifText {
fn doc_get_or_insert_text(env: Env<'_>, doc: NifDoc, name: &str) -> NifText {
ENV.set(&mut env.clone(), || doc.get_or_insert_text(name))
}

#[rustler::nif]
fn doc_get_or_insert_array<'a>(env: Env<'a>, doc: NifDoc, name: &str) -> NifArray {
fn doc_get_or_insert_array(env: Env<'_>, doc: NifDoc, name: &str) -> NifArray {
ENV.set(&mut env.clone(), || doc.get_or_insert_array(name))
}

#[rustler::nif]
fn doc_get_or_insert_map<'a>(env: Env<'a>, doc: NifDoc, name: &str) -> NifMap {
fn doc_get_or_insert_map(env: Env<'_>, doc: NifDoc, name: &str) -> NifMap {
ENV.set(&mut env.clone(), || doc.get_or_insert_map(name))
}

Expand All @@ -108,7 +108,7 @@ fn doc_begin_transaction(doc: NifDoc, origin: Option<&str>) -> Result<(), NifErr
}

#[rustler::nif]
fn doc_commit_transaction<'a>(env: Env<'a>, doc: NifDoc) -> () {
fn doc_commit_transaction(env: Env<'_>, doc: NifDoc) {
ENV.set(&mut env.clone(), || doc.commit_transaction())
}

Expand All @@ -128,13 +128,13 @@ fn doc_monitor_update_v2(
}

#[rustler::nif]
fn text_insert<'a>(env: Env<'a>, text: NifText, index: u32, chunk: &str) -> Result<(), NifError> {
fn text_insert(env: Env<'_>, text: NifText, index: u32, chunk: &str) -> Result<(), NifError> {
ENV.set(&mut env.clone(), || text.insert(index, chunk))
}

#[rustler::nif]
fn text_insert_with_attributes<'a>(
env: Env<'a>,
fn text_insert_with_attributes(
env: Env<'_>,
text: NifText,
index: u32,
chunk: &str,
Expand All @@ -146,13 +146,13 @@ fn text_insert_with_attributes<'a>(
}

#[rustler::nif]
fn text_delete<'a>(env: Env<'a>, text: NifText, index: u32, len: u32) -> Result<(), NifError> {
fn text_delete(env: Env<'_>, text: NifText, index: u32, len: u32) -> Result<(), NifError> {
ENV.set(&mut env.clone(), || text.delete(index, len))
}

#[rustler::nif]
fn text_format<'a>(
env: Env<'a>,
fn text_format(
env: Env<'_>,
text: NifText,
index: u32,
len: u32,
Expand All @@ -171,8 +171,8 @@ fn text_length(text: NifText) -> u32 {
}

#[rustler::nif]
fn array_insert<'a>(
env: Env<'a>,
fn array_insert(
env: Env<'_>,
array: NifArray,
index: u32,
value: NifYInput,
Expand All @@ -188,8 +188,8 @@ fn array_get(array: NifArray, index: u32) -> Result<NifValue, NifError> {
array.get(index)
}
#[rustler::nif]
fn array_delete_range<'a>(
env: Env<'a>,
fn array_delete_range(
env: Env<'_>,
array: NifArray,
index: u32,
length: u32,
Expand All @@ -206,7 +206,7 @@ fn array_to_json(array: NifArray) -> NifAny {
}

#[rustler::nif]
fn map_set<'a>(env: Env<'a>, map: NifMap, key: &str, value: NifYInput) -> Result<(), NifError> {
fn map_set(env: Env<'_>, map: NifMap, key: &str, value: NifYInput) -> Result<(), NifError> {
ENV.set(&mut env.clone(), || map.set(key, value))
}
#[rustler::nif]
Expand All @@ -218,7 +218,7 @@ fn map_get(map: NifMap, key: &str) -> Result<NifValue, NifError> {
map.get(key)
}
#[rustler::nif]
fn map_delete<'a>(env: Env<'a>, map: NifMap, key: &str) -> Result<(), NifError> {
fn map_delete(env: Env<'_>, map: NifMap, key: &str) -> Result<(), NifError> {
ENV.set(&mut env.clone(), || map.delete(key))
}
#[rustler::nif]
Expand All @@ -231,10 +231,7 @@ fn map_to_json(map: NifMap) -> NifAny {
}

#[rustler::nif]
fn sub_unsubscribe<'a>(
env: Env<'a>,
sub: ResourceArc<SubscriptionResource>,
) -> Result<(), NifError> {
fn sub_unsubscribe(env: Env<'_>, sub: ResourceArc<SubscriptionResource>) -> Result<(), NifError> {
ENV.set(&mut env.clone(), || {
*sub.borrow_mut() = None;
Ok(())
Expand All @@ -261,7 +258,7 @@ fn encode_state_as_update<'a>(
})
}
#[rustler::nif]
fn apply_update<'a>(env: Env<'a>, doc: NifDoc, update: Binary) -> Result<(), NifError> {
fn apply_update(env: Env<'_>, doc: NifDoc, update: Binary) -> Result<(), NifError> {
ENV.set(&mut env.clone(), || doc.apply_update(update.as_slice()))
}

Expand Down
6 changes: 3 additions & 3 deletions native/yex/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct NifMap {
impl NifMap {
pub fn new(doc: ResourceArc<DocResource>, map: MapRef) -> Self {
NifMap {
doc: doc,
doc,
reference: ResourceArc::new(map.into()),
}
}
Expand All @@ -36,8 +36,8 @@ impl NifMap {
}

pub fn set(&self, key: &str, input: NifYInput) -> Result<(), NifError> {
if let Some(mut txn) = self.doc.current_transaction.borrow_mut().as_mut() {
self.reference.insert(&mut txn, key, input);
if let Some(txn) = self.doc.current_transaction.borrow_mut().as_mut() {
self.reference.insert(txn, key, input);
Ok(())
} else {
let mut txn = self.doc.0.doc.transact_mut();
Expand Down
Loading
Loading