we got tests passing!

Adding components to save files is a pain, but hopefully a one-time cost.
attempt/001
Elijah Voigt 1 year ago
parent e8b15c80c1
commit 7947be5cfb

@ -21,6 +21,7 @@ mkShell rec {
udev # Audio?
vulkan-loader # Rendering
xorg.libX11 xorg.libXcursor xorg.libXi xorg.libXrandr # To use the x11 feature
tmux # Sharing environemnt between editor and terminal
];
LD_LIBRARY_PATH = lib.makeLibraryPath buildInputs;

@ -1,3 +1,5 @@
use nom::character::complete::hex_digit1;
use crate::prelude::*;
#[derive(Error, Debug)]
@ -12,6 +14,8 @@ pub(crate) enum SaveEntityParseError {
Nom(nom::Err<nom::error::Error<Box<str>>>),
#[error("Failed to parse camera")]
Camera,
#[error("Failed to parse marker component")]
Marker,
}
// Convert Nom error to parse error
@ -22,25 +26,20 @@ impl From<nom::Err<nom::error::Error<&str>>> for SaveEntityParseError {
}
}
fn parse_word<'a>(w: &'a str) -> impl Fn(&'a str) -> IResult<&'a str, &'a str> {
move |i: &'a str| tag(w)(i)
fn parse_bool(i: &str) -> IResult<&str, bool> {
alt((tag("true"), tag("false")))(i).map(|(s, val)| match val {
"true" => (s, true),
"false" => (s, false),
_ => panic!("Bools must be `true` or `false`!"),
})
}
fn parse_xyz(i: &str) -> IResult<&str, (f32, f32, f32)> {
tuple((float, space1, float, space1, float))(i)
.map(|(s, (x, _, y, _, z))| (s, (x, y, z)))
tuple((float, space1, float, space1, float))(i).map(|(s, (x, _, y, _, z))| (s, (x, y, z)))
}
fn parse_wxyz(i: &str) -> IResult<&str, (f32, f32, f32, f32)> {
tuple((
float,
space1,
float,
space1,
float,
space1,
float,
))(i)
tuple((float, space1, float, space1, float, space1, float))(i)
.map(|(s, (w, _, x, _, y, _, z))| (s, (w, x, y, z)))
}
@ -58,13 +57,19 @@ pub(crate) fn parse_save_transform(line: &str) -> Result<Transform, SaveEntityPa
let mut curr = rem.trim_start();
for _ in 0..3 {
if let Ok((rem, (_, _, (x, y, z)))) = tuple((parse_word("translation"), space1, parse_xyz))(curr.trim_start()) {
if let Ok((rem, (_, _, (x, y, z)))) =
tuple((tag("translation"), space1, parse_xyz))(curr.trim_start())
{
transform.translation = Vec3::new(x, y, z);
curr = rem.trim_start();
} else if let Ok((rem, (_, _, (x, y, z, w)))) = tuple((parse_word("rotation"), space1, parse_wxyz))(curr.trim_start()) {
} else if let Ok((rem, (_, _, (x, y, z, w)))) =
tuple((tag("rotation"), space1, parse_wxyz))(curr.trim_start())
{
transform.rotation = Quat::from_xyzw(x, y, z, w);
curr = rem.trim_start();
} else if let Ok((rem, (_, _, (x, y, z)))) = tuple((parse_word("scale"), space1, parse_xyz))(curr.trim_start()) {
} else if let Ok((rem, (_, _, (x, y, z)))) =
tuple((tag("scale"), space1, parse_xyz))(curr.trim_start())
{
transform.scale = Vec3::new(x, y, z);
curr = rem.trim_start();
} else {
@ -122,11 +127,10 @@ fn test_parse_name() {
}
}
#[derive(Component, Clone, Debug, Reflect, PartialEq)]
#[reflect(Component)]
pub(crate) struct EntityUuid {
id: String
id: String,
}
///
@ -141,7 +145,9 @@ pub(crate) fn parse_save_uuid(line: &str) -> Result<EntityUuid, SaveEntityParseE
fn test_parse_uuid() {
let line = "uuid 1c16ab9a-5f79-4340-8469-4086f69c64f2";
let parsed = parse_save_uuid(line).unwrap();
let expected = EntityUuid { id: "1c16ab9a-5f79-4340-8469-4086f69c64f2".into() };
let expected = EntityUuid {
id: "1c16ab9a-5f79-4340-8469-4086f69c64f2".into(),
};
assert_eq!(parsed, expected);
}
@ -149,13 +155,8 @@ fn test_parse_uuid() {
///
///
pub(crate) fn parse_save_model(line: &str) -> Result<(String, String), SaveEntityParseError> {
let (rem, (_, _, gltf_path, _, scene_name)) = tuple((
tag("model"),
space1,
parse_string,
space1,
parse_string,
))(line)?;
let (rem, (_, _, gltf_path, _, scene_name)) =
tuple((tag("model"), space1, parse_string, space1, parse_string))(line)?;
debug_assert!(rem == "");
@ -179,7 +180,9 @@ pub(crate) enum SaveCameraRenderTarget {
Window(Uuid),
}
pub(crate) fn parse_save_camera(line: &str) -> Result<SaveCameraRenderTarget, SaveEntityParseError> {
pub(crate) fn parse_save_camera(
line: &str,
) -> Result<SaveCameraRenderTarget, SaveEntityParseError> {
if let Ok((rem, _camera)) = tag::<&str, &str, ()>("camera")(line) {
if let Ok((rem, (_, _target))) = tuple((space1::<&str, ()>, tag("target")))(rem) {
if let Ok((rem, (_, _window))) = tuple((space1::<&str, ()>, tag("window")))(rem) {
@ -246,7 +249,6 @@ fn test_parse_camera() {
}
}
/// SaveParent entity which is a reference to which entity this is a child of
/// A run-time system converts this to a bevy Parent component
#[derive(Component, PartialEq, Debug, Reflect, Clone)]
@ -258,12 +260,16 @@ pub(crate) struct SaveParent(String);
/// parent some_other_file.entity
/// ```
pub(crate) fn parse_save_parent(line: &str) -> Result<SaveParent, SaveEntityParseError> {
todo!()
let (rem, (_, _, parent_file)) = tuple((tag("parent"), space1, parse_string))(line)?;
debug_assert!(rem.is_empty());
Ok(SaveParent(parent_file.into()))
}
#[test]
fn test_parse_parent() {
let line = "parent some_other_file.entity";
let line = "parent \"some_other_file.entity\"";
let parsed = parse_save_parent(line).unwrap();
let expected = SaveParent("some_other_file.entity".into());
assert_eq!(parsed, expected);
@ -274,42 +280,101 @@ fn test_parse_parent() {
/// We only sparsely define the bits that we want to edit (for now)
///
pub(crate) fn parse_save_window(line: &str) -> Result<Window, SaveEntityParseError> {
todo!()
let (rem, (_, _, window_title)) = tuple((tag("window"), space1, parse_string))(line)?;
let (rem, (_, _, _, visibility)) = tuple((space1, tag("visible"), space1, parse_bool))(rem)?;
debug_assert!(rem.is_empty());
Ok(Window {
title: window_title.into(),
visible: visibility,
..default()
})
}
#[test]
fn test_parse_window() {
let line = "window \"Editor\" visible false";
let parsed = parse_save_window(line).unwrap();
let expected = Window { visible: false, title: "Editor".into(), ..default() };
let expected = Window {
visible: false,
title: "Editor".into(),
..default()
};
assert_eq!(parsed.visible, expected.visible);
assert_eq!(parsed.title, expected.title);
}
///
/// The UI Text bundle specified as a sparse subset of a bundle
///
/// ```text
/// text "This is the text" color #abc123 size 12.34
/// ```
pub(crate) fn parse_save_ui_text(line: &str) -> Result<TextBundle, SaveEntityParseError> {
todo!()
let (rem, (_tag_text, _space1, text)) = tuple((tag("text"), space1, parse_string))(line)?;
let (rem, (_space, _tag_color, _space1, _hash, hex_color)) =
tuple((space1, tag("color"), space1, char('#'), hex_digit1))(rem)?;
let (rem, (_space, _tag_size, _space1, font_size)) =
tuple((space1, tag("size"), space1, float))(rem)?;
debug_assert!(rem.is_empty());
let style = TextStyle {
color: Color::Srgba(Srgba::hex(hex_color).unwrap()),
font_size,
..default()
};
Ok(TextBundle {
text: Text::from_section(text, style),
..default()
})
}
#[test]
fn test_save_ui_text() {
todo!()
let line = "text \"This is the text\" color #caffee size 14.6";
let parsed = parse_save_ui_text(line).unwrap();
let expected = Text::from_section(
"This is the text",
TextStyle {
color: Srgba::hex("#caffee").unwrap().into(),
font_size: 14.6,
..default()
},
);
assert_eq!(parsed.text.sections[0].value, expected.sections[0].value);
assert_eq!(
parsed.text.sections[0].style.color,
expected.sections[0].style.color,
);
assert_eq!(
parsed.text.sections[0].style.font_size,
expected.sections[0].style.font_size,
);
}
///
/// Returns a parser function for a basic stand-alone tag
/// e.g., "editor_tag" or "ui_node"
pub(crate) fn parse_save_tag<T: Component + Reflect>(tag: &str) -> impl FnOnce(&str) -> Result<T, SaveEntityParseError> {
move |line: &str| -> Result<T, SaveEntityParseError> {
todo!()
}
pub(crate) fn parse_save_tag<T: Component + Reflect + Default>(
t: &str,
) -> impl FnOnce(&str) -> Result<T, SaveEntityParseError> + '_ {
move |line: &str| -> Result<T, SaveEntityParseError> { Ok(tag(t)(line).map(|_| T::default())?) }
}
#[derive(Component, Reflect, PartialEq, Debug, Default)]
#[reflect(Component)]
struct TestingTag;
#[test]
fn test_save_tag() {
todo!()
let line = "testing";
let parsed = parse_save_tag::<TestingTag>("testing")(line).unwrap();
let expected = TestingTag;
assert_eq!(parsed, expected);
}
#[derive(Component, Clone, Debug, Reflect, PartialEq)]
@ -317,11 +382,25 @@ fn test_save_tag() {
pub(crate) struct SaveTargetCamera(String);
/// Parses the a SaveTargetCamera which at runtime is converted to a TargetCamera
pub(crate) fn parse_save_target_camera(line: &str) -> Result<SaveTargetCamera, SaveEntityParseError> {
todo!()
/// ```text
/// target-camera "./level-camera.entity"
/// ```
pub(crate) fn parse_save_target_camera(
line: &str,
) -> Result<SaveTargetCamera, SaveEntityParseError> {
Ok(
tuple((tag("target-camera"), space1, parse_string))(line).map(
|(_, (_, _, target_camera_entity_file))| {
SaveTargetCamera(target_camera_entity_file.into())
},
)?,
)
}
#[test]
fn test_target_camera() {
todo!()
let line = "target-camera \"./level-camera.entity\"";
let parsed = parse_save_target_camera(line).unwrap();
let expected = SaveTargetCamera("./level-camera.entity".into());
assert_eq!(parsed, expected);
}

@ -1,23 +1,24 @@
pub(crate) use bevy::ecs::reflect::ReflectCommandExt;
pub(crate) use bevy::{
app::AppExit,
asset::{io::Reader, AssetLoader, LoadContext, AsyncReadExt},
color::palettes::css::{GRAY, RED, DARK_GREEN, BLUE},
asset::{io::Reader, AssetLoader, AsyncReadExt, LoadContext},
color::palettes::css::{BLUE, DARK_GREEN, GRAY, RED},
gltf::Gltf,
input::common_conditions::input_just_pressed,
prelude::*,
render::camera::RenderTarget,
window::{WindowRef, WindowCloseRequested, PrimaryWindow},
window::{PrimaryWindow, WindowCloseRequested, WindowRef},
};
pub(crate) use bevy::ecs::reflect::ReflectCommandExt;
pub(crate) use nom::bytes::complete::take;
pub(crate) use nom::{
IResult,
branch::{alt, permutation},
bytes::complete::{tag, take_until1},
character::complete::space1,
character::complete::{char, hex_digit1, space1},
number::complete::float,
sequence::tuple,
IResult,
};
pub(crate) use uuid::Uuid;
pub(crate) use thiserror::Error;
pub(crate) use uuid::Uuid;
pub(crate) use crate::{conditions::*, parser::*};

Loading…
Cancel
Save