Skip to content

Commit d14b2da

Browse files
committed
Ask Veykril
1 parent c6ed2e2 commit d14b2da

File tree

6 files changed

+148
-0
lines changed

6 files changed

+148
-0
lines changed

Cargo.lock

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ stdx = { path = "./crates/stdx", version = "0.0.0" }
7575
syntax = { path = "./crates/syntax", version = "0.0.0" }
7676
text-edit = { path = "./crates/text-edit", version = "0.0.0" }
7777
toolchain = { path = "./crates/toolchain", version = "0.0.0" }
78+
toml-syntax = { path = "./crates/toml-syntax", version = "0.0.0" }
7879
tt = { path = "./crates/tt", version = "0.0.0" }
7980
vfs-notify = { path = "./crates/vfs-notify", version = "0.0.0" }
8081
vfs = { path = "./crates/vfs", version = "0.0.0" }

crates/toml-sema/Cargo.toml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "toml-sema"
3+
version = "0.0.0"
4+
rust-version.workspace = true
5+
edition.workspace = true
6+
license.workspace = true
7+
authors.workspace = true
8+
9+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
10+
11+
[dependencies]
12+
toml-syntax.workspace = true
13+
semver.workspace = true
14+
either.workspace = true
15+
serde.workspace = true

crates/toml-sema/src/lib.rs

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#![allow(dead_code)]
2+
use semver;
3+
use serde::Deserializer;
4+
use std::collections::HashMap;
5+
use toml_syntax::dom;
6+
7+
struct Semantics {
8+
dict: HashMap<dom::Keys, Value>,
9+
}
10+
11+
struct KeyValuePair {
12+
key: dom::Keys,
13+
value: Value,
14+
}
15+
16+
struct Optional<T> {
17+
default: T,
18+
}
19+
20+
enum Value<T = ValueInner> {
21+
Optional(Optional<T>),
22+
Mandatory(T),
23+
}
24+
25+
enum ValueInner {
26+
Bool(bool),
27+
Integer(Integer),
28+
String(String),
29+
Semver(semver::Version),
30+
SemverReq(semver::VersionReq),
31+
}
32+
33+
enum Integer {
34+
RangedInteger(Ranged<i32>),
35+
Integer(i32),
36+
}
37+
38+
struct Ranged<T> {
39+
lower: T,
40+
upper: T,
41+
}
42+
43+
enum String<T> {
44+
45+
EitherOf(T),
46+
}
47+
48+
fn str_to_keys(s: &'_ str) -> dom::Keys {
49+
let subkeys = s.split(".").into_iter().map(|sfix| {
50+
assert!(!sfix.is_empty());
51+
dom::KeyOrIndex::Key(dom::node::Key::new(sfix))
52+
});
53+
dom::Keys::new(subkeys)
54+
}
55+
56+
impl Semantics {
57+
fn new(kvs: Vec<(&str, Value)>) -> Semantics {
58+
Semantics { dict: kvs.into_iter().map(|kv| (str_to_keys(kv.0), kv.1)).collect() }
59+
}
60+
}
61+
62+
#[cfg(test)]
63+
mod tests {
64+
use serde::Deserialize;
65+
use toml_syntax::dom::{node::Key, KeyOrIndex, Keys};
66+
67+
use crate::{KeyValuePair, Optional, Semantics, Value};
68+
69+
fn test_1() {
70+
let a = r#"
71+
[assist]
72+
emitMustUse = true
73+
expressionFillDefault = "todo"
74+
75+
[cargo]
76+
buildScripts.enable = true
77+
78+
[cargo.buildScripts]
79+
invocationLocation = "workspace"
80+
"#;
81+
let parsed = toml_syntax::parse_toml(a);
82+
let dom = parsed.into_dom();
83+
84+
#[derive(Deserialize, Debug, Clone)]
85+
#[serde(rename_all = "snake_case")]
86+
enum ExprFillDefaultDef {
87+
Todo,
88+
Default,
89+
}
90+
91+
let kv1 = ("assist.emitMustUse", Value::Optional(Optional { default: false }));
92+
let kv2 = ( "assist.expressionFillDefault" , Value::Optional(Optional { default: }));
93+
94+
let sema = Semantics::new(vec![]);
95+
}
96+
}

crates/toml-syntax/Cargo.toml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "toml-syntax"
3+
version = "0.0.0"
4+
rust-version.workspace = true
5+
edition.workspace = true
6+
license.workspace = true
7+
authors.workspace = true
8+
9+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
10+
11+
[dependencies]
12+
taplo = "0.12.1"

crates/toml-syntax/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use taplo::parser;
2+
3+
pub use taplo::dom;
4+
5+
pub fn parse_toml<'a>(content: &'a str) -> parser::Parse {
6+
parser::parse(content)
7+
}

0 commit comments

Comments
 (0)