Current section
4 Versions
Jump to
Current section
4 Versions
Compare versions
4
files changed
+34
additions
-38
deletions
| @@ -1,6 +1,6 @@ | |
| 1 | - {<<"links">>,[{<<"GitHub">>,<<"https://github.com/err931/magika_ex">>}]}. |
| 1 | + {<<"links">>,[{<<"SourceHut">>,<<"https://git.sr.ht/~err931/magika_ex">>}]}. |
| 2 2 | {<<"name">>,<<"magika_ex">>}. |
| 3 | - {<<"version">>,<<"0.1.1">>}. |
| 3 | + {<<"version">>,<<"0.1.2">>}. |
| 4 4 | {<<"description">>,<<"Elixir NIF wrapper for google/magika">>}. |
| 5 5 | {<<"elixir">>,<<"~> 1.18">>}. |
| 6 6 | {<<"app">>,<<"magika_ex">>}. |
| @@ -25,11 +25,7 @@ defmodule MagikaEx do | |
| 25 25 | @spec identify_bytes(binary()) :: {:ok, MagikaEx.Result.t()} | {:error, term} |
| 26 26 | def identify_bytes(data) when is_binary(data) do |
| 27 27 | resource = GenServer.call(__MODULE__, :get_resource) |
| 28 | - |
| 29 | - case Native.identify_bytes(resource, data) do |
| 30 | - {:error, _} = err -> err |
| 31 | - result -> {:ok, result} |
| 32 | - end |
| 28 | + {:ok, Native.identify_bytes(resource, data)} |
| 33 29 | end |
| 34 30 | |
| 35 31 | @doc """ |
| @@ -37,11 +33,8 @@ defmodule MagikaEx do | |
| 37 33 | """ |
| 38 34 | @spec identify_path(String.t()) :: {:ok, MagikaEx.Result.t()} | {:error, term} |
| 39 35 | def identify_path(path) when is_binary(path) do |
| 40 | - with {:ok, data} <- File.read(path), |
| 41 | - {:ok, result} <- identify_bytes(data) do |
| 42 | - {:ok, result} |
| 43 | - else |
| 44 | - {:error, _} = err -> err |
| 36 | + with {:ok, binary} <- File.read(path) do |
| 37 | + identify_bytes(binary) |
| 45 38 | end |
| 46 39 | end |
| 47 40 | |
| @@ -50,9 +43,6 @@ defmodule MagikaEx do | |
| 50 43 | case Native.new() do |
| 51 44 | resource when is_reference(resource) -> |
| 52 45 | {:ok, resource} |
| 53 | - |
| 54 | - {:error, reason} -> |
| 55 | - {:stop, reason} |
| 56 46 | end |
| 57 47 | end |
| @@ -1,8 +1,8 @@ | |
| 1 1 | defmodule MagikaEx.MixProject do |
| 2 2 | use Mix.Project |
| 3 3 | |
| 4 | - @source_url "https://github.com/err931/magika_ex" |
| 5 | - @version "0.1.1" |
| 4 | + @source_url "https://git.sr.ht/~err931/magika_ex" |
| 5 | + @version "0.1.2" |
| 6 6 | |
| 7 7 | def project do |
| 8 8 | [ |
| @@ -28,7 +28,7 @@ defmodule MagikaEx.MixProject do | |
| 28 28 | [ |
| 29 29 | maintainers: ["Minoru Maekawa"], |
| 30 30 | licenses: ["Apache-2.0"], |
| 31 | - links: %{"GitHub" => @source_url}, |
| 31 | + links: %{"SourceHut" => @source_url}, |
| 32 32 | files: ~w(lib native .formatter.exs mix.exs README.md LICENSE) |
| 33 33 | ] |
| 34 34 | end |
| @@ -1,29 +1,40 @@ | |
| 1 1 | use magika::Session; |
| 2 | - use rustler::{Binary, Env, NifResult, ResourceArc, Term}; |
| 2 | + use rustler::{Binary, Env, NifResult, Resource, ResourceArc, Term}; |
| 3 3 | use std::sync::Mutex; |
| 4 4 | |
| 5 5 | struct MagikaResource { |
| 6 6 | session: Mutex<Session>, |
| 7 7 | } |
| 8 8 | |
| 9 | + #[rustler::resource_impl] |
| 10 | + impl Resource for MagikaResource {} |
| 11 | + |
| 9 12 | #[derive(rustler::NifStruct)] |
| 10 13 | #[module = "MagikaEx.Result"] |
| 11 14 | struct MagikaResult { |
| 15 | + /// The unique label identifying this file type. |
| 12 16 | pub label: String, |
| 13 | - pub score: f32, |
| 17 | + |
| 18 | + /// The MIME type of the file type. |
| 14 19 | pub mime_type: String, |
| 20 | + |
| 21 | + /// The group of the file type. |
| 15 22 | pub group: String, |
| 23 | + |
| 24 | + /// The description of the file type. |
| 16 25 | pub description: String, |
| 26 | + |
| 27 | + /// The inference score between 0 and 1. |
| 28 | + pub score: f32, |
| 29 | + |
| 30 | + /// Whether the file type is text. |
| 17 31 | pub is_text: bool, |
| 18 32 | } |
| 19 33 | |
| 20 34 | #[rustler::nif] |
| 21 35 | fn new() -> NifResult<ResourceArc<MagikaResource>> { |
| 22 36 | let session = Session::new().map_err(|e| { |
| 23 | - rustler::Error::Term(Box::new(format!( |
| 24 | - "Failed to create Magika session: {:?}", |
| 25 | - e |
| 26 | - ))) |
| 37 | + rustler::Error::Term(Box::new(format!("Failed to create Magika session: {e:?}"))) |
| 27 38 | })?; |
| 28 39 | |
| 29 40 | Ok(ResourceArc::new(MagikaResource { |
| @@ -40,27 +51,22 @@ fn identify_bytes(resource: ResourceArc<MagikaResource>, data: Binary) -> NifRes | |
| 40 51 | |
| 41 52 | let result = session |
| 42 53 | .identify_content_sync(data.as_slice()) |
| 43 | - .map_err(|e| rustler::Error::Term(Box::new(format!("Magika error: {:?}", e))))?; |
| 54 | + .map_err(|e| rustler::Error::Term(Box::new(format!("Magika error: {e:?}"))))?; |
| 44 55 | |
| 45 56 | let info = result.info(); |
| 46 | - let score = result.score(); |
| 47 57 | |
| 48 | - let response = MagikaResult { |
| 49 | - label: info.label.to_string(), |
| 50 | - score: score, |
| 51 | - mime_type: info.mime_type.to_string(), |
| 52 | - group: info.group.to_string(), |
| 53 | - description: info.description.to_string(), |
| 58 | + Ok(MagikaResult { |
| 59 | + label: info.label.into(), |
| 60 | + mime_type: info.mime_type.into(), |
| 61 | + group: info.group.into(), |
| 62 | + description: info.description.into(), |
| 63 | + score: result.score(), |
| 54 64 | is_text: info.is_text, |
| 55 | - }; |
| 56 | - |
| 57 | - Ok(response) |
| 65 | + }) |
| 58 66 | } |
| 59 67 | |
| 60 | - #[allow(non_local_definitions)] |
| 61 68 | fn on_load(env: Env, _info: Term) -> bool { |
| 62 | - let _ = rustler::resource!(MagikaResource, env); |
| 63 | - true |
| 69 | + env.register::<MagikaResource>().is_ok() |
| 64 70 | } |
| 65 71 | |
| 66 72 | rustler::init!("Elixir.MagikaEx.Native", load = on_load); |