diff options
Diffstat (limited to 'toolkit/components/uniffi-fixtures/geometry')
9 files changed, 141 insertions, 0 deletions
diff --git a/toolkit/components/uniffi-fixtures/geometry/Cargo.toml b/toolkit/components/uniffi-fixtures/geometry/Cargo.toml new file mode 100644 index 0000000000..a35ce906aa --- /dev/null +++ b/toolkit/components/uniffi-fixtures/geometry/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "uniffi-example-geometry" +edition = "2021" +version = "0.22.0" +authors = ["Firefox Sync Team <sync-team@mozilla.com>"] +license = "MPL-2.0" +publish = false + +[lib] +crate-type = ["lib", "cdylib"] +name = "uniffi_geometry" + +[dependencies] +uniffi = { workspace = true } + +[build-dependencies] +uniffi = { workspace = true, features = ["build"] } + +[dev-dependencies] +uniffi = { workspace = true, features = ["bindgen-tests"] } diff --git a/toolkit/components/uniffi-fixtures/geometry/build.rs b/toolkit/components/uniffi-fixtures/geometry/build.rs new file mode 100644 index 0000000000..2dd1c96bc3 --- /dev/null +++ b/toolkit/components/uniffi-fixtures/geometry/build.rs @@ -0,0 +1,7 @@ +/* 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/. */ + +fn main() { + uniffi::generate_scaffolding("src/geometry.udl").unwrap(); +} diff --git a/toolkit/components/uniffi-fixtures/geometry/src/geometry.udl b/toolkit/components/uniffi-fixtures/geometry/src/geometry.udl new file mode 100644 index 0000000000..af60d429bf --- /dev/null +++ b/toolkit/components/uniffi-fixtures/geometry/src/geometry.udl @@ -0,0 +1,15 @@ + +namespace geometry { + double gradient(Line ln); + Point? intersection(Line ln1, Line ln2); +}; + +dictionary Point { + double coord_x; + double coord_y; +}; + +dictionary Line { + Point start; + Point end; +}; diff --git a/toolkit/components/uniffi-fixtures/geometry/src/lib.rs b/toolkit/components/uniffi-fixtures/geometry/src/lib.rs new file mode 100644 index 0000000000..d710b150bb --- /dev/null +++ b/toolkit/components/uniffi-fixtures/geometry/src/lib.rs @@ -0,0 +1,47 @@ +/* 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/. */ + +// https://rust-lang.github.io/rust-clippy/master/index.html#float_cmp +// Silence, clippy! +const EPSILON: f64 = 0.0001f64; + +#[derive(Debug, Clone)] +pub struct Point { + coord_x: f64, + coord_y: f64, +} + +#[derive(Debug, Clone)] +pub struct Line { + start: Point, + end: Point, +} + +pub fn gradient(ln: Line) -> f64 { + let rise = ln.end.coord_y - ln.start.coord_y; + let run = ln.end.coord_x - ln.start.coord_x; + rise / run +} + +pub fn intersection(ln1: Line, ln2: Line) -> Option<Point> { + // TODO: yuck, should be able to take &Line as argument here + // and have rust figure it out with a bunch of annotations... + let g1 = gradient(ln1.clone()); + let z1 = ln1.start.coord_y - g1 * ln1.start.coord_x; + let g2 = gradient(ln2.clone()); + let z2 = ln2.start.coord_y - g2 * ln2.start.coord_x; + // Parallel lines do not intersect. + if (g1 - g2).abs() < EPSILON { + return None; + } + // Otherwise, they intersect at this fancy calculation that + // I found on wikipedia. + let x = (z2 - z1) / (g1 - g2); + Some(Point { + coord_x: x, + coord_y: g1 * x + z1, + }) +} + +uniffi::include_scaffolding!("geometry"); diff --git a/toolkit/components/uniffi-fixtures/geometry/tests/bindings/test_geometry.kts b/toolkit/components/uniffi-fixtures/geometry/tests/bindings/test_geometry.kts new file mode 100644 index 0000000000..77bb9932ec --- /dev/null +++ b/toolkit/components/uniffi-fixtures/geometry/tests/bindings/test_geometry.kts @@ -0,0 +1,10 @@ +import uniffi.geometry.*; + +val ln1 = Line(Point(0.0,0.0), Point(1.0,2.0)) +val ln2 = Line(Point(1.0,1.0), Point(2.0,2.0)) + +assert( gradient(ln1) == 2.0 ) +assert( gradient(ln2) == 1.0 ) + +assert( intersection(ln1, ln2) == Point(0.0, 0.0) ) +assert( intersection(ln1, ln1) == null ) diff --git a/toolkit/components/uniffi-fixtures/geometry/tests/bindings/test_geometry.py b/toolkit/components/uniffi-fixtures/geometry/tests/bindings/test_geometry.py new file mode 100644 index 0000000000..bfb6560626 --- /dev/null +++ b/toolkit/components/uniffi-fixtures/geometry/tests/bindings/test_geometry.py @@ -0,0 +1,10 @@ +from geometry import Line, Point, gradient, intersection + +ln1 = Line(start=Point(coord_x=0, coord_y=0), end=Point(coord_x=1, coord_y=2)) +ln2 = Line(start=Point(coord_x=1, coord_y=1), end=Point(coord_x=2, coord_y=2)) + +assert gradient(ln1) == 2 +assert gradient(ln2) == 1 + +assert intersection(ln1, ln2) == Point(coord_x=0, coord_y=0) +assert intersection(ln1, ln1) is None diff --git a/toolkit/components/uniffi-fixtures/geometry/tests/bindings/test_geometry.rb b/toolkit/components/uniffi-fixtures/geometry/tests/bindings/test_geometry.rb new file mode 100644 index 0000000000..90fdff684e --- /dev/null +++ b/toolkit/components/uniffi-fixtures/geometry/tests/bindings/test_geometry.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require 'test/unit' +require 'geometry' + +include Test::Unit::Assertions +include Geometry + +ln1 = Line.new(start: Point.new(coord_x: 0.0, coord_y: 0.0), _end: Point.new(coord_x: 1.0, coord_y: 2.0)) +ln2 = Line.new(start: Point.new(coord_x: 1.0, coord_y: 1.0), _end: Point.new(coord_x: 2.0, coord_y: 2.0)) + +assert_equal Geometry.gradient(ln1), 2 +assert_equal Geometry.gradient(ln2), 1 + +assert_equal Geometry.intersection(ln1, ln2), Point.new(coord_x: 0, coord_y: 0) +assert Geometry.intersection(ln1, ln1).nil? diff --git a/toolkit/components/uniffi-fixtures/geometry/tests/bindings/test_geometry.swift b/toolkit/components/uniffi-fixtures/geometry/tests/bindings/test_geometry.swift new file mode 100644 index 0000000000..58bd65607f --- /dev/null +++ b/toolkit/components/uniffi-fixtures/geometry/tests/bindings/test_geometry.swift @@ -0,0 +1,10 @@ +import geometry + +let ln1 = Line(start: Point(coordX: 0, coordY: 0), end: Point(coordX: 1, coordY: 2)) +let ln2 = Line(start: Point(coordX: 1, coordY: 1), end: Point(coordX: 2, coordY: 2)) + +assert(gradient(ln: ln1) == 2.0) +assert(gradient(ln: ln2) == 1.0) + +assert(intersection(ln1: ln1, ln2: ln2) == Point(coordX: 0, coordY: 0)) +assert(intersection(ln1: ln1, ln2: ln1) == nil) diff --git a/toolkit/components/uniffi-fixtures/geometry/tests/test_generated_bindings.rs b/toolkit/components/uniffi-fixtures/geometry/tests/test_generated_bindings.rs new file mode 100644 index 0000000000..4638d847c8 --- /dev/null +++ b/toolkit/components/uniffi-fixtures/geometry/tests/test_generated_bindings.rs @@ -0,0 +1,6 @@ +uniffi::build_foreign_language_testcases!( + "tests/bindings/test_geometry.py", + "tests/bindings/test_geometry.rb", + "tests/bindings/test_geometry.kts", + "tests/bindings/test_geometry.swift", +); |