This commit is contained in:
Michael Zhang 2023-02-20 18:38:34 -06:00
parent 800eb68134
commit f993b9d7ec
7 changed files with 115 additions and 19 deletions

View file

@ -4,9 +4,9 @@ version = 3
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.68" version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2cb2f989d18dd141ab8ae82f64d1a8cdd37e0840f73a406896cf5e99502fab61" checksum = "224afbd727c3d6e4b90103ece64b8d1b67fbb1973b1046c2281eed3f3803f800"
[[package]] [[package]]
name = "approx" name = "approx"
@ -24,6 +24,7 @@ dependencies = [
"anyhow", "anyhow",
"base64", "base64",
"clap", "clap",
"csci5607-macros",
"derivative", "derivative",
"nalgebra", "nalgebra",
"num", "num",
@ -150,6 +151,16 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "csci5607-macros"
version = "0.1.0"
dependencies = [
"anyhow",
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "derivative" name = "derivative"
version = "2.2.0" version = "2.2.0"
@ -478,9 +489,9 @@ dependencies = [
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.50" version = "1.0.51"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ef7d57beacfaf2d8aee5937dab7b7f28de3cb8b1828479bb5de2a7106f2bae2" checksum = "5d727cae5b39d21da60fa540906919ad737832fe0b1c165da3a34d6548c849d6"
dependencies = [ dependencies = [
"unicode-ident", "unicode-ident",
] ]

View file

@ -4,6 +4,9 @@ authors = ["Michael Zhang <zhan4854@umn.edu>"]
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
[workspace]
members = ["csci5607-macros"]
# For profiling with flamegraphs # For profiling with flamegraphs
[profile.release] [profile.release]
debug = true debug = true
@ -22,6 +25,7 @@ path = "src/main.rs"
anyhow = "1.0.68" anyhow = "1.0.68"
base64 = "0.21.0" base64 = "0.21.0"
clap = { version = "4.1.4", features = ["cargo", "derive"] } clap = { version = "4.1.4", features = ["cargo", "derive"] }
csci5607-macros = { path = "./csci5607-macros" }
derivative = "2.2.0" derivative = "2.2.0"
nalgebra = "0.32.1" nalgebra = "0.32.1"
num = { version = "0.4.0", features = ["serde"] } num = { version = "0.4.0", features = ["serde"] }

View file

@ -0,0 +1,13 @@
[package]
name = "csci5607-macros"
version = "0.1.0"
edition = "2021"
[lib]
proc-macro = true
[dependencies]
anyhow = "1.0.69"
proc-macro2 = "1.0.51"
quote = "1.0.23"
syn = "1.0.107"

View file

@ -0,0 +1,65 @@
#[macro_use]
extern crate anyhow;
use anyhow::Result;
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use syn::{Data, DataStruct, DeriveInput, Field};
#[proc_macro_derive(Csci5607Parser)]
pub fn derive_answer_fn(tokens: TokenStream) -> TokenStream {
let tokens: TokenStream2 = tokens.into();
match derive_answer_fn_impl(tokens) {
Ok(v) => v.into(),
Err(e) => {
eprintln!("Error: {e}");
panic!("{e:?}");
}
}
}
fn derive_answer_fn_impl(tokens: TokenStream2) -> Result<TokenStream2> {
let derive_input: DeriveInput = syn::parse2(tokens)?;
// Only defined on structs
let struct_input: DataStruct = match derive_input.data {
Data::Struct(v) => v,
_ => bail!("Only defined on structs."),
};
let struct_ident = derive_input.ident;
// Gather struct fields
let mut initializers = TokenStream2::default();
let mut final_constructor = TokenStream2::default();
for field in struct_input.fields {
let field_ident = field.ident;
initializers.extend(quote! { let #field_ident = todo!(); });
final_constructor.extend(quote! { #field_ident, });
// Check if this field is a vec
}
Ok(
quote! {
impl std::str::FromStr for #struct_ident {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
#initializers
for line in contents.lines() {
let mut parts = line.split_whitespace();
let keyword = match parts.next() {
Some(v) => v,
None => continue,
};
}
Ok(#struct_ident { #final_constructor })
}
}
}
.into(),
)
}

View file

@ -5,12 +5,13 @@ pub mod input_file;
pub mod sphere; pub mod sphere;
use nalgebra::Vector3; use nalgebra::Vector3;
use csci5607_macros::Csci5607Parser;
use crate::{image::Color, Point}; use crate::{image::Color, Point};
use self::data::{Attenuation, DepthCueing, Light, Material, Object}; use self::data::{Attenuation, DepthCueing, Light, Material, Object};
#[derive(Debug, Default)] #[derive(Debug, Default, Csci5607Parser)]
pub struct Scene { pub struct Scene {
pub eye_pos: Vector3<f64>, pub eye_pos: Vector3<f64>,
pub view_dir: Vector3<f64>, pub view_dir: Vector3<f64>,

View file

@ -6,11 +6,11 @@
"rust-analyzer-src": "rust-analyzer-src" "rust-analyzer-src": "rust-analyzer-src"
}, },
"locked": { "locked": {
"lastModified": 1674240251, "lastModified": 1676907239,
"narHash": "sha256-AVMmf/CtcGensTZmMicToDpOwySEGNKYgRPC7lu3m8w=", "narHash": "sha256-x/NG2pzLef1e8aXQfAwKECYYF0+lAQu+MD+Z0PtIJX4=",
"owner": "nix-community", "owner": "nix-community",
"repo": "fenix", "repo": "fenix",
"rev": "d8067f4d1d3d30732703209bec5ca7d62aaececc", "rev": "c3f4f35c2add975dd1f40a07cfbb758a049b20d2",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -21,11 +21,11 @@
}, },
"flake-utils": { "flake-utils": {
"locked": { "locked": {
"lastModified": 1667395993, "lastModified": 1676283394,
"narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=", "narHash": "sha256-XX2f9c3iySLCw54rJ/CZs+ZK6IQy7GXNY4nSOyu2QG4=",
"owner": "numtide", "owner": "numtide",
"repo": "flake-utils", "repo": "flake-utils",
"rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f", "rev": "3db36a8b464d0c4532ba1c7dda728f4576d6d073",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -35,11 +35,11 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1673796341, "lastModified": 1676721149,
"narHash": "sha256-1kZi9OkukpNmOaPY7S5/+SlCDOuYnP3HkXHvNDyLQcc=", "narHash": "sha256-mN2EpTGxxVNnFZLoLWRwh6f7UWhXy4qE+wO2CZyrXps=",
"owner": "nixos", "owner": "nixos",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "6dccdc458512abce8d19f74195bb20fdb067df50", "rev": "5f4e07deb7c44f27d498f8df9c5f34750acf52d2",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -73,11 +73,11 @@
"rust-analyzer-src": { "rust-analyzer-src": {
"flake": false, "flake": false,
"locked": { "locked": {
"lastModified": 1674162026, "lastModified": 1676584162,
"narHash": "sha256-iY0bxoVE7zAZmp0BB/m5hZW5pWHUfgntDvc1m2zyt/U=", "narHash": "sha256-8h0sV0fmMSB7KydJJD5Iz1kJxR3YzYa3iJ71VD2zePk=",
"owner": "rust-lang", "owner": "rust-lang",
"repo": "rust-analyzer", "repo": "rust-analyzer",
"rev": "6e52c64031825920983515b9e975e93232739f7f", "rev": "a6603fc21d50b3386a488c96225b2d1fd492e533",
"type": "github" "type": "github"
}, },
"original": { "original": {

View file

@ -9,12 +9,15 @@
overlays = [ fenix.overlays.default ]; overlays = [ fenix.overlays.default ];
}; };
toolchain = pkgs.fenix.stable; # toolchain = pkgs.fenix.stable;
toolchain = pkgs.fenix.complete;
in rec { in rec {
devShell = pkgs.mkShell { devShell = pkgs.mkShell {
packages = (with pkgs; [ packages = (with pkgs; [
cargo-deny cargo-deny
cargo-edit cargo-edit
cargo-expand
cargo-flamegraph cargo-flamegraph
cargo-watch cargo-watch
imagemagick imagemagick
@ -23,7 +26,6 @@
texlive.combined.scheme-full texlive.combined.scheme-full
unzip unzip
zip zip
(python310.withPackages (p: with p; [ numpy ])) (python310.withPackages (p: with p; [ numpy ]))
]) ++ (with toolchain; [ ]) ++ (with toolchain; [
cargo cargo