Compare commits

...

1 Commits

Author SHA1 Message Date
9697f040bd Add serde-kcl library for working with KCL objects/values
This defines:

 - serde_kcl::Value
 - serde_kcl::Object
 - serde_kcl::to_val (takes a Rust type and 'serializes' it into KCL object)

All similar to their equivalents in `serde_json`.

Part of #1130
2024-08-14 12:41:58 -05:00
8 changed files with 811 additions and 0 deletions

View File

@ -2590,6 +2590,15 @@ dependencies = [
"serde_derive",
]
[[package]]
name = "serde-kcl"
version = "0.1.0"
dependencies = [
"ryu",
"serde",
"thiserror",
]
[[package]]
name = "serde_bytes"
version = "0.11.14"

View File

@ -66,6 +66,7 @@ members = [
"kcl",
"kcl-macros",
"kcl-test-server",
"serde-kcl",
]
[workspace.dependencies]

View File

@ -0,0 +1,18 @@
[package]
name = "serde-kcl"
description = "KittyCAD Language object model"
version = "0.1.0"
edition = "2021"
repository = "https://github.com/KittyCAD/modeling-app"
rust-version = "1.80"
license = "MIT"
authors = ["Jess Frazelle", "Adam Chalmers", "Jon Tran", "KittyCAD, Inc"]
keywords = ["kcl", "KittyCAD", "CAD"]
[dependencies]
ryu = "1.0"
serde = "1.0.207"
thiserror = "1.0.63"
[dev-dependencies]
serde = { version = "1.0.207", features = ["derive"]}

View File

@ -0,0 +1,29 @@
use std::{fmt::Display, num::TryFromIntError};
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("{0}")]
Message(String),
#[error("Number is too big")]
NumberTooBig,
#[error("You cannot use this as a key of a KCL object")]
InvalidKey,
}
impl From<TryFromIntError> for Error {
fn from(_: TryFromIntError) -> Self {
Self::NumberTooBig
}
}
impl serde::ser::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Self::Message(msg.to_string())
}
}
impl serde::de::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Self::Message(msg.to_string())
}
}

View File

@ -0,0 +1,44 @@
use serde::Serialize;
pub use crate::error::Error;
pub use crate::object::Object;
pub use crate::value::Value;
mod error;
mod object;
mod value;
pub fn to_value<T>(value: T) -> Result<Value, Error>
where
T: Serialize,
{
value.serialize(crate::value::ser::Serializer)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn structs_into_kcl_object() {
#[derive(serde::Serialize)]
struct Person {
name: String,
age: u8,
}
let adam = Person {
name: "Adam".to_owned(),
age: 32,
};
let val = to_value(&adam).expect("Serializing to KCL object should pass");
let obj = val.as_object().unwrap();
let expected = Object {
properties: std::collections::HashMap::from([
("name".to_owned(), Value::from("Adam".to_owned())),
("age".to_owned(), Value::from(32)),
]),
};
assert_eq!(obj.properties, expected.properties);
}
}

View File

@ -0,0 +1,35 @@
use std::collections::HashMap;
use crate::Value;
/// A KCL object.
#[derive(Debug, Default, PartialEq)]
pub struct Object {
/// The object's properties.
pub properties: HashMap<String, Value>,
}
impl Object {
/// Create a new object with no properties.
pub fn new() -> Self {
Self::default()
}
/// How many properties does this object have?
pub fn len(&self) -> usize {
self.properties.len()
}
/// Add a new property to the object.
/// If the object already has a property with this name, overwrites it.
pub fn insert(&mut self, property: String, value: Value) {
self.properties.insert(property, value);
}
}
/// Given a list of (key, value) pairs, you can make a KCL object.
impl<const N: usize> From<[(String, Value); N]> for Object {
fn from(value: [(String, Value); N]) -> Self {
Self {
properties: HashMap::from(value),
}
}
}

View File

