Rename Raw-rs to Rawkit (#2088)

* Rename within files

* Rename in CI

* Rename the folder and file names

* Rename raw_rs to rawkit

* Add example to README

* Add initial documentation

* Small API changes and extra documentation

* Bump versions and stuff

* Readme improvements

* Merge proc-macro crates into one

* Add README to rawkit-proc-macros

* Remove keywords and categories

* Add licenses to rawkit-proc-macros

---------

Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
Elbert Ronnie
2024-11-03 13:40:39 +05:30
committed by GitHub
parent 8d3da83606
commit 8fdecaa487
89 changed files with 664 additions and 356 deletions

View File

@@ -0,0 +1,98 @@
use proc_macro::TokenStream;
use quote::{quote, ToTokens};
use toml::{Table, Value};
use std::fs;
use std::path::Path;
enum CustomValue {
String(String),
Integer(i64),
Float(f64),
Boolean(bool),
Array(Vec<CustomValue>),
}
impl ToTokens for CustomValue {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
match self {
CustomValue::String(x) => x.to_tokens(tokens),
CustomValue::Integer(x) => {
let x: proc_macro2::TokenStream = format!("{:?}", x).parse().unwrap();
x.to_tokens(tokens)
}
CustomValue::Float(x) => {
let x: proc_macro2::TokenStream = format!("{:?}", x).parse().unwrap();
x.to_tokens(tokens)
}
CustomValue::Boolean(x) => x.to_tokens(tokens),
CustomValue::Array(x) => quote! { [ #( #x ),* ] }.to_tokens(tokens),
}
}
}
impl From<Value> for CustomValue {
fn from(value: Value) -> Self {
match value {
Value::String(x) => CustomValue::String(x),
Value::Integer(x) => CustomValue::Integer(x),
Value::Float(x) => CustomValue::Float(x),
Value::Boolean(x) => CustomValue::Boolean(x),
Value::Array(x) => CustomValue::Array(x.into_iter().map(|x| x.into()).collect()),
_ => panic!("Unsupported data type"),
}
}
}
pub fn build_camera_data() -> TokenStream {
let mut camera_data: Vec<(String, Table)> = Vec::new();
let mut path = Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()).to_path_buf();
path.push("camera_data");
fs::read_dir(path).unwrap().for_each(|entry| {
let company_name_path = entry.unwrap().path();
if !company_name_path.is_dir() {
panic!("camera_data should only contain folders of company names")
}
let company_name = company_name_path.file_name().unwrap().to_str().unwrap().to_string();
fs::read_dir(company_name_path).unwrap().for_each(|entry| {
let model_path = entry.unwrap().path();
if !model_path.is_file() || model_path.extension().unwrap() != "toml" {
panic!("The folders within camera_data should only contain toml files")
}
let name = company_name.clone() + " " + model_path.file_stem().unwrap().to_str().unwrap();
let mut values: Table = toml::from_str(&fs::read_to_string(model_path).unwrap()).unwrap();
if let Some(val) = values.get_mut("xyz_to_camera") {
*val = Value::Array(val.as_array().unwrap().iter().map(|x| Value::Integer((x.as_float().unwrap() * 10_000.) as i64)).collect());
}
camera_data.push((name, values))
});
});
let x: Vec<_> = camera_data
.iter()
.map(|(name, camera_data)| {
let keys: Vec<_> = camera_data.keys().map(|key| syn::Ident::new(key, proc_macro2::Span::call_site())).collect();
let values: Vec<CustomValue> = camera_data.values().cloned().map(|x| x.into()).collect();
quote! {
(
#name,
CameraData {
#( #keys: #values, )*
..CameraData::DEFAULT
}
)
}
})
.collect();
quote!([ #(#x),* ]).into()
}

View File

@@ -0,0 +1,16 @@
extern crate proc_macro;
mod build_camera_data;
mod tag_derive;
use proc_macro::TokenStream;
#[proc_macro_derive(Tag)]
pub fn tag_derive(input: TokenStream) -> TokenStream {
tag_derive::tag_derive(input)
}
#[proc_macro]
pub fn build_camera_data(_: TokenStream) -> TokenStream {
build_camera_data::build_camera_data()
}

View File

@@ -0,0 +1,43 @@
use proc_macro::TokenStream;
use quote::{format_ident, quote};
use syn::{Data, DeriveInput, Fields};
pub fn tag_derive(input: TokenStream) -> TokenStream {
let ast: DeriveInput = syn::parse(input).unwrap();
let name = &ast.ident;
let data_struct = if let Data::Struct(data_struct) = ast.data {
data_struct
} else {
panic!("Tag trait can only be derived for structs")
};
let named_fields = if let Fields::Named(named_fields) = data_struct.fields {
named_fields
} else {
panic!("Tag trait can only be derived for structs with named_fields")
};
let struct_idents: Vec<_> = named_fields.named.iter().map(|field| field.ident.clone().unwrap()).collect();
let struct_types: Vec<_> = named_fields.named.iter().map(|field| field.ty.clone()).collect();
let new_name = format_ident!("_{}", name);
let gen = quote! {
struct #new_name {
#( #struct_idents: <#struct_types as Tag>::Output ),*
}
impl Tag for #name {
type Output = #new_name;
fn get<R: Read + Seek>(ifd: &Ifd, file: &mut TiffRead<R>) -> Result<Self::Output, TiffError> {
#( let #struct_idents = <#struct_types as Tag>::get(ifd, file)?; )*
Ok(#new_name { #( #struct_idents ),* })
}
}
};
gen.into()
}