1
Fork 0
mirror of https://github.com/RGBCube/ManageTTD synced 2025-07-28 09:27:45 +00:00

Chore: Start rewriting

This commit is contained in:
RGBCube 2023-03-19 13:13:34 +03:00
parent 38dfbe836e
commit 4abbc79f16
2 changed files with 63 additions and 1 deletions

4
.gitignore vendored
View file

@ -5,7 +5,9 @@
!src/
!src/config/
!src/config/mapping
!src2/
!src2/config/
!.editorconfig
!openttd.cfg

60
src2/config/mapping.v Normal file
View file

@ -0,0 +1,60 @@
module config
pub type Mapping = map[string]map[string]Map
// TODO: &Mapping
pub fn (mpng Mapping) unmap(mapped map[string]map[string]string) !map[string]map[string]string {
mut unmapped := map[string]map[string]string{}
for mapped_section, mapped_fields in mapped {
if mapped_section !in mpng {
return error('Unknown section: ${mapped_section}')
}
for mapped_field, mapped_value in mapped_fields {
if mapped_field !in mpng[mapped_section] {
return error('Unknown field: ${mapped_section}.${mapped_field}')
}
mapped_field_mapping := mpng[mapped_section][mapped_field]
unmapped[mapped_field_mapping.section()][mapped_field_mapping.field()] = mapped_field_mapping.transform(mapped_value)
}
}
return unmapped
}
pub enum ValueType {
string
integer
boolean
}
// Maps one ManageTTD -> OpenTTD config field.
[noinit]
pub struct Map {
equivalent [2]string [required]
transformer ?fn (string) string
pub:
@type ValueType = .string
documentation string [required]
}
[inline]
pub fn (m &Map) section() string {
return m.equivalent[0]
}
[inline]
pub fn (m &Map) field() string {
return m.equivalent[1]
}
pub fn (m &Map) transform(s string) string {
return if transform_fn := m.transformer {
transform_fn(s)
} else {
s
}
}