|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const { |
| 4 | + RepositoryType, |
| 5 | + Result, |
| 6 | + createRepository, |
| 7 | + createRepositoryData, |
| 8 | + createGlobusConfig, |
| 9 | +} = require("./types"); |
| 10 | +const { validateGlobusConfig, validateMetadataConfig } = require("./validation"); |
| 11 | +const globusRepo = require("./globus"); |
| 12 | +const metadataRepo = require("./metadata"); |
| 13 | +const g_lib = require("../support"); |
| 14 | + |
| 15 | +/** |
| 16 | + * Repository factory using Rust-compatible patterns |
| 17 | + * Uses switch/case for type-based polymorphism instead of inheritance |
| 18 | + */ |
| 19 | + |
| 20 | +/** |
| 21 | + * Create repository based on type (similar to Rust match expression) |
| 22 | + * Rust's match expression provides exhaustive pattern matching |
| 23 | + * JavaScript's switch is used here to emulate this pattern |
| 24 | + * @param {object} config - Repository configuration object |
| 25 | + * @param {string} config.id - Repository ID |
| 26 | + * @param {string} config.type - Repository type (from RepositoryType enum) |
| 27 | + * @param {string} config.title - Repository title |
| 28 | + * @param {string} [config.desc] - Repository description |
| 29 | + * @param {number} config.capacity - Storage capacity in bytes |
| 30 | + * @param {string[]} config.admins - Array of admin user IDs |
| 31 | + * @param {string} [config.endpoint] - Globus endpoint (required for GLOBUS type) |
| 32 | + * @param {string} [config.path] - File path (required for GLOBUS type) |
| 33 | + * @param {string} [config.pub_key] - Public key for ZeroMQ CURVE authentication (required for GLOBUS type) |
| 34 | + * @param {string} [config.address] - Network address (required for GLOBUS type) |
| 35 | + * @param {string} [config.exp_path] - Export path (optional for GLOBUS type) |
| 36 | + * @returns {{ok: boolean, error: *}|{ok: boolean, value: *}} Result object containing repository or error |
| 37 | + * @see https://doc.rust-lang.org/book/ch06-02-match.html |
| 38 | + */ |
| 39 | +const createRepositoryByType = (config) => { |
| 40 | + const missingFields = []; |
| 41 | + if (!config.id) missingFields.push("id"); |
| 42 | + if (!config.type) missingFields.push("type"); |
| 43 | + if (!config.title) missingFields.push("title"); |
| 44 | + if (!config.capacity) missingFields.push("capacity"); |
| 45 | + if (!config.admins) missingFields.push("admins"); |
| 46 | + |
| 47 | + if (missingFields.length > 0) { |
| 48 | + return Result.err({ |
| 49 | + code: g_lib.ERR_INVALID_PARAM, |
| 50 | + message: `Missing required repository fields: ${missingFields.join(", ")}`, |
| 51 | + }); |
| 52 | + } |
| 53 | + /** |
| 54 | + * Type-based creation using switch (Rust match pattern) |
| 55 | + * Each case is like a match arm in Rust, handling a specific variant |
| 56 | + * @see https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html |
| 57 | + */ |
| 58 | + switch (config.type) { |
| 59 | + case RepositoryType.GLOBUS: { |
| 60 | + const validationResult = validateGlobusConfig(config); |
| 61 | + if (!validationResult.ok) { |
| 62 | + return validationResult; |
| 63 | + } |
| 64 | + |
| 65 | + const globusConfig = createGlobusConfig({ |
| 66 | + endpoint: config.endpoint, |
| 67 | + path: config.path, |
| 68 | + pub_key: config.pub_key, |
| 69 | + address: config.address, |
| 70 | + exp_path: config.exp_path, |
| 71 | + }); |
| 72 | + |
| 73 | + const repoData = createRepositoryData({ |
| 74 | + id: config.id, |
| 75 | + type: config.type, |
| 76 | + title: config.title, |
| 77 | + desc: config.desc, |
| 78 | + capacity: config.capacity, |
| 79 | + admins: config.admins, |
| 80 | + typeSpecific: globusConfig, |
| 81 | + }); |
| 82 | + |
| 83 | + return Result.ok(createRepository(RepositoryType.GLOBUS, repoData)); |
| 84 | + } |
| 85 | + |
| 86 | + case RepositoryType.METADATA_ONLY: { |
| 87 | + const validationResult = validateMetadataConfig(config); |
| 88 | + if (!validationResult.ok) { |
| 89 | + return validationResult; |
| 90 | + } |
| 91 | + |
| 92 | + const repoData = createRepositoryData({ |
| 93 | + id: config.id, |
| 94 | + type: config.type, |
| 95 | + title: config.title, |
| 96 | + desc: config.desc, |
| 97 | + capacity: config.capacity, |
| 98 | + admins: config.admins, |
| 99 | + }); |
| 100 | + |
| 101 | + return Result.ok(createRepository(RepositoryType.METADATA_ONLY, repoData)); |
| 102 | + } |
| 103 | + |
| 104 | + default: |
| 105 | + /** |
| 106 | + * In Rust, match must be exhaustive - all cases must be handled |
| 107 | + * The default case ensures we handle unknown variants |
| 108 | + * @see https://doc.rust-lang.org/book/ch06-02-match.html#matching-with-option-t |
| 109 | + */ |
| 110 | + return Result.err({ |
| 111 | + code: g_lib.ERR_INVALID_PARAM, |
| 112 | + message: `Unknown repository type: ${config.type}`, |
| 113 | + }); |
| 114 | + } |
| 115 | +}; |
| 116 | + |
| 117 | +/** |
| 118 | + * Get repository implementation based on type |
| 119 | + * This emulates Rust's trait object dynamic dispatch |
| 120 | + * @param {string} repositoryType - Repository type from RepositoryType enum |
| 121 | + * @returns {object|null} Repository implementation object or null if not found |
| 122 | + * @see https://doc.rust-lang.org/book/ch17-02-trait-objects.html |
| 123 | + */ |
| 124 | +const getRepositoryImplementation = (repositoryType) => { |
| 125 | + switch (repositoryType) { |
| 126 | + case RepositoryType.GLOBUS: |
| 127 | + return globusRepo; |
| 128 | + case RepositoryType.METADATA_ONLY: |
| 129 | + return metadataRepo; |
| 130 | + default: |
| 131 | + return null; |
| 132 | + } |
| 133 | +}; |
| 134 | + |
| 135 | +/** |
| 136 | + * Execute operation on repository using dynamic dispatch |
| 137 | + * This pattern emulates Rust's trait method dispatch |
| 138 | + * @param {object} repository - Repository object with type and data fields |
| 139 | + * @param {string} operation - Operation name to execute |
| 140 | + * @param {...*} args - Additional arguments to pass to the operation |
| 141 | + * @returns {{ok: boolean, error: *}|*} Result of the operation |
| 142 | + * @see https://doc.rust-lang.org/book/ch17-02-trait-objects.html#trait-objects-perform-dynamic-dispatch |
| 143 | + */ |
| 144 | +const executeRepositoryOperation = (repository, operation, ...args) => { |
| 145 | + const impl = getRepositoryImplementation(repository.type); |
| 146 | + if (!impl) { |
| 147 | + return Result.err({ |
| 148 | + code: g_lib.ERR_INVALID_PARAM, |
| 149 | + message: `No implementation for repository type: ${repository.type}`, |
| 150 | + }); |
| 151 | + } |
| 152 | + |
| 153 | + if (typeof impl[operation] !== "function") { |
| 154 | + return Result.err({ |
| 155 | + code: g_lib.ERR_NOT_IMPLEMENTED, |
| 156 | + message: `Operation '${operation}' not implemented for type: ${repository.type}`, |
| 157 | + }); |
| 158 | + } |
| 159 | + |
| 160 | + return impl[operation](repository.data, ...args); |
| 161 | +}; |
| 162 | + |
| 163 | +module.exports = { |
| 164 | + createRepositoryByType, |
| 165 | + getRepositoryImplementation, |
| 166 | + executeRepositoryOperation, |
| 167 | +}; |
0 commit comments