#![cfg_attr(all(coverage_nightly, test), feature(coverage_attribute))]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![deny(missing_docs)]
#![deny(unreachable_pub)]
#![deny(clippy::mod_module_files)]
#![deny(clippy::undocumented_unsafe_blocks)]
#![deny(clippy::multiple_unsafe_ops_per_block)]
mod cedar_action;
mod cedar_namespace;
mod codegen;
mod error;
mod module;
mod types;
mod utils;
use cedar_policy_core::extensions::Extensions;
use cedar_policy_core::validator::RawName;
use cedar_policy_core::validator::cedar_schema::SchemaWarning;
use cedar_policy_core::validator::json_schema::Fragment;
pub use error::{CodegenError, CodegenResult};
use crate::utils::process_fragment;
pub struct Config {
pub(crate) crate_path: syn::Path,
}
impl Default for Config {
fn default() -> Self {
Self {
crate_path: syn::parse_quote!(::scuffle_cedar_policy),
}
}
}
pub struct CodegenOutput {
file: syn::File,
warnings: Vec<SchemaWarning>,
}
impl CodegenOutput {
pub fn warnings(&self) -> &[SchemaWarning] {
&self.warnings
}
}
impl quote::ToTokens for CodegenOutput {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
self.file.to_tokens(tokens);
}
}
impl std::fmt::Display for CodegenOutput {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
prettyplease::unparse(&self.file).fmt(f)
}
}
impl Config {
pub fn new() -> Self {
Self::default()
}
pub fn crate_path(&mut self, path: syn::Path) -> &mut Self {
self.crate_path = path;
self
}
pub fn generate_from_schema(&self, schema: &str) -> CodegenResult<CodegenOutput> {
let (fragment, warnings) = Fragment::from_cedarschema_str(schema, Extensions::all_available())?;
self.generate_from_fragment(&fragment).map(|mut out| {
out.warnings.extend(warnings);
out
})
}
pub fn generate_from_json(&self, schema_json: &str) -> CodegenResult<CodegenOutput> {
let fragment = Fragment::from_json_str(schema_json)?;
self.generate_from_fragment(&fragment)
}
pub fn generate_from_fragment(&self, fragment: &Fragment<RawName>) -> CodegenResult<CodegenOutput> {
let file = process_fragment(fragment, self)?;
Ok(CodegenOutput {
file,
warnings: Vec::new(),
})
}
}