1 /**
2 Copyright: Copyright (c) 2020, Joakim Brännström. All rights reserved.
3 License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
4 Author: Joakim Brännström (joakim.brannstrom@gmx.com)
5 */
6 module my.from_;
7 
8 /** Local imports everywhere.
9 
10 Example:
11 ---
12 std_.foo fun(std_.string.Path p)
13 ---
14 
15 Or using in a function definition:
16 ---
17 
18 string foo(from.my.module.Type x) { return null; }
19 */
20 
21 enum from = FromImpl!null();
22 enum std_ = from.std;
23 
24 private:
25 
26 template isModuleImport(string import_) {
27     enum isModuleImport = __traits(compiles, { mixin("import ", import_, ";"); });
28 }
29 
30 template isSymbolInModule(string module_, string symbol) {
31     static if (isModuleImport!module_) {
32         enum import_ = module_ ~ ":" ~ symbol;
33         enum isSymbolInModule = __traits(compiles, {
34                 mixin("import ", import_, ";");
35             });
36     } else {
37         enum isSymbolInModule = false;
38     }
39 }
40 
41 template FailedSymbol(string symbol, string module_) {
42     auto FailedSymbol(Args...)(auto ref Args args) {
43         static assert(0, "Symbol \"" ~ symbol ~ "\" not found in " ~ module_);
44     }
45 }
46 
47 struct FromImpl(string module_) {
48     template opDispatch(string symbol) {
49         static if (isSymbolInModule!(module_, symbol)) {
50             mixin("import ", module_, "; alias opDispatch = ", symbol, ";");
51         } else {
52             static if (module_.length == 0) {
53                 enum opDispatch = FromImpl!(symbol)();
54             } else {
55                 enum import_ = module_ ~ "." ~ symbol;
56                 static if (isModuleImport!import_) {
57                     enum opDispatch = FromImpl!(import_)();
58                 } else {
59                     alias opDispatch = FailedSymbol!(symbol, module_);
60                 }
61             }
62         }
63     }
64 }