mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
config: refactor, prepare to load keys
This commit is contained in:
parent
aefc147347
commit
cd8d85bd97
4 changed files with 87 additions and 43 deletions
|
|
@ -20,7 +20,15 @@ impl Configuration {
|
||||||
pub fn from_source (source: impl AsRef<str> + 'static) -> Usually<Self> {
|
pub fn from_source (source: impl AsRef<str> + 'static) -> Usually<Self> {
|
||||||
let source: Box<str> = source.as_ref().into();
|
let source: Box<str> = source.as_ref().into();
|
||||||
let source: &'static str = Box::leak(source);
|
let source: &'static str = Box::leak(source);
|
||||||
let iter = TokenIter::from(source.as_ref());
|
let [name, info, view, keys] = Self::parse(TokenIter::from(source.as_ref()))?;
|
||||||
|
Ok(Self {
|
||||||
|
info: info.map(Self::parse_info).flatten(),
|
||||||
|
name: name.map(Self::parse_name).flatten(),
|
||||||
|
view: Self::parse_view(view)?,
|
||||||
|
keys: Self::parse_keys(keys)?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
fn parse (iter: TokenIter) -> Usually<[Option<TokenIter>;4]> {
|
||||||
let mut name: Option<TokenIter> = None;
|
let mut name: Option<TokenIter> = None;
|
||||||
let mut info: Option<TokenIter> = None;
|
let mut info: Option<TokenIter> = None;
|
||||||
let mut view: Option<TokenIter> = None;
|
let mut view: Option<TokenIter> = None;
|
||||||
|
|
@ -35,37 +43,73 @@ impl Configuration {
|
||||||
"info" => info = Some(exp),
|
"info" => info = Some(exp),
|
||||||
"keys" => keys = Some(exp),
|
"keys" => keys = Some(exp),
|
||||||
"view" => view = Some(exp),
|
"view" => view = Some(exp),
|
||||||
_ => return Err(format!("(e3) unexpected symbol {sym:?}").into())
|
_ => return Err(
|
||||||
|
format!("(e3) unexpected symbol {sym:?}").into()
|
||||||
|
)
|
||||||
},
|
},
|
||||||
_ => return Err(format!("(e2) unexpected exp {:?}", next.map(|x|x.value)).into())
|
_ => return Err(
|
||||||
|
format!("(e2) unexpected exp {:?}", next.map(|x|x.value)).into()
|
||||||
|
)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
t => return Err(format!("(e1) unexpected token {token:?}").into())
|
t => return Err(
|
||||||
|
format!("(e1) unexpected token {token:?}").into()
|
||||||
|
)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Ok(Self {
|
Ok([name, info, view, keys])
|
||||||
info: info.and_then(|mut x|x.next()).and_then(|x|if let Value::Str(x) = x.value {
|
}
|
||||||
|
fn parse_info (mut iter: TokenIter) -> Option<Arc<str>> {
|
||||||
|
iter.next().and_then(|x|if let Value::Str(x) = x.value {
|
||||||
Some(x.into())
|
Some(x.into())
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}),
|
|
||||||
name: name.and_then(|mut x|x.next()).and_then(|x|if let Value::Str(x) = x.value {
|
|
||||||
Some(x.into())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}),
|
|
||||||
view: if let Some(view) = view {
|
|
||||||
view
|
|
||||||
} else {
|
|
||||||
return Err(format!("missing view definition").into())
|
|
||||||
},
|
|
||||||
keys: if let Some(keys) = keys {
|
|
||||||
let mut map = InputMap::default();
|
|
||||||
// TODO add layers
|
|
||||||
map
|
|
||||||
} else {
|
|
||||||
return Err(format!("missing keys definition").into())
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
fn parse_name (mut iter: TokenIter) -> Option<Arc<str>> {
|
||||||
|
iter.next().and_then(|x|if let Value::Str(x) = x.value {
|
||||||
|
Some(x.into())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
})
|
||||||
|
}
|
||||||
|
fn parse_view (iter: Option<TokenIter>) -> Usually<TokenIter> {
|
||||||
|
if let Some(view) = iter {
|
||||||
|
Ok(view)
|
||||||
|
} else {
|
||||||
|
Err(format!("missing view definition").into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn parse_keys (iter: Option<TokenIter>)
|
||||||
|
-> Usually<InputMap<'static, Tek, TekCommand, TuiIn, TokenIter<'static>>>
|
||||||
|
{
|
||||||
|
if let Some(mut keys) = iter {
|
||||||
|
let mut map = InputMap::default();
|
||||||
|
while let Some(token) = keys.next() {
|
||||||
|
match token.value {
|
||||||
|
Value::Exp(_, mut exp) => {
|
||||||
|
let next = exp.next();
|
||||||
|
match next {
|
||||||
|
Some(Token { value: Value::Key(sym), .. }) => match sym {
|
||||||
|
"layer" => { todo!() },
|
||||||
|
"layer-if" => { todo!() },
|
||||||
|
_ => return Err(
|
||||||
|
format!("(e3) unexpected symbol {sym:?}").into()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
_ => return Err(
|
||||||
|
format!("(e2) unexpected exp {:?}", next.map(|x|x.value)).into()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
t => return Err(
|
||||||
|
format!("(e1) unexpected token {token:?}").into()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(map)
|
||||||
|
} else {
|
||||||
|
return Err(format!("missing keys definition").into())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue