Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move generated impls to dedicated module #19

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/lox-space.iml

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

7 changes: 2 additions & 5 deletions crates/lox_core/src/bodies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ use std::f64::consts::PI;

use crate::time::constants::f64::{SECONDS_PER_DAY, SECONDS_PER_JULIAN_CENTURY};

pub mod barycenters;
pub mod minor;
pub mod planets;
pub mod satellites;
pub mod sun;
mod generated;
pub use generated::*;

/// NaifId is implemented for all bodies.
pub trait NaifId: Copy {
Expand Down
18 changes: 18 additions & 0 deletions crates/lox_core/src/bodies/generated.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) 2023. Helge Eichhorn and the LOX contributors
*
* 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/.
*/

//! generated holds trait implementations that have been generated by `lox_gen`, separating
//! generated code from manual implementations.
//!
//! Artifacts in its submodules are intended to be transparently re-exported.

pub mod barycenters;
pub mod minor;
pub mod planets;
pub mod satellites;
pub mod sun;
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

// Auto-generated by `lox_gen`. Do not edit!

use super::{
use crate::bodies::{
EarthBarycenter, JupiterBarycenter, MarsBarycenter, MercuryBarycenter, NeptuneBarycenter,
PlutoBarycenter, PointMass, SaturnBarycenter, UranusBarycenter, VenusBarycenter,
};
Expand Down Expand Up @@ -60,7 +60,7 @@ impl PointMass for PlutoBarycenter {
#[cfg(test)]
#[allow(clippy::approx_constant)]
mod tests {
use super::*;
use crate::bodies::*;
#[test]
fn test_point_mass_1() {
assert_eq!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

// Auto-generated by `lox_gen`. Do not edit!

use super::{
use crate::bodies::{
Bennu, Braille, Ceres, Dactyl, Davida, Ellipsoid, Eros, Gaspra, Ida, Itokawa, Kleopatra,
Lutetia, Mathilde, NutationPrecessionCoefficients, Pallas, PointMass, PolynomialCoefficients,
Psyche, RotationalElements, Steins, Toutatis, TriAxial, Vesta, WilsonHarrington,
Expand Down Expand Up @@ -448,7 +448,7 @@ impl RotationalElements for Bennu {
#[cfg(test)]
#[allow(clippy::approx_constant)]
mod tests {
use super::*;
use crate::bodies::*;
#[test]
fn test_tri_axial_9511010() {
assert_eq!(Gaspra::polar_radius(), 4.4f64);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

// Auto-generated by `lox_gen`. Do not edit!

use super::{
use crate::bodies::{
Earth, Ellipsoid, Jupiter, Mars, Mercury, Neptune, NutationPrecessionCoefficients, Pluto,
PointMass, PolynomialCoefficients, RotationalElements, Saturn, Spheroid, Uranus, Venus,
};
Expand Down Expand Up @@ -746,7 +746,7 @@ impl RotationalElements for Pluto {
#[cfg(test)]
#[allow(clippy::approx_constant)]
mod tests {
use super::*;
use crate::bodies::*;
#[test]
fn test_point_mass_199() {
assert_eq!(Mercury::gravitational_parameter(), 22031.868551400003f64);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

// Auto-generated by `lox_gen`. Do not edit!

use super::{
use crate::bodies::{
Adrastea, Aegaeon, Aegir, Aitne, Albiorix, Amalthea, Ananke, Anthe, Aoede, Arche, Ariel, Atlas,
Autonoe, Bebhionn, Belinda, Bergelmir, Bestla, Bianca, Caliban, Callirrhoe, Callisto, Calypso,
Carme, Carpo, Chaldene, Charon, Cordelia, Cressida, Cupid, Cyllene, Daphnis, Deimos, Desdemona,
Expand Down Expand Up @@ -4856,7 +4856,7 @@ impl RotationalElements for Styx {
#[cfg(test)]
#[allow(clippy::approx_constant)]
mod tests {
use super::*;
use crate::bodies::*;
#[test]
fn test_point_mass_301() {
assert_eq!(Moon::gravitational_parameter(), 4902.80011845755f64);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

// Auto-generated by `lox_gen`. Do not edit!

use super::{
use crate::bodies::{
Ellipsoid, NutationPrecessionCoefficients, PointMass, PolynomialCoefficients,
RotationalElements, Spheroid, Sun,
};
Expand Down Expand Up @@ -48,7 +48,7 @@ impl RotationalElements for Sun {
#[cfg(test)]
#[allow(clippy::approx_constant)]
mod tests {
use super::*;
use crate::bodies::*;
#[test]
fn test_point_mass_10() {
assert_eq!(Sun::gravitational_parameter(), 132712440041.27942f64);
Expand Down
7 changes: 7 additions & 0 deletions tools/lox_gen/Cargo.lock

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

1 change: 1 addition & 0 deletions tools/lox_gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ quote = "1.0"
lox_io = { path = "../../crates/lox_io" }
lox_core = { path = "../../crates/lox_core" }
thiserror = "1.0.50"
lazy_static = "1.4.0"
20 changes: 14 additions & 6 deletions tools/lox_gen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

use lazy_static::lazy_static;
use std::collections::HashSet;
use std::fs;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::process::Command;

use proc_macro2::{Ident, TokenStream};
Expand Down Expand Up @@ -80,14 +81,21 @@ const COPYRIGHT_NOTICE: &str = "/*

const AUTO_GENERATION_NOTICE: &str = "// Auto-generated by `lox_gen`. Do not edit!\n\n";

lazy_static! {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lazy_static simplifies once-only runtime evaluation of non-const expressions which can't otherwise be declared static.

static ref TARGET_DIR: PathBuf = {
let parent = Path::new(file!()).parent().unwrap();
parent.join(Path::new("../../../crates/lox_core/src/bodies/generated/"))
};
}

fn write_file(file: &str, bodies: &[Body], generators: &[Generator], data: &Data) {
let path = format!("../../../crates/lox_core/src/bodies/{file}.rs");
let mut code = String::from(COPYRIGHT_NOTICE);
code.push_str(AUTO_GENERATION_NOTICE);
code.push_str(&generate_code(bodies, generators, data));
let file = Path::new(file!());
let out = file.parent().unwrap().join(path);

let out = TARGET_DIR.join(format!("{}.rs", file));
fs::write(&out, code).expect("file should be writeable");

Command::new("rustfmt")
.args([out.to_str().unwrap()])
.status()
Expand All @@ -108,14 +116,14 @@ fn generate_code(bodies: &[Body], generators: &[Generator], data: &Data) -> Stri

let imports_iter = imports.iter();
let module = quote! {
use super::{#(#imports_iter),*};
use crate::bodies::{#(#imports_iter),*};

#code

#[cfg(test)]
#[allow(clippy::approx_constant)] // at least one parsed constant is close to TAU
mod tests {
use super::*;
use crate::bodies::*;

#tests
}
Expand Down