This commit is contained in:
Adam Chalmers
2024-08-14 15:22:20 -05:00
parent 223e0c0edd
commit bff0bd8696
3 changed files with 12 additions and 2 deletions

View File

@ -4,14 +4,15 @@ use std::{fmt::Display, num::TryFromIntError};
/// Rust types and KCL types.
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Some error not covered by other cases.
#[error("{0}")]
Message(String),
/// Number is too big.
#[error("Number is too big")]
NumberTooBig,
/// Invalid key for a KCL object.
#[error("You cannot use this as a key of a KCL object")]
InvalidKey,
#[error("Invalid syntax")]
Syntax,
}
impl From<TryFromIntError> for Error {

View File

@ -1,3 +1,9 @@
//! # Serde KCL
//!
//! KCL (KittyCAD Language) has an object model similar to JSON.
//! This crate works similarly to serde_json.
#![deny(missing_docs)]
use serde::Serialize;
pub use crate::{error::Error, object::Object, value::Value};

View File

@ -3,6 +3,7 @@ use crate::Object;
pub(crate) mod de;
pub(crate) mod ser;
/// Values that can be represented in KCL.
#[derive(Debug, PartialEq)]
pub enum Value {
/// A value to use when the specific value isn't really important.
@ -33,6 +34,7 @@ pub enum Value {
macro_rules! impl_as {
($name:ident, $variant:ident, $return_type:ty) => {
/// If the KCL value matches this type, return it.
pub fn $name(&self) -> Option<&$return_type> {
match self {
Self::$variant(x) => Some(x),
@ -60,6 +62,7 @@ impl Value {
impl_as!(as_array, Array, Vec<Value>);
impl_as!(as_object, Object, Object);
impl_as!(as_binary, Bytes, Vec<u8>);
/// If the KCL value matches this type, return it.
pub fn as_unit(&self) -> Option<()> {
match self {
Self::Unit => Some(()),