Skip to content

Commit 170c094

Browse files
authored
Merge pull request #358 from koto-lang/various-tweaks
Various tweaks
2 parents 8cc8300 + 7b0fc95 commit 170c094

File tree

9 files changed

+147
-223
lines changed

9 files changed

+147
-223
lines changed

Cargo.lock

Lines changed: 97 additions & 194 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ glam = "0.22.0"
2626
# Shared definitions of home directories.
2727
home = "0.5.5"
2828
# A Rust library for conveniently watching and handling file changes.
29-
hotwatch = "0.4.5"
29+
hotwatch = "0.5.0"
3030
# A hash table with consistent order and fast iteration.
3131
indexmap = "2.0.0"
3232
# A partial replacement for std::time::Instant that works on WASM too.
@@ -67,8 +67,8 @@ saturating_cast = "0.1.0"
6767
serde = "1.0.0"
6868
# JSON support for serde
6969
serde_json = { version = "1.0.0", features = ["preserve_order", "std"] }
70-
# YAML support for serde
71-
serde_yaml = "0.8.20"
70+
# YAML data format for serde
71+
serde_yaml_ng = "0.10.0"
7272
# 'Small vector' optimization: store up to a small number of items on the stack
7373
smallvec = { version = "1.11.1", features = ["const_generics", "union"] }
7474
# Parser for Rust source code

