Created
September 14, 2019 20:45
-
-
Save Virviil/a2184c2e6bc5781d1ffd0e7530f1a306 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Compiling fluent v0.1.0 (/Users/virviil/dev/elixir/fluent/native/fluent) | |
error[E0277]: the trait bound `unic_langid_impl::LanguageIdentifier: std::convert::From<unic_langid_impl::LanguageIdentifier>` is not satisfied | |
--> src/lib.rs:85:22 | |
| | |
85 | let mut bundle = FluentBundle::new(&[langid_en]); | |
| ^^^^^^^^^^^^^^^^^ the trait `std::convert::From<unic_langid_impl::LanguageIdentifier>` is not implemented for `unic_langid_impl::LanguageIdentifier` | |
| | |
= note: required because of the requirements on the impl of `std::convert::Into<unic_langid_impl::LanguageIdentifier>` for `unic_langid_impl::LanguageIdentifier` | |
= note: required by `fluent_bundle::bundle::FluentBundle::<R>::new` | |
error: aborting due to previous error | |
For more information about this error, try `rustc --explain E0277`. | |
error: Could not compile `fluent`. | |
To learn more, run the command again with --verbose. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[macro_use] | |
extern crate rustler; | |
use rustler::{Encoder, Env, Error, Term, ResourceArc}; | |
use std::sync::RwLock; | |
use unic_langid::LanguageIdentifier; | |
use fluent::{FluentBundle, FluentResource}; | |
mod atoms { | |
rustler_atoms! { | |
atom ok; | |
//atom error; | |
//atom __true__ = "true"; | |
//atom __false__ = "false"; | |
} | |
} | |
struct Buffer { | |
data: RwLock<Vec<u8>>, | |
} | |
rustler::rustler_export_nifs! { | |
"Elixir.Fluent", | |
[ | |
("add", 2, add), | |
("new", 1, buffer_new), | |
("set", 3, buffer_set), | |
("get", 2, buffer_get) | |
], | |
Some(on_load) | |
} | |
fn on_load(env: Env, _info: Term) -> bool { | |
resource_struct_init!(Buffer, env); | |
true | |
} | |
fn buffer_new<'a>(env: Env<'a>, args: &[Term<'a>]) -> Result<Term<'a>, Error> { | |
let buffer_size: usize = args[0].decode()?; | |
let mut buffer = Vec::with_capacity(buffer_size); | |
for _i in 0..buffer_size { | |
buffer.push(0); | |
} | |
let buffer_struct = Buffer { | |
data: RwLock::new(buffer) | |
}; | |
Ok(ResourceArc::new(buffer_struct).encode(env)) | |
} | |
fn buffer_set<'a>(env: Env<'a>, args: &[Term<'a>]) -> Result<Term<'a>, Error> { | |
let buffer: ResourceArc<Buffer> = args[0].decode()?; | |
let offset: usize = args[1].decode()?; | |
let byte: u8 = args[2].decode()?; | |
buffer.data.write().unwrap()[offset] = byte; | |
Ok(byte.encode(env)) | |
} | |
fn buffer_get<'a>(env: Env<'a>, args: &[Term<'a>]) -> Result<Term<'a>, Error> { | |
let buffer: ResourceArc<Buffer> = args[0].decode()?; | |
let offset: usize = args[1].decode()?; | |
let byte = buffer.data.read().unwrap()[offset]; | |
Ok(byte.encode(env)) | |
} | |
fn add<'a>(env: Env<'a>, args: &[Term<'a>]) -> Result<Term<'a>, Error> { | |
let num1: i64 = args[0].decode()?; | |
let num2: i64 = args[1].decode()?; | |
Ok((atoms::ok(), num1 + num2).encode(env)) | |
} | |
fn fluent<'a>(env: Env<'a>, args: &[Term<'a>]) -> Result<Term<'a>, Error> { | |
let ftl_string = "hello-world = Hello, world!".to_owned(); | |
let res = FluentResource::try_new(ftl_string) | |
.expect("Failed to parse an FTL string."); | |
let langid_en: LanguageIdentifier = "en-US".parse::<LanguageIdentifier>().expect("Parsing failed."); | |
let mut bundle = FluentBundle::new(&[langid_en]); | |
bundle.add_resource(&res) | |
.expect("Failed to add FTL resources to the bundle."); | |
let msg = bundle.get_message("hello-world") | |
.expect("Message doesn't exist."); | |
let mut errors = vec![]; | |
let pattern = msg.value | |
.expect("Message has no value."); | |
let value = bundle.format_pattern(&pattern, None, &mut errors); | |
Ok((atoms::ok(), String::from(value)).encode(env)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment