Skip to content

Commit

Permalink
RequestConfig: remove reference from config and request
Browse files Browse the repository at this point in the history
  • Loading branch information
Arpita-Jaiswal committed Nov 3, 2023
1 parent 870e268 commit 69af598
Show file tree
Hide file tree
Showing 16 changed files with 82 additions and 83 deletions.
14 changes: 7 additions & 7 deletions fastn-core/src/commands/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ fn handle_redirect(
/// path: /<file-name>/
///
#[tracing::instrument(skip_all)]
async fn serve_file<'o, 'm: 'o>(
config: &'m mut fastn_core::RequestConfig<'o>,
async fn serve_file<'m>(
config: &'m mut fastn_core::RequestConfig,
path: &camino::Utf8Path,
) -> fastn_core::http::Response {
if let Some(r) = handle_redirect(config.config, path) {
if let Some(r) = handle_redirect(&config.config, path) {
return r;
}

Expand Down Expand Up @@ -123,8 +123,8 @@ async fn serve_file<'o, 'm: 'o>(
}
}

async fn serve_cr_file<'o, 'm: 'o>(
req_config: &'m mut fastn_core::RequestConfig<'o>,
async fn serve_cr_file<'m>(
req_config: &'m mut fastn_core::RequestConfig,
path: &camino::Utf8Path,
cr_number: usize,
) -> fastn_core::http::Response {
Expand Down Expand Up @@ -302,7 +302,7 @@ pub async fn serve(

// if request goes with mount-point /todos/api/add-todo/
// so it should say not found and pass it to proxy
let cookies = req_config.request.cookies();
let cookies = req_config.request.cookies().clone();

let file_response = serve_file(&mut req_config, path.as_path()).await;
// If path is not present in sitemap then pass it to proxy
Expand Down Expand Up @@ -339,7 +339,7 @@ pub async fn serve(
if let Some(user_data) = fastn_core::auth::get_user_data_from_cookies(
platform,
requested_field,
cookies,
&cookies,
)
.await?
{
Expand Down
28 changes: 14 additions & 14 deletions fastn-core/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ pub struct Config {
}

#[derive(Debug, Clone)]
pub struct RequestConfig<'a> {
pub struct RequestConfig {
pub named_parameters: Vec<(String, ftd::Value)>,
pub extra_data: std::collections::BTreeMap<String, String>,
pub downloaded_assets: std::collections::BTreeMap<String, String>,
pub current_document: Option<String>,
pub dependencies_during_render: Vec<String>,
pub request: &'a fastn_core::http::Request,
pub config: &'a Config,
pub request: fastn_core::http::Request,
pub config: Config,
/// If the current module being parsed is a markdown file, `.markdown` contains the name and
/// content of that file
pub markdown: Option<(String, String)>,
Expand All @@ -60,10 +60,10 @@ pub struct RequestConfig<'a> {
pub module_package_map: std::collections::BTreeMap<String, String>,
}

impl<'a> RequestConfig<'a> {
impl RequestConfig {
pub fn new(
config: &'a Config,
request: &'a fastn_core::http::Request,
config: &Config,
request: &fastn_core::http::Request,
document_id: &str,
base_url: &str,
) -> Self {
Expand All @@ -73,8 +73,8 @@ impl<'a> RequestConfig<'a> {
downloaded_assets: Default::default(),
current_document: None,
dependencies_during_render: vec![],
request,
config,
request: request.clone(),
config: config.clone(),
markdown: None,
document_id: document_id.to_string(),
translated_data: Default::default(),
Expand Down Expand Up @@ -234,15 +234,15 @@ impl<'a> RequestConfig<'a> {
return Ok(true);
}
let access_identities = fastn_core::user_group::access_identities(
self.config,
self.request,
&self.config,
&self.request,
&document_name,
true,
)
.await?;

let belongs_to = fastn_core::user_group::belongs_to(
self.config,
&self.config,
document_readers.as_slice(),
access_identities.iter().collect_vec().as_slice(),
)?;
Expand All @@ -267,15 +267,15 @@ impl<'a> RequestConfig<'a> {
let document_writers =
sitemap.writers(document_name.as_str(), &self.config.package.groups);
let access_identities = fastn_core::user_group::access_identities(
self.config,
self.request,
&self.config,
&self.request,
&document_name,
false,
)
.await?;

return fastn_core::user_group::belongs_to(
self.config,
&self.config,
document_writers.as_slice(),
access_identities.iter().collect_vec().as_slice(),
);
Expand Down
16 changes: 8 additions & 8 deletions fastn-core/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn cached_parse(
pub async fn interpret_helper<'a>(
name: &str,
source: &str,
lib: &'a mut fastn_core::Library2022<'_>,
lib: &'a mut fastn_core::Library2022,
base_url: &str,
download_assets: bool,
line_number: usize,
Expand Down Expand Up @@ -113,7 +113,7 @@ pub async fn interpret_helper<'a>(
}

pub async fn resolve_import<'a>(
lib: &'a mut fastn_core::Library2<'_>,
lib: &'a mut fastn_core::Library2,
state: &mut ftd::ftd2021::InterpreterState,
module: &str,
) -> ftd::ftd2021::p1::Result<String> {
Expand Down Expand Up @@ -160,7 +160,7 @@ pub async fn resolve_import<'a>(

// source, foreign_variable, foreign_function
pub async fn resolve_import_2022<'a>(
lib: &'a mut fastn_core::Library2022<'_>,
lib: &'a mut fastn_core::Library2022,
_state: &mut ftd::interpreter::InterpreterState,
module: &str,
caller_module: &str,
Expand Down Expand Up @@ -292,7 +292,7 @@ pub async fn resolve_import_2022<'a>(
pub async fn resolve_foreign_variable2022(
variable: &str,
doc_name: &str,
lib: &mut fastn_core::Library2022<'_>,
lib: &mut fastn_core::Library2022,
base_url: &str,
download_assets: bool,
caller_module: &str,
Expand Down Expand Up @@ -353,7 +353,7 @@ pub async fn resolve_foreign_variable2022(
module: &str,
package: &fastn_core::Package,
files: &str,
lib: &mut fastn_core::RequestConfig<'_>,
lib: &mut fastn_core::RequestConfig,
base_url: &str,
download_assets: bool, // true: in case of `fastn build`
) -> ftd::ftd2021::p1::Result<ftd::interpreter::Value> {
Expand Down Expand Up @@ -537,7 +537,7 @@ pub async fn resolve_foreign_variable2022(
}

async fn download(
lib: &mut fastn_core::Library2022<'_>,
lib: &mut fastn_core::Library2022,
download_assets: bool,
package: &fastn_core::Package,
path: &str,
Expand Down Expand Up @@ -585,7 +585,7 @@ pub async fn resolve_foreign_variable2(
variable: &str,
doc_name: &str,
state: &ftd::ftd2021::InterpreterState,
lib: &mut fastn_core::Library2<'_>,
lib: &mut fastn_core::Library2,
base_url: &str,
download_assets: bool,
) -> ftd::ftd2021::p1::Result<ftd::Value> {
Expand Down Expand Up @@ -620,7 +620,7 @@ pub async fn resolve_foreign_variable2(
async fn get_assets_value(
package: &fastn_core::Package,
files: &str,
lib: &mut fastn_core::Library2<'_>,
lib: &mut fastn_core::Library2,
base_url: &str,
download_assets: bool, // true: in case of `fastn build`
) -> ftd::ftd2021::p1::Result<ftd::Value> {
Expand Down
18 changes: 9 additions & 9 deletions fastn-core/src/library/fastn_dot_ftd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::utils::HasElements;

async fn i18n_data(lib: &fastn_core::Library<'_>) -> String {
async fn i18n_data(lib: &fastn_core::Library) -> String {
let lang = match lib.config.config.package.language {
Some(ref lang) => {
realm_lang::Language::from_2_letter_code(lang).unwrap_or(realm_lang::Language::English)
Expand All @@ -19,7 +19,7 @@ async fn i18n_data(lib: &fastn_core::Library<'_>) -> String {

let current_document_last_modified_on =
fastn_core::utils::get_current_document_last_modified_on(
lib.config.config,
&lib.config.config,
lib.document_id.as_str(),
)
.await;
Expand Down Expand Up @@ -243,7 +243,7 @@ fn construct_fastn_cli_variables(_lib: &fastn_core::Library) -> String {
)
}

pub(crate) async fn get2022_(lib: &fastn_core::Library<'_>) -> String {
pub(crate) async fn get2022_(lib: &fastn_core::Library) -> String {
#[allow(clippy::format_in_format_args)]
let mut fastn_base = format!(
indoc::indoc! {"
Expand All @@ -267,7 +267,7 @@ pub(crate) async fn get2022_(lib: &fastn_core::Library<'_>) -> String {
);

if let Ok(number_of_documents) = futures::executor::block_on(
fastn_core::utils::get_number_of_documents(lib.config.config),
fastn_core::utils::get_number_of_documents(&lib.config.config),
) {
fastn_base = format!(
indoc::indoc! {"
Expand Down Expand Up @@ -299,7 +299,7 @@ pub(crate) async fn get2022_(lib: &fastn_core::Library<'_>) -> String {
fastn_base
}

pub(crate) async fn get(lib: &fastn_core::Library<'_>) -> String {
pub(crate) async fn get(lib: &fastn_core::Library) -> String {
#[allow(clippy::format_in_format_args)]
let mut fastn_base = format!(
indoc::indoc! {"
Expand Down Expand Up @@ -406,7 +406,7 @@ pub(crate) async fn get(lib: &fastn_core::Library<'_>) -> String {
}

if let Ok(number_of_documents) = futures::executor::block_on(
fastn_core::utils::get_number_of_documents(lib.config.config),
fastn_core::utils::get_number_of_documents(&lib.config.config),
) {
fastn_base = format!(
indoc::indoc! {"
Expand Down Expand Up @@ -435,7 +435,7 @@ pub(crate) async fn get(lib: &fastn_core::Library<'_>) -> String {

if let Some(last_modified_on) =
futures::executor::block_on(fastn_core::utils::get_current_document_last_modified_on(
lib.config.config,
&lib.config.config,
lib.document_id.as_str(),
))
{
Expand Down Expand Up @@ -812,7 +812,7 @@ pub(crate) async fn get(lib: &fastn_core::Library<'_>) -> String {
fastn_base
}

pub(crate) async fn get2(lib: &fastn_core::Library2<'_>) -> String {
pub(crate) async fn get2(lib: &fastn_core::Library2) -> String {
let lib = fastn_core::Library {
config: lib.config.clone(),
markdown: lib.markdown.clone(),
Expand All @@ -824,7 +824,7 @@ pub(crate) async fn get2(lib: &fastn_core::Library2<'_>) -> String {
get(&lib).await
}

pub(crate) async fn get2022(lib: &fastn_core::Library2022<'_>) -> String {
pub(crate) async fn get2022(lib: &fastn_core::Library2022) -> String {
let lib = fastn_core::Library {
config: lib.clone(),
markdown: lib.markdown.clone(),
Expand Down
20 changes: 10 additions & 10 deletions fastn-core/src/library/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ pub(crate) mod toc;
pub use fastn_core::Library2022;

#[derive(Debug)]
pub struct Library<'a> {
pub config: fastn_core::RequestConfig<'a>,
pub struct Library {
pub config: fastn_core::RequestConfig,
/// If the current module being parsed is a markdown file, `.markdown` contains the name and
/// content of that file
pub markdown: Option<(String, String)>,
Expand All @@ -19,7 +19,7 @@ pub struct Library<'a> {
pub base_url: String,
}

impl Library<'_> {
impl Library {
pub async fn get_with_result(
&self,
name: &str,
Expand All @@ -46,7 +46,7 @@ impl Library<'_> {
async fn get_for_package(
name: &str,
packages: &mut Vec<fastn_core::Package>,
lib: &fastn_core::Library<'_>,
lib: &fastn_core::Library,
) -> Option<String> {
let package = packages.last()?;
if name.starts_with(package.name.as_str()) {
Expand Down Expand Up @@ -120,7 +120,7 @@ impl Library<'_> {
async fn get_data_from_package(
name: &str,
package: &fastn_core::Package,
lib: &Library<'_>,
lib: &Library,
) -> Option<String> {
let path = lib.config.config.get_root_for_package(package);
fastn_core::Config::download_required_file(&lib.config.config.root, name, package)
Expand All @@ -147,8 +147,8 @@ impl Library<'_> {
}

#[derive(Debug)]
pub struct Library2<'a> {
pub config: fastn_core::RequestConfig<'a>,
pub struct Library2 {
pub config: fastn_core::RequestConfig,
/// If the current module being parsed is a markdown file, `.markdown` contains the name and
/// content of that file
pub markdown: Option<(String, String)>,
Expand All @@ -158,7 +158,7 @@ pub struct Library2<'a> {
pub packages_under_process: Vec<String>,
}

impl Library2<'_> {
impl Library2 {
pub(crate) async fn push_package_under_process(
&mut self,
package: &fastn_core::Package,
Expand Down Expand Up @@ -236,7 +236,7 @@ impl Library2<'_> {

return get_for_package(format!("{}/", name.trim_end_matches('/')).as_str(), self).await;

async fn get_for_package(name: &str, lib: &mut fastn_core::Library2<'_>) -> Option<String> {
async fn get_for_package(name: &str, lib: &mut fastn_core::Library2) -> Option<String> {
let package = lib.get_current_package().ok()?;
if name.starts_with(package.name.as_str()) {
if let Some(r) = get_data_from_package(name, &package, lib).await {
Expand Down Expand Up @@ -291,7 +291,7 @@ impl Library2<'_> {
async fn get_data_from_package(
name: &str,
package: &fastn_core::Package,
lib: &mut Library2<'_>,
lib: &mut Library2,
) -> Option<String> {
lib.push_package_under_process(package).await.ok()?;
let packages = lib.config.config.all_packages.borrow();
Expand Down
12 changes: 7 additions & 5 deletions fastn-core/src/library2022/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ impl KeyValueData {
}
}

pub type Library2022<'m> = fastn_core::RequestConfig<'m>;
pub type Library2022 = fastn_core::RequestConfig;

impl Library2022<'_> {
impl Library2022 {
pub async fn get_with_result(
&mut self,
name: &str,
Expand Down Expand Up @@ -75,7 +75,7 @@ impl Library2022<'_> {

async fn get_for_package(
name: &str,
lib: &mut fastn_core::Library2022<'_>,
lib: &mut fastn_core::Library2022,
current_processing_module: &str,
) -> Option<(String, String, usize)> {
let package = lib.get_current_package(current_processing_module).ok()?;
Expand Down Expand Up @@ -139,7 +139,7 @@ impl Library2022<'_> {
async fn get_data_from_package(
name: &str,
package: &fastn_core::Package,
lib: &mut fastn_core::Library2022<'_>,
lib: &mut fastn_core::Library2022,
) -> Option<(String, usize)> {
lib.push_package_under_process(name, package).await.ok()?;
let packages = lib.config.all_packages.borrow();
Expand Down Expand Up @@ -267,7 +267,9 @@ impl Library2022<'_> {
.await
}
"pg" => processor::pg::process(value, kind, doc).await,
"package-tree" => processor::package_tree::process(value, kind, doc, self.config).await,
"package-tree" => {
processor::package_tree::process(value, kind, doc, &self.config).await
}
"query" => processor::query::process(value, kind, doc, self).await,
t => Err(ftd::interpreter::Error::ParseError {
doc_id: self.document_id.to_string(),
Expand Down
Loading

0 comments on commit 69af598

Please sign in to comment.