@ -0,0 +1,84 @@
use crate::Object;
pub(crate) mod ser;
#[derive(Debug, PartialEq)]
pub enum Value {
/// A value to use when the specific value isn't really important.
/// For example, this is the return type of functions that don't return
/// any other value.
///
/// Don't worry about it too much.
///
/// Kind of like 'null' in other languages, but it doesn't have the
/// connotation that nothing was missing. It probably means nothing was
/// required, not nothing was found.
Unit,
/// Either true or false.
Boolean(bool),
/// Text.
String(String),
/// Whole numbers (positive, negative or zero).
Integer(i64),
/// Numbers with a fractional part.
Float(f64),
/// A list of other values.
Array(Vec<Value>),
/// A set of properties. Each property has a name (aka "key") and a value.
Object(Object),
/// Binary data
Bytes(Vec<u8>),
}
macro_rules! impl_as {
($name:ident, $variant:ident, $return_type:ty) => {
pub fn $name(&self) -> Option<&$return_type> {
match self {
Self::$variant(x) => Some(x),
_ => None,
}
}
};
}
macro_rules! impl_from {
($variant:ident, $t:ty) => {
impl From<$t> for Value {
fn from(t: $t) -> Self {
Self::$variant(t.into())
}
}
};
}
impl Value {
impl_as!(as_boolean, Boolean, bool);
impl_as!(as_string, String, String);
impl_as!(as_integer, Integer, i64);
impl_as!(as_float, Float, f64);
impl_as!(as_array, Array, Vec<Value>);
impl_as!(as_object, Object, Object);
impl_as!(as_binary, Bytes, Vec<u8>);
pub fn as_unit(&self) -> Option<()> {
match self {
Self::Unit => Some(()),
_ => None,
}
}
}
impl_from!(String, String);
impl_from!(Boolean, bool);
impl_from!(Integer, i64);
impl_from!(Integer, i32);
impl_from!(Integer, u32);
impl_from!(Integer, u8);
impl_from!(Integer, i8);
impl_from!(Float, f64);
impl_from!(Float, f32);
impl_from!(Bytes, Vec<u8>);
impl From<()> for Value {
fn from(_: ()) -> Self {
Self::Unit
}
}

View File

