|
| 1 | +//! Read and parse the files of one collection into its retained native form. |
| 2 | +
|
| 3 | +use crate::declaration_files::_helpers::listing::collections::{ |
| 4 | + NamedKind, declaration_files, file_name, function_files, named_declaration_files, read_files, |
| 5 | + seed_files, |
| 6 | +}; |
| 7 | +use crate::declaration_files::_helpers::parsing::audit_files::parse_audit_file; |
| 8 | +use crate::declaration_files::_helpers::parsing::constant_files::parse_constant_file; |
| 9 | +use crate::declaration_files::_helpers::parsing::enum_files::parse_enum_file; |
| 10 | +use crate::declaration_files::_helpers::parsing::function_files::parse_function_file; |
| 11 | +use crate::declaration_files::_helpers::parsing::hook_files::parse_hook_file; |
| 12 | +use crate::declaration_files::_helpers::parsing::schema_files::parse_schema_file; |
| 13 | +use crate::declaration_files::models::{ |
| 14 | + CollectionFailure, CollectionKind, CollectionRequest, DeclarationCollection, |
| 15 | + DeclarationFileOptions, DiscoverySession, MacroFile, ScopedFile, SeedFile, |
| 16 | +}; |
| 17 | +use crate::declarations::models::DeclarationKind; |
| 18 | +use crate::models::{DiscoveredFile, FileOutcome, ProjectRoot, StageFailure}; |
| 19 | +use crate::tree::models::ProjectTree; |
| 20 | + |
| 21 | +/// The project a collection is read from and the options its parsers use. |
| 22 | +pub(crate) struct CollectionSource<'a> { |
| 23 | + pub(crate) session: &'a DiscoverySession, |
| 24 | + pub(crate) tree: &'a ProjectTree, |
| 25 | +} |
| 26 | + |
| 27 | +/// Read and parse the collection `request` names. |
| 28 | +pub(crate) fn read_collection( |
| 29 | + source: &CollectionSource<'_>, |
| 30 | + request: CollectionRequest, |
| 31 | +) -> DeclarationCollection { |
| 32 | + let tree: &ProjectTree = source.tree; |
| 33 | + let root: &ProjectRoot = &source.session.root; |
| 34 | + let options: &DeclarationFileOptions = &source.session.options; |
| 35 | + let isolate_kind: bool = request.isolate_kind; |
| 36 | + match request.kind { |
| 37 | + CollectionKind::Enums => DeclarationCollection::Enums( |
| 38 | + declaration_files(source, DeclarationKind::Enum, isolate_kind).map(|files| { |
| 39 | + read_files(root, tree, files, |file_path, _, contents| { |
| 40 | + parse_enum_file(file_path, contents) |
| 41 | + }) |
| 42 | + }), |
| 43 | + ), |
| 44 | + CollectionKind::Constants => DeclarationCollection::Constants( |
| 45 | + declaration_files(source, DeclarationKind::Constant, isolate_kind).map(|files| { |
| 46 | + read_files(root, tree, files, |file_path, _, contents| { |
| 47 | + parse_constant_file(file_path, contents) |
| 48 | + }) |
| 49 | + }), |
| 50 | + ), |
| 51 | + CollectionKind::Macros => DeclarationCollection::Macros( |
| 52 | + declaration_files(source, DeclarationKind::Macro, isolate_kind).map(|files| { |
| 53 | + read_files(root, tree, files, |_, _, contents| { |
| 54 | + Ok(MacroFile { contents }) |
| 55 | + }) |
| 56 | + }), |
| 57 | + ), |
| 58 | + CollectionKind::ModelSchemas => DeclarationCollection::ModelSchemas( |
| 59 | + named_declaration_files(source, &[NamedKind::Schema], false).map(|files| { |
| 60 | + read_files(root, tree, files, |file_path, _, contents| { |
| 61 | + parse_schema_file(file_path, contents) |
| 62 | + }) |
| 63 | + }), |
| 64 | + ), |
| 65 | + CollectionKind::Audits => DeclarationCollection::Audits( |
| 66 | + named_declaration_files(source, &[NamedKind::Audit, NamedKind::SingularAudit], false) |
| 67 | + .map(|files| { |
| 68 | + read_files(root, tree, files, |file_path, _, contents| { |
| 69 | + parse_audit_file(file_path, contents, options) |
| 70 | + }) |
| 71 | + }), |
| 72 | + ), |
| 73 | + CollectionKind::SqlHooks => DeclarationCollection::SqlHooks( |
| 74 | + named_declaration_files(source, &[NamedKind::SqlHook], true).map(|files| { |
| 75 | + read_files(root, tree, files, |file_path, relative_path, contents| { |
| 76 | + parse_hook_file(file_path, stem(file_name(relative_path)), contents, options) |
| 77 | + }) |
| 78 | + }), |
| 79 | + ), |
| 80 | + CollectionKind::SqlFunctions => { |
| 81 | + DeclarationCollection::SqlFunctions(function_files(tree).map(|files| { |
| 82 | + read_files(root, tree, files, |file_path, _, contents| { |
| 83 | + parse_function_file(file_path, contents, options) |
| 84 | + }) |
| 85 | + })) |
| 86 | + } |
| 87 | + CollectionKind::Seeds => DeclarationCollection::Seeds( |
| 88 | + seed_files(tree).map(|paths| paths.into_iter().map(seed_file).collect()), |
| 89 | + ), |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +fn seed_file(relative_path: String) -> ScopedFile<SeedFile> { |
| 94 | + ScopedFile { |
| 95 | + file: DiscoveredFile { |
| 96 | + relative_path, |
| 97 | + outcome: FileOutcome::Parsed(SeedFile), |
| 98 | + }, |
| 99 | + scope: None, |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +/// The collection `kind` failing as a whole. |
| 104 | +pub(crate) fn failed_collection( |
| 105 | + kind: CollectionKind, |
| 106 | + failure: StageFailure, |
| 107 | +) -> DeclarationCollection { |
| 108 | + let failure = CollectionFailure::Stage(failure); |
| 109 | + match kind { |
| 110 | + CollectionKind::Enums => DeclarationCollection::Enums(Err(failure)), |
| 111 | + CollectionKind::Constants => DeclarationCollection::Constants(Err(failure)), |
| 112 | + CollectionKind::ModelSchemas => DeclarationCollection::ModelSchemas(Err(failure)), |
| 113 | + CollectionKind::SqlFunctions => DeclarationCollection::SqlFunctions(Err(failure)), |
| 114 | + CollectionKind::SqlHooks => DeclarationCollection::SqlHooks(Err(failure)), |
| 115 | + CollectionKind::Audits => DeclarationCollection::Audits(Err(failure)), |
| 116 | + CollectionKind::Seeds => DeclarationCollection::Seeds(Err(failure)), |
| 117 | + CollectionKind::Macros => DeclarationCollection::Macros(Err(failure)), |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +/// Python's `Path.stem` of a file name. |
| 122 | +fn stem(file_name: &str) -> &str { |
| 123 | + match file_name.rfind('.') { |
| 124 | + Some(index) if index > 0 && index + 1 < file_name.len() => &file_name[..index], |
| 125 | + _ => file_name, |
| 126 | + } |
| 127 | +} |
0 commit comments