75 lines
3.6 KiB
Python
75 lines
3.6 KiB
Python
"""启动选择一个具体实现;对照使用相同配置、文本查询和 checked sum。"""
|
|
from settings_reference import config as settings_config, source as settings_source
|
|
|
|
|
|
def config():
|
|
return {'adapter': {'main.main': {'use': 'contains', 'setting': {
|
|
'corpus': settings_config()['module']['main']['main']['corpus']}}}}
|
|
|
|
|
|
def project(root, app):
|
|
domain = root / 'module/main/main'
|
|
(domain / 'adapter').mkdir(parents=True)
|
|
(domain / 'app').mkdir()
|
|
(domain / 'app/main.dever').write_text(app)
|
|
(domain / 'api.dever').write_text('cmd bench = app.measure\n')
|
|
(domain / 'port.dever').write_text('includes(value: Text) (out: Bool)\n')
|
|
for name, operation in [('contains', 'contains'), ('prefix', 'startsWith')]:
|
|
(domain / f'adapter/{name}.dever').write_text(
|
|
'setting { corpus: Text }\n'
|
|
f'port.includes(value: Text) (out: Bool) {{ out = text.{operation}(setting.corpus, value) }}\n')
|
|
return root
|
|
|
|
|
|
def source(language):
|
|
original = settings_source(language)
|
|
if language == 'dever':
|
|
return original.replace('setting corpus: Text\n', '').replace(
|
|
'text.contains(corpus, int.toText(value % 1000))',
|
|
'port.includes(int.toText(value % 1000))').replace('cmd bench = app.measure\n', '')
|
|
if language == 'rust':
|
|
return r'''use serde::Deserialize;
|
|
#[derive(Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
struct Root { adapter: Adapters }
|
|
#[derive(Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
struct Adapters { #[serde(rename="main.main")] selected: DecodeAdapter }
|
|
#[derive(Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
struct Settings { corpus: String }
|
|
#[derive(Deserialize)]
|
|
#[serde(tag="use",content="setting",deny_unknown_fields)]
|
|
enum DecodeAdapter { #[serde(rename="contains")] Contains(Settings), #[serde(rename="prefix")] Prefix(Settings) }
|
|
enum Adapter { Contains(Settings), Prefix(Settings) }
|
|
impl Adapter {
|
|
fn includes(&self, value: &str) -> bool {
|
|
match self { Self::Contains(s) => s.corpus.contains(value), Self::Prefix(s) => s.corpus.starts_with(value) }
|
|
}
|
|
}
|
|
fn measure(adapter: &Adapter, n: i64) -> i64 {
|
|
let mut total = 0i64;
|
|
for value in 0..n {
|
|
let text = (value % 1000).to_string();
|
|
let selected = if adapter.includes(&text) { value } else { 0 };
|
|
total = total.checked_add(selected).unwrap();
|
|
}
|
|
total
|
|
}
|
|
fn main() {
|
|
let path = std::env::args().nth(2).unwrap();
|
|
let input = std::fs::read_to_string(path).unwrap();
|
|
let root: Root = serde_json::from_str(&input).unwrap();
|
|
let adapter = match root.adapter.selected { DecodeAdapter::Contains(s) => Adapter::Contains(s), DecodeAdapter::Prefix(s) => Adapter::Prefix(s) };
|
|
let n: i64 = std::env::args().nth(1).unwrap().parse().unwrap();
|
|
println!("{}", measure(&adapter, n));
|
|
}
|
|
'''
|
|
original = original.replace('type Domain struct { Main Settings }\n', '').replace('type Module struct { Main Domain }\n', '')
|
|
return original.replace('type Root struct { Module Module }',
|
|
'type Adapter struct { Use string; Setting Settings }\ntype Root struct { Adapter map[string]Adapter }').replace(
|
|
'n, err := strconv.ParseInt',
|
|
'adapter := root.Adapter["main.main"]\n if adapter.Use != "contains" && adapter.Use != "prefix" { panic("unknown implementation") }\n n, err := strconv.ParseInt').replace(
|
|
'if strings.Contains(root.Module.Main.Main.Corpus, text) { selected = value }',
|
|
'found := false\n switch adapter.Use { case "contains": found = strings.Contains(adapter.Setting.Corpus, text); case "prefix": found = strings.HasPrefix(adapter.Setting.Corpus, text) }\n if found { selected = value }')
|