Skip to content

Commit

Permalink
Implement methods on kittycad.rs types
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchalmers committed Oct 23, 2023
1 parent ae8eb02 commit f97cb87
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
.vscode
.idea
51 changes: 37 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions kittycad/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ pub mod hidden;
/// FROM: <https://docs.kittycad.io/api/meta>
#[cfg(feature = "requests")]
pub mod meta;
#[cfg(feature = "requests")]
mod methods;
/// Modeling API for updating your 3D files using the KittyCAD engine.
///
/// FROM: <https://docs.kittycad.io/api/modeling>
Expand Down
150 changes: 150 additions & 0 deletions kittycad/src/methods.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
use crate::types::{Angle, Point2D, Point3D, UnitAngle};
use std::ops::{Add, Div, Mul, Sub};

impl Angle {
/// Make a new angle in degrees
pub fn from_degrees(size: f64) -> Self {
Self {
unit: UnitAngle::Degrees,
value: size,
}
}
/// Make a new angle in radians
pub fn from_radians(size: f64) -> Self {
Self {
unit: UnitAngle::Radians,
value: size,
}
}

/// Get the size of the angle, in radians
pub fn radians(&self) -> f64 {
match self.unit {
UnitAngle::Radians => self.value,
UnitAngle::Degrees => self.value.to_radians(),
}
}

/// Get the size of the angle, in degrees
pub fn degrees(&self) -> f64 {
match self.unit {
UnitAngle::Degrees => self.value,
UnitAngle::Radians => self.value.to_degrees(),
}
}
}

impl Add for Point2D {
type Output = Self;

fn add(self, rhs: Self) -> Self::Output {
Self {
x: self.x + rhs.x,
y: self.y + rhs.y,
}
}
}

impl Sub for Point2D {
type Output = Self;

fn sub(self, rhs: Self) -> Self::Output {
Self {
x: self.x - rhs.x,
y: self.y - rhs.y,
}
}
}

impl Mul for Point2D {
type Output = Self;

fn mul(self, rhs: Self) -> Self::Output {
Self {
x: self.x * rhs.x,
y: self.y * rhs.y,
}
}
}

impl Div for Point2D {
type Output = Self;

fn div(self, rhs: Self) -> Self::Output {
Self {
x: self.x / rhs.x,
y: self.y / rhs.y,
}
}
}

impl Add for Point3D {
type Output = Self;

fn add(self, rhs: Self) -> Self::Output {
Self {
x: self.x + rhs.x,
y: self.y + rhs.y,
z: self.z + rhs.z,
}
}
}

impl Sub for Point3D {
type Output = Self;

fn sub(self, rhs: Self) -> Self::Output {
Self {
x: self.x - rhs.x,
y: self.y - rhs.y,
z: self.z - rhs.z,
}
}
}

impl Mul for Point3D {
type Output = Self;

fn mul(self, rhs: Self) -> Self::Output {
Self {
x: self.x * rhs.x,
y: self.y * rhs.y,
z: self.z * rhs.z,
}
}
}

impl Div for Point3D {
type Output = Self;

fn div(self, rhs: Self) -> Self::Output {
Self {
x: self.x / rhs.x,
y: self.y / rhs.y,
z: self.z / rhs.z,
}
}
}

impl Point3D {
/// Add a Z component to a 2D point.
pub fn with_z(Point2D { x, y }: Point2D, z: f64) -> Self {
Self { x, y, z }
}

pub const ZERO: Self = Self {
x: 0.0,
y: 0.0,
z: 0.0,
};
}

impl Point2D {
pub const ZERO: Self = Self { x: 0.0, y: 0.0 };
}

impl From<Point3D> for Point2D {
fn from(Point3D { x, y, .. }: Point3D) -> Self {
Point2D { x, y }
}
}
6 changes: 3 additions & 3 deletions openapitor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ pub fn generate(spec: &openapiv3::OpenAPI, opts: &Opts) -> Result<()> {
.to_string();

let persistent_modules = persistent_modules();
if persistent_modules.contains(&file_name) {
if persistent_modules.contains(&file_name.as_str()) {
continue;
}

Expand Down Expand Up @@ -536,8 +536,8 @@ impl Default for Opts {

/// Return a list of the persistent modules.
/// These are modules we do not nuke at generation time.
fn persistent_modules() -> Vec<String> {
vec!["tests".to_string()]
fn persistent_modules() -> Vec<&'static str> {
vec!["tests", "methods"]
}

fn generate_cargo_toml(opts: &Opts) -> String {
Expand Down

0 comments on commit f97cb87

Please sign in to comment.