From f993b9d7ec23e040931dd94ee4aa3cc78377ab5a Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Mon, 20 Feb 2023 18:38:34 -0600 Subject: [PATCH] zz --- assignment-1c/Cargo.lock | 19 +++++-- assignment-1c/Cargo.toml | 4 ++ assignment-1c/csci5607-macros/Cargo.toml | 13 +++++ assignment-1c/csci5607-macros/src/lib.rs | 65 ++++++++++++++++++++++++ assignment-1c/src/scene/mod.rs | 3 +- flake.lock | 24 ++++----- flake.nix | 6 ++- 7 files changed, 115 insertions(+), 19 deletions(-) create mode 100644 assignment-1c/csci5607-macros/Cargo.toml create mode 100644 assignment-1c/csci5607-macros/src/lib.rs diff --git a/assignment-1c/Cargo.lock b/assignment-1c/Cargo.lock index 126c06c..383a2d6 100644 --- a/assignment-1c/Cargo.lock +++ b/assignment-1c/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "anyhow" -version = "1.0.68" +version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2cb2f989d18dd141ab8ae82f64d1a8cdd37e0840f73a406896cf5e99502fab61" +checksum = "224afbd727c3d6e4b90103ece64b8d1b67fbb1973b1046c2281eed3f3803f800" [[package]] name = "approx" @@ -24,6 +24,7 @@ dependencies = [ "anyhow", "base64", "clap", + "csci5607-macros", "derivative", "nalgebra", "num", @@ -150,6 +151,16 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "csci5607-macros" +version = "0.1.0" +dependencies = [ + "anyhow", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "derivative" version = "2.2.0" @@ -478,9 +489,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.50" +version = "1.0.51" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ef7d57beacfaf2d8aee5937dab7b7f28de3cb8b1828479bb5de2a7106f2bae2" +checksum = "5d727cae5b39d21da60fa540906919ad737832fe0b1c165da3a34d6548c849d6" dependencies = [ "unicode-ident", ] diff --git a/assignment-1c/Cargo.toml b/assignment-1c/Cargo.toml index 0fe39cd..455cc29 100644 --- a/assignment-1c/Cargo.toml +++ b/assignment-1c/Cargo.toml @@ -4,6 +4,9 @@ authors = ["Michael Zhang "] version = "0.1.0" edition = "2021" +[workspace] +members = ["csci5607-macros"] + # For profiling with flamegraphs [profile.release] debug = true @@ -22,6 +25,7 @@ path = "src/main.rs" anyhow = "1.0.68" base64 = "0.21.0" clap = { version = "4.1.4", features = ["cargo", "derive"] } +csci5607-macros = { path = "./csci5607-macros" } derivative = "2.2.0" nalgebra = "0.32.1" num = { version = "0.4.0", features = ["serde"] } diff --git a/assignment-1c/csci5607-macros/Cargo.toml b/assignment-1c/csci5607-macros/Cargo.toml new file mode 100644 index 000000000..fb2c1bb --- /dev/null +++ b/assignment-1c/csci5607-macros/Cargo.toml @@ -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" diff --git a/assignment-1c/csci5607-macros/src/lib.rs b/assignment-1c/csci5607-macros/src/lib.rs new file mode 100644 index 000000000..0b29639 --- /dev/null +++ b/assignment-1c/csci5607-macros/src/lib.rs @@ -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 { + 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 { + #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(), + ) +} diff --git a/assignment-1c/src/scene/mod.rs b/assignment-1c/src/scene/mod.rs index 9d70991..d982b81 100644 --- a/assignment-1c/src/scene/mod.rs +++ b/assignment-1c/src/scene/mod.rs @@ -5,12 +5,13 @@ pub mod input_file; pub mod sphere; use nalgebra::Vector3; +use csci5607_macros::Csci5607Parser; use crate::{image::Color, Point}; use self::data::{Attenuation, DepthCueing, Light, Material, Object}; -#[derive(Debug, Default)] +#[derive(Debug, Default, Csci5607Parser)] pub struct Scene { pub eye_pos: Vector3, pub view_dir: Vector3, diff --git a/flake.lock b/flake.lock index 2335fbb..6270094 100644 --- a/flake.lock +++ b/flake.lock @@ -6,11 +6,11 @@ "rust-analyzer-src": "rust-analyzer-src" }, "locked": { - "lastModified": 1674240251, - "narHash": "sha256-AVMmf/CtcGensTZmMicToDpOwySEGNKYgRPC7lu3m8w=", + "lastModified": 1676907239, + "narHash": "sha256-x/NG2pzLef1e8aXQfAwKECYYF0+lAQu+MD+Z0PtIJX4=", "owner": "nix-community", "repo": "fenix", - "rev": "d8067f4d1d3d30732703209bec5ca7d62aaececc", + "rev": "c3f4f35c2add975dd1f40a07cfbb758a049b20d2", "type": "github" }, "original": { @@ -21,11 +21,11 @@ }, "flake-utils": { "locked": { - "lastModified": 1667395993, - "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=", + "lastModified": 1676283394, + "narHash": "sha256-XX2f9c3iySLCw54rJ/CZs+ZK6IQy7GXNY4nSOyu2QG4=", "owner": "numtide", "repo": "flake-utils", - "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f", + "rev": "3db36a8b464d0c4532ba1c7dda728f4576d6d073", "type": "github" }, "original": { @@ -35,11 +35,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1673796341, - "narHash": "sha256-1kZi9OkukpNmOaPY7S5/+SlCDOuYnP3HkXHvNDyLQcc=", + "lastModified": 1676721149, + "narHash": "sha256-mN2EpTGxxVNnFZLoLWRwh6f7UWhXy4qE+wO2CZyrXps=", "owner": "nixos", "repo": "nixpkgs", - "rev": "6dccdc458512abce8d19f74195bb20fdb067df50", + "rev": "5f4e07deb7c44f27d498f8df9c5f34750acf52d2", "type": "github" }, "original": { @@ -73,11 +73,11 @@ "rust-analyzer-src": { "flake": false, "locked": { - "lastModified": 1674162026, - "narHash": "sha256-iY0bxoVE7zAZmp0BB/m5hZW5pWHUfgntDvc1m2zyt/U=", + "lastModified": 1676584162, + "narHash": "sha256-8h0sV0fmMSB7KydJJD5Iz1kJxR3YzYa3iJ71VD2zePk=", "owner": "rust-lang", "repo": "rust-analyzer", - "rev": "6e52c64031825920983515b9e975e93232739f7f", + "rev": "a6603fc21d50b3386a488c96225b2d1fd492e533", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index 8825492..f308207 100644 --- a/flake.nix +++ b/flake.nix @@ -9,12 +9,15 @@ overlays = [ fenix.overlays.default ]; }; - toolchain = pkgs.fenix.stable; + # toolchain = pkgs.fenix.stable; + toolchain = pkgs.fenix.complete; in rec { devShell = pkgs.mkShell { packages = (with pkgs; [ + cargo-deny cargo-edit + cargo-expand cargo-flamegraph cargo-watch imagemagick @@ -23,7 +26,6 @@ texlive.combined.scheme-full unzip zip - (python310.withPackages (p: with p; [ numpy ])) ]) ++ (with toolchain; [ cargo