Skip to main content

valve_keyvalue/
types.rs

1/*
2   Copyright 2026 Gerg0Vagyok
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15*/
16
17use crate::error::Result;
18
19#[derive(Debug, PartialEq, Clone)]
20pub struct ValveKeyValue {
21	pub key: String,
22	pub value: ValveKeyValueType
23}
24
25#[derive(Debug, PartialEq, Clone)]
26pub enum ValveKeyValueType {
27	String(String), Object(Vec<ValveKeyValue>)
28}
29
30pub trait Serialize: Sized {
31	fn serialize(&self, use_escape_sequences: bool, indentation_steps: usize) -> Result<String>;
32}
33
34pub trait Parse: Sized {
35	fn parse(input: String, use_escape_sequences: bool) -> Result<Vec<Self>>;
36}
37
38impl Serialize for ValveKeyValue {
39	fn serialize(&self, use_escape_sequences: bool, indentation_steps: usize) -> Result<String> {
40		crate::serialize::serialize(vec![self.clone()], use_escape_sequences, indentation_steps)
41	}
42}
43
44impl Serialize for Vec<ValveKeyValue> {
45	fn serialize(&self, use_escape_sequences: bool, indentation_steps: usize) -> Result<String> {
46		crate::serialize::serialize(self.clone(), use_escape_sequences, indentation_steps)
47	}
48}
49
50impl Parse for ValveKeyValue {
51	fn parse(input: String, use_escape_sequences: bool) -> Result<Vec<Self>> {
52		crate::parse::parse(input, use_escape_sequences)
53	}
54}
55
56impl ValveKeyValue {
57	pub fn new(key: String, value: ValveKeyValueType) -> Self {
58		Self { key, value }
59	}
60}