valve-keyvalue 1.0.3

A parser and serializer for the Valve KeyValue format (VMT/VDF).
Documentation
/*
   Copyright 2026 Gerg0Vagyok

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

use crate::error::Result;

#[derive(Debug, PartialEq, Clone)]
pub struct ValveKeyValue {
	pub key: String,
	pub value: ValveKeyValueType
}

#[derive(Debug, PartialEq, Clone)]
pub enum ValveKeyValueType {
	String(String), Object(Vec<ValveKeyValue>)
}

pub trait Serialize: Sized {
	fn serialize(&self, use_escape_sequences: bool, indentation_steps: usize) -> Result<String>;
}

pub trait Parse: Sized {
	fn parse(input: String, use_escape_sequences: bool) -> Result<Vec<Self>>;
}

impl Serialize for ValveKeyValue {
	fn serialize(&self, use_escape_sequences: bool, indentation_steps: usize) -> Result<String> {
		crate::serialize::serialize(vec![self.clone()], use_escape_sequences, indentation_steps)
	}
}

impl Serialize for Vec<ValveKeyValue> {
	fn serialize(&self, use_escape_sequences: bool, indentation_steps: usize) -> Result<String> {
		crate::serialize::serialize(self.clone(), use_escape_sequences, indentation_steps)
	}
}

impl Parse for ValveKeyValue {
	fn parse(input: String, use_escape_sequences: bool) -> Result<Vec<Self>> {
		crate::parse::parse(input, use_escape_sequences)
	}
}

impl ValveKeyValue {
	pub fn new(key: String, value: ValveKeyValueType) -> Self {
		Self { key, value }
	}
}