Bug 1686965 - Scaffolding for intl::components::Segmenter. draft
authorZibi Braniecki <zbraniecki@mozilla.com>
Wed, 17 Feb 2021 06:39:36 +0000
changeset 3544886 7e31d2a969097c1978d0762942647a43c73653e2
parent 3544885 1daf53269a09085015aae8503529da9ada82bd1e
child 3544887 efd1a6dbc1b589c6801b73da4c81d59f633729e4
push idunknown
push userunknown
push dateunknown
bugs1686965
milestone87.0a1
Bug 1686965 - Scaffolding for intl::components::Segmenter. Summary: Depends on D105412 Test Plan: Reviewers: Subscribers: Bug #: 1686965 Differential Diff: PHID-DIFF-7olee24gqgkqvsbp54qb
intl/components/ICU4XBindings.h
intl/components/Segmenter.cpp
intl/components/Segmenter.h
intl/components/moz.build
intl/components/rust/icu4x-ffi/Cargo.toml
intl/components/rust/icu4x-ffi/cbindgen.toml
intl/components/rust/icu4x-ffi/src/lib.rs
intl/moz.build
new file mode 100644
--- /dev/null
+++ b/intl/components/ICU4XBindings.h
@@ -0,0 +1,24 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_intl_components_ICU4XBindings_h
+#define mozilla_intl_components_ICU4XBindings_h
+
+#include "mozilla/intl/icu4x_ffi_generated.h"
+
+#include "mozilla/RefPtr.h"
+
+namespace mozilla {
+
+template <>
+class DefaultDelete<intl::components::ffi::Segmenter> {
+ public:
+  void operator()(intl::components::ffi::Segmenter* aPtr) const {
+    segmenter_destroy(aPtr);
+  }
+};
+
+}  // namespace mozilla
+
+#endif
new file mode 100644
--- /dev/null
+++ b/intl/components/Segmenter.cpp
@@ -0,0 +1,19 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "Segmenter.h"
+
+namespace mozilla::intl::components {
+
+Segmenter::Segmenter() { mRaw.reset(ffi::segmenter_new()); }
+
+void Segmenter::Segment() {
+  bool val = ffi::segmenter_segment(mRaw.get());
+  MOZ_ASSERT(val, "Expected true");
+}
+
+Segmenter::~Segmenter() {}
+}  // namespace mozilla::intl::components
new file mode 100644
--- /dev/null
+++ b/intl/components/Segmenter.h
@@ -0,0 +1,28 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_intl_components_Segmenter_h
+#define mozilla_intl_components_Segmenter_h
+
+#include "mozilla/intl/components/ICU4XBindings.h"
+
+namespace mozilla::intl::components {
+
+class Segmenter {
+ public:
+  Segmenter();
+
+  void Segment();
+
+ protected:
+  ~Segmenter();
+
+  UniquePtr<ffi::Segmenter> mRaw;
+};
+
+}  // namespace mozilla::intl::components
+
+#endif
new file mode 100644
--- /dev/null
+++ b/intl/components/moz.build
@@ -0,0 +1,23 @@
+# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
+# vim: set filetype=python:
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+EXPORTS.mozilla.intl.components += [
+    "ICU4XBindings.h",
+    "Segmenter.h",
+]
+
+UNIFIED_SOURCES += [
+    "Segmenter.cpp",
+]
+
+if CONFIG["COMPILE_ENVIRONMENT"]:
+    CbindgenHeader("icu4x_ffi_generated.h", inputs=["/intl/components/rust/icu4x-ffi"])
+
+    EXPORTS.mozilla.intl += [
+        "!icu4x_ffi_generated.h",
+    ]
+
+FINAL_LIBRARY = "xul"
new file mode 100644
--- /dev/null
+++ b/intl/components/rust/icu4x-ffi/Cargo.toml
@@ -0,0 +1,10 @@
+[package]
+name = "icu4x-ffi"
+version = "0.1.0"
+authors = ["Zibi Braniecki <zibi@braniecki.net>"]
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+icu_segmenter = { git = "https://github.com/zbraniecki/icu4x", branch = "segmenter" }
new file mode 100644
--- /dev/null
+++ b/intl/components/rust/icu4x-ffi/cbindgen.toml
@@ -0,0 +1,24 @@
+header = """/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */"""
+autogen_warning = """/* DO NOT MODIFY THIS MANUALLY! This file was generated using cbindgen. See RunCbindgen.py */
+#ifndef mozilla_intl_components_ICU4XBindings_h
+#error "Don't include this file directly, instead include ICU4XBindings.h"
+#endif
+"""
+include_version = true
+braces = "SameLine"
+line_length = 100
+tab_width = 2
+language = "C++"
+namespaces = ["mozilla", "intl", "components", "ffi"]
+
+[parse]
+parse_deps = true
+include = ["icu4x", "icu_segmenter"]
+
+[enum]
+derive_helper_methods = true
+
+[export.rename]
+"ThinVec" = "nsTArray"
new file mode 100644
--- /dev/null
+++ b/intl/components/rust/icu4x-ffi/src/lib.rs
@@ -0,0 +1,25 @@
+pub use icu_segmenter::{Segmenter, SegmenterOptions, LanguageIdentifier, options};
+
+#[no_mangle]
+pub unsafe extern "C" fn segmenter_new() -> *mut Segmenter {
+
+    let segmenter = Segmenter::try_new(
+        LanguageIdentifier::und(),
+        SegmenterOptions {
+            granularity: options::SegmenterGranularity::Line
+        },
+    ).expect("Failed to construct Segmenter");
+
+    Box::into_raw(Box::new(segmenter))
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn segmenter_segment(_segmenter: &Segmenter) -> bool {
+    // Here you will likely want to return an instance of an Iterator
+    true
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn segmenter_destroy(segmenter: *mut Segmenter) {
+    let _ = Box::from_raw(segmenter);
+}
--- a/intl/moz.build
+++ b/intl/moz.build
@@ -11,16 +11,17 @@ TEST_DIRS += [
 DIRS += [
     "hyphenation/glue",
     "locale",
     "locales",
     "lwbrk",
     "strres",
     "unicharutil",
     "l10n",
+    "components",
 ]
 
 DIRS += [
     "uconv",
     "build",
 ]
 
 EXPORTS.mozilla += [