crates/cli/docs/libs/yaml.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ nested:
2020
number: -1.2
2121
2222
entries:
23-
- foo: bar
24-
- foo: baz
23+
- foo: bar
24+
- foo: baz
2525
'
2626
2727
result = yaml.from_string data
@@ -56,12 +56,12 @@ data =
5656
)
5757
5858
print! yaml.to_string data
59-
check! ---
60-
check! string: ">_>"
59+
check! string: '>_>'
6160
check! nested:
6261
check! number: 99
6362
check! entries:
64-
check! - foo: bar
65-
check! - foo: baz
63+
check! - foo: bar
64+
check! - foo: baz
6665
check!
6766
```
67+

crates/koto/examples/poetry/src/main.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ mod poetry;
44
use anyhow::{bail, Context, Result};
55
use hotwatch::{
66
blocking::{Flow, Hotwatch},
7-
Event,
7+
notify::event::ModifyKind,
8+
Event, EventKind,
89
};
910
use koto::{Koto, KotoSettings};
1011
use poetry::*;
11-
use std::{fs, path::Path, time::Duration};
12+
use std::{
13+
fs,
14+
path::{Path, PathBuf},
15+
time::Duration,
16+
};
1217

1318
fn version_string() -> String {
1419
format!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"))
@@ -87,21 +92,21 @@ fn main() -> Result<()> {
8792
.insert("poetry", koto_bindings::make_module());
8893
koto.prelude().insert("random", koto_random::make_module());
8994

90-
let script_path = Path::new(&args.script);
91-
koto.set_script_path(Some(script_path))
95+
let script_path = PathBuf::from(args.script);
96+
koto.set_script_path(Some(&script_path))
9297
.expect("Failed to set script path");
9398

9499
if args.watch {
95-
if let Err(e) = compile_and_run(&mut koto, script_path) {
100+
if let Err(e) = compile_and_run(&mut koto, &script_path) {
96101
eprintln!("{e}");
97102
}
98103

99104
let mut hotwatch = Hotwatch::new_with_custom_delay(Duration::from_secs_f64(0.25))
100105
.context("Failed to initialize file watcher")?;
101106
hotwatch
102-
.watch(&args.script, move |event: Event| {
103-
match event {
104-
Event::Create(script_path) | Event::Write(script_path) => {
107+
.watch(script_path.clone(), move |event: Event| {
108+
match event.kind {
109+
EventKind::Create(_) | EventKind::Modify(ModifyKind::Data(_)) => {
105110
if let Err(error) = compile_and_run(&mut koto, &script_path) {
106111
eprintln!("{error}");
107112
}
@@ -114,7 +119,7 @@ fn main() -> Result<()> {
114119
hotwatch.run();
115120
Ok(())
116121
} else {
117-
compile_and_run(&mut koto, script_path)
122+
compile_and_run(&mut koto, &script_path)
118123
}
119124
}
120125

crates/parser/src/node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub enum Node {
107107
/// Keys will either be Id, String, or Meta nodes.
108108
///
109109
/// Values are optional for inline maps.
110-
Map(Vec<(AstIndex, Option<AstIndex>)>),
110+
Map(AstVec<(AstIndex, Option<AstIndex>)>),
111111

112112
/// The `self` keyword
113113
Self_,

crates/parser/src/parser.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,7 +1861,7 @@ impl<'source> Parser<'source> {
18611861
return self.error(InternalError::ExpectedMapColon);
18621862
}
18631863

1864-
let mut entries = vec![(first_key, Some(self.consume_map_block_value()?))];
1864+
let mut entries = astvec![(first_key, Some(self.consume_map_block_value()?))];
18651865

18661866
let block_context = ExpressionContext::permissive()
18671867
.with_expected_indentation(Indentation::Equal(start_indent));
@@ -1919,8 +1919,10 @@ impl<'source> Parser<'source> {
19191919
)
19201920
}
19211921

1922-
fn parse_comma_separated_map_entries(&mut self) -> Result<Vec<(AstIndex, Option<AstIndex>)>> {
1923-
let mut entries = Vec::new();
1922+
fn parse_comma_separated_map_entries(
1923+
&mut self,
1924+
) -> Result<AstVec<(AstIndex, Option<AstIndex>)>> {
1925+
let mut entries = AstVec::new();
19241926
let mut entry_context = ExpressionContext::braced_items_start();
19251927

19261928
while self.peek_token_with_context(&entry_context).is_some() {

koto/tests/libs/data/test.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ nested:
88
string: hello
99

1010
entries:
11-
- foo: bar
12-
- foo: baz
11+
- foo: bar
12+
- foo: baz

libs/yaml/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ repository = "https://github.com/koto-lang/koto"
1010
keywords = ["scripting", "language", "koto", "yaml"]
1111

1212
[dependencies]
13-
serde_yaml = { workspace = true }
13+
serde_yaml_ng = { workspace = true }
1414

1515
[dependencies.koto_runtime]
1616
path = "../../crates/runtime"

libs/yaml/src/lib.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
33
use koto_runtime::{prelude::*, Result};
44
use koto_serialize::SerializableValue;
5-
use serde_yaml::Value as YamlValue;
5+
use serde_yaml_ng::Value as YamlValue;
66

7-
pub fn yaml_value_to_koto_value(value: &serde_yaml::Value) -> Result<KValue> {
7+
pub fn yaml_value_to_koto_value(value: &YamlValue) -> Result<KValue> {
88
let result = match value {
99
YamlValue::Null => KValue::Null,
1010
YamlValue::Bool(b) => KValue::Bool(*b),
@@ -37,6 +37,20 @@ pub fn yaml_value_to_koto_value(value: &serde_yaml::Value) -> Result<KValue> {
3737
}
3838
KValue::Map(map)
3939
}
40+
YamlValue::Tagged(tagged_value) => {
41+
let map = KMap::with_type("TaggedValue");
42+
43+
let tag = tagged_value.tag.to_string();
44+
let tag = match tag.strip_prefix("!") {
45+
Some(stripped) => stripped.to_string(),
46+
None => tag,
47+
};
48+
49+
map.insert("tag", tag);
50+
map.insert("value", yaml_value_to_koto_value(value)?);
51+
52+
KValue::Map(map)
53+
}
4054
};
4155

4256
Ok(result)
@@ -46,7 +60,7 @@ pub fn make_module() -> KMap {
4660
let result = KMap::with_type("yaml");
4761

4862
result.add_fn("from_string", |ctx| match ctx.args() {
49-
[KValue::Str(s)] => match serde_yaml::from_str(s) {
63+
[KValue::Str(s)] => match serde_yaml_ng::from_str(s) {
5064
Ok(value) => match yaml_value_to_koto_value(&value) {
5165
Ok(result) => Ok(result),
5266
Err(e) => runtime_error!("Error while parsing input: {}", e),
@@ -57,7 +71,7 @@ pub fn make_module() -> KMap {
5771
});
5872

5973
result.add_fn("to_string", |ctx| match ctx.args() {
60-
[value] => match serde_yaml::to_string(&SerializableValue(value)) {
74+
[value] => match serde_yaml_ng::to_string(&SerializableValue(value)) {
6175
Ok(result) => Ok(result.into()),
6276
Err(e) => runtime_error!("yaml.to_string: {}", e),
6377
},

0 commit comments

Comments
 (0)