@ -0,0 +1,591 @@
use serde::ser::Impossible;
use serde::Serialize;
use crate::value::Value;
use crate::{to_value, Error, Object};
// We only use our own error type; no need for From conversions provided by the
// standard library's try! macro. This reduces lines of LLVM IR by 4%.
macro_rules! tri {
($e:expr $(,)?) => {
match $e {
core::result::Result::Ok(val) => val,
core::result::Result::Err(err) => return core::result::Result::Err(err),
}
};
}
impl Serialize for Value {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: ::serde::Serializer,
{
match self {
Self::Unit => serializer.serialize_unit(),
Self::Boolean(b) => serializer.serialize_bool(*b),
Self::String(s) => serializer.serialize_str(s),
Self::Integer(x) => serializer.serialize_i64(*x),
Self::Float(x) => serializer.serialize_f64(*x),
Self::Bytes(b) => serializer.serialize_bytes(b),
Self::Array(v) => serializer.collect_seq(v),
Self::Object(o) => {
use serde::ser::SerializeMap;
let mut map = serializer.serialize_map(Some(o.len()))?;
for (k, v) in &o.properties {
map.serialize_entry(k, v)?;
}
map.end()
}
}
}
}
pub(crate) struct Serializer;
type Result<T> = std::result::Result<T, Error>;
pub struct SerializeVec {
vec: Vec<Value>,
}
impl serde::ser::SerializeSeq for SerializeVec {
type Ok = Value;
type Error = Error;
fn serialize_element<T>(&mut self, value: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
self.vec.push(tri!(to_value(value)));
Ok(())
}
fn end(self) -> Result<Value> {
Ok(Value::Array(self.vec))
}
}
impl serde::ser::SerializeTuple for SerializeVec {
type Ok = Value;
type Error = Error;
fn serialize_element<T>(&mut self, value: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
serde::ser::SerializeSeq::serialize_element(self, value)
}
fn end(self) -> Result<Value> {
serde::ser::SerializeSeq::end(self)
}
}
impl serde::ser::SerializeTupleStruct for SerializeVec {
type Ok = Value;
type Error = Error;
fn serialize_field<T>(&mut self, value: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
serde::ser::SerializeSeq::serialize_element(self, value)
}
fn end(self) -> Result<Value> {
serde::ser::SerializeSeq::end(self)
}
}
struct MapKeySerializer;
fn key_must_be_a_string() -> Error {
Error::InvalidKey
}
fn float_key_must_be_finite() -> Error {
Error::InvalidKey
}
impl serde::Serializer for MapKeySerializer {
type Ok = String;
type Error = Error;
type SerializeSeq = Impossible<String, Error>;
type SerializeTuple = Impossible<String, Error>;
type SerializeTupleStruct = Impossible<String, Error>;
type SerializeTupleVariant = Impossible<String, Error>;
type SerializeMap = Impossible<String, Error>;
type SerializeStruct = Impossible<String, Error>;
type SerializeStructVariant = Impossible<String, Error>;
#[inline]
fn serialize_unit_variant(self, _name: &'static str, _variant_index: u32, variant: &'static str) -> Result<String> {
Ok(variant.to_owned())
}
#[inline]
fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<String>
where
T: ?Sized + Serialize,
{
value.serialize(self)
}
fn serialize_bool(self, value: bool) -> Result<String> {
Ok(value.to_string())
}
fn serialize_i8(self, value: i8) -> Result<String> {
Ok(value.to_string())
}
fn serialize_i16(self, value: i16) -> Result<String> {
Ok(value.to_string())
}
fn serialize_i32(self, value: i32) -> Result<String> {
Ok(value.to_string())
}
fn serialize_i64(self, value: i64) -> Result<String> {
Ok(value.to_string())
}
fn serialize_u8(self, value: u8) -> Result<String> {
Ok(value.to_string())
}
fn serialize_u16(self, value: u16) -> Result<String> {
Ok(value.to_string())
}
fn serialize_u32(self, value: u32) -> Result<String> {
Ok(value.to_string())
}
fn serialize_u64(self, value: u64) -> Result<String> {
Ok(value.to_string())
}
fn serialize_f32(self, value: f32) -> Result<String> {
if value.is_finite() {
Ok(ryu::Buffer::new().format_finite(value).to_owned())
} else {
Err(float_key_must_be_finite())
}
}
fn serialize_f64(self, value: f64) -> Result<String> {
if value.is_finite() {
Ok(ryu::Buffer::new().format_finite(value).to_owned())
} else {
Err(float_key_must_be_finite())
}
}
#[inline]
fn serialize_char(self, value: char) -> Result<String> {
Ok({
let mut s = String::new();
s.push(value);
s
})
}
#[inline]
fn serialize_str(self, value: &str) -> Result<String> {
Ok(value.to_owned())
}
fn serialize_bytes(self, _value: &[u8]) -> Result<String> {
Err(key_must_be_a_string())
}
fn serialize_unit(self) -> Result<String> {
Err(key_must_be_a_string())
}
fn serialize_unit_struct(self, _name: &'static str) -> Result<String> {
Err(key_must_be_a_string())
}
fn serialize_newtype_variant<T>(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_value: &T,
) -> Result<String>
where
T: ?Sized + Serialize,
{
Err(key_must_be_a_string())
}
fn serialize_none(self) -> Result<String> {
Err(key_must_be_a_string())
}
fn serialize_some<T>(self, _value: &T) -> Result<String>
where
T: ?Sized + Serialize,
{
Err(key_must_be_a_string())
}
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
Err(key_must_be_a_string())
}
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
Err(key_must_be_a_string())
}
fn serialize_tuple_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeTupleStruct> {
Err(key_must_be_a_string())
}
fn serialize_tuple_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleVariant> {
Err(key_must_be_a_string())
}
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
Err(key_must_be_a_string())
}
fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
Err(key_must_be_a_string())
}
fn serialize_struct_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeStructVariant> {
Err(key_must_be_a_string())
}
fn collect_str<T>(self, value: &T) -> Result<String>
where
T: ?Sized + std::fmt::Display,
{
Ok(value.to_string())
}
}
pub struct SerializeTupleVariant {
name: String,
vec: Vec<Value>,
}
impl serde::ser::SerializeTupleVariant for SerializeTupleVariant {
type Ok = Value;
type Error = Error;
fn serialize_field<T>(&mut self, value: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
self.vec.push(to_value(value)?);
Ok(())
}
fn end(self) -> Result<Value> {
let mut object = Object::new();
object.insert(self.name, Value::Array(self.vec));
Ok(Value::Object(object))
}
}
pub enum SerializeMap {
Map { map: Object, next_key: Option<String> },
}
impl serde::ser::SerializeMap for SerializeMap {
type Ok = Value;
type Error = Error;
fn serialize_key<T>(&mut self, key: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
match self {
SerializeMap::Map { next_key, .. } => {
*next_key = Some(tri!(key.serialize(MapKeySerializer)));
Ok(())
}
}
}
fn serialize_value<T>(&mut self, value: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
match self {
SerializeMap::Map { map, next_key } => {
let key = next_key.take();
// Panic because this indicates a bug in the program rather than an
// expected failure.
let key = key.expect("serialize_value called before serialize_key");
map.insert(key, tri!(to_value(value)));
Ok(())
}
}
}
fn end(self) -> Result<Value> {
match self {
SerializeMap::Map { map, .. } => Ok(Value::Object(map)),
}
}
}
impl serde::ser::SerializeStruct for SerializeMap {
type Ok = Value;
type Error = Error;
fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
match self {
SerializeMap::Map { .. } => serde::ser::SerializeMap::serialize_entry(self, key, value),
}
}
fn end(self) -> Result<Value> {
match self {
SerializeMap::Map { .. } => serde::ser::SerializeMap::end(self),
}
}
}
pub struct SerializeStructVariant {
name: String,
map: Object,
}
impl serde::ser::SerializeStructVariant for SerializeStructVariant {
type Ok = Value;
type Error = Error;
fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
self.map.insert(String::from(key), tri!(to_value(value)));
Ok(())
}
fn end(self) -> Result<Value> {
let mut object = Object::new();
object.insert(self.name, Value::Object(self.map));
Ok(Value::Object(object))
}
}
impl serde::Serializer for Serializer {
type Ok = Value;
type Error = Error;
type SerializeSeq = SerializeVec;
type SerializeTuple = SerializeVec;
type SerializeTupleStruct = SerializeVec;
type SerializeTupleVariant = SerializeTupleVariant;
type SerializeMap = SerializeMap;
type SerializeStruct = SerializeMap;
type SerializeStructVariant = SerializeStructVariant;
#[inline]
fn serialize_bool(self, value: bool) -> Result<Value> {
Ok(Value::Boolean(value))
}
#[inline]
fn serialize_i8(self, value: i8) -> Result<Value> {
self.serialize_i64(value as i64)
}
#[inline]
fn serialize_i16(self, value: i16) -> Result<Value> {
self.serialize_i64(value as i64)
}
#[inline]
fn serialize_i32(self, value: i32) -> Result<Value> {
self.serialize_i64(value as i64)
}
fn serialize_i64(self, value: i64) -> Result<Value> {
Ok(Value::Integer(value.into()))
}
#[inline]
fn serialize_u8(self, value: u8) -> Result<Value> {
self.serialize_u64(value as u64)
}
#[inline]
fn serialize_u16(self, value: u16) -> Result<Value> {
self.serialize_u64(value as u64)
}
#[inline]
fn serialize_u32(self, value: u32) -> Result<Value> {
self.serialize_u64(value as u64)
}
#[inline]
fn serialize_u64(self, value: u64) -> Result<Value> {
Ok(Value::Integer(value.try_into()?))
}
#[inline]
fn serialize_f32(self, float: f32) -> Result<Value> {
Ok(Value::Float(float.into()))
}
#[inline]
fn serialize_f64(self, float: f64) -> Result<Value> {
Ok(Value::Float(float))
}
#[inline]
fn serialize_char(self, value: char) -> Result<Value> {
let mut s = String::new();
s.push(value);
Ok(Value::String(s))
}
#[inline]
fn serialize_str(self, value: &str) -> Result<Value> {
Ok(Value::String(value.to_owned()))
}
fn serialize_bytes(self, value: &[u8]) -> Result<Value> {
let vec = value.to_owned();
Ok(Value::Bytes(vec))
}
#[inline]
fn serialize_unit(self) -> Result<Value> {
Ok(Value::Unit)
}
#[inline]
fn serialize_unit_struct(self, _name: &'static str) -> Result<Value> {
self.serialize_unit()
}
#[inline]
fn serialize_unit_variant(self, _name: &'static str, _variant_index: u32, variant: &'static str) -> Result<Value> {
self.serialize_str(variant)
}
#[inline]
fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<Value>
where
T: ?Sized + Serialize,
{
value.serialize(self)
}
fn serialize_newtype_variant<T>(
self,
_name: &'static str,
_variant_index: u32,
variant: &'static str,
value: &T,
) -> Result<Value>
where
T: ?Sized + Serialize,
{
let mut values = Object::default();
values.insert(String::from(variant), to_value(value)?);
Ok(Value::Object(values))
}
#[inline]
fn serialize_none(self) -> Result<Value> {
self.serialize_unit()
}
#[inline]
fn serialize_some<T>(self, value: &T) -> Result<Value>
where
T: ?Sized + Serialize,
{
value.serialize(self)
}
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
Ok(SerializeVec {
vec: Vec::with_capacity(len.unwrap_or(0)),
})
}
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> {
self.serialize_seq(Some(len))
}
fn serialize_tuple_struct(self, _name: &'static str, len: usize) -> Result<Self::SerializeTupleStruct> {
self.serialize_seq(Some(len))
}
fn serialize_tuple_variant(
self,
_name: &'static str,
_variant_index: u32,
variant: &'static str,
len: usize,
) -> Result<Self::SerializeTupleVariant> {
Ok(SerializeTupleVariant {
name: String::from(variant),
vec: Vec::with_capacity(len),
})
}
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
Ok(SerializeMap::Map {
map: Object::new(),
next_key: None,
})
}
fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeStruct> {
match name {
_ => self.serialize_map(Some(len)),
}
}
fn serialize_struct_variant(
self,
_name: &'static str,
_variant_index: u32,
variant: &'static str,
_len: usize,
) -> Result<Self::SerializeStructVariant> {
Ok(SerializeStructVariant {
name: String::from(variant),
map: Object::new(),
})
}
fn collect_str<T>(self, value: &T) -> Result<Value>
where
T: ?Sized + std::fmt::Display,
{
Ok(Value::String(value.to_string()))
}
}