1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//! `tsproto-structs` contains machine readable data for several TeamSpeak
//! related topics.
//!
//! The underlying data files can be found in the [tsdeclarations](https://github.com/ReSpeak/tsdeclarations)
//! repository.
//!
//! The contained data may change with any version so the suggested way of
//! referring to this crate is using `tsproto-structs = "=0.1.0"`.
//!
//! The helper functions found in the root of this crate may also change with
//! any version.
//!
//! A change in the way data are stored and made accessible is considered an API
//! breaking change and will increment the minor version number.

use serde_derive::Deserialize;

pub mod book;
pub mod book_to_messages;
pub mod errors;
pub mod messages;
pub mod messages_to_book;
pub mod permissions;
pub mod versions;

#[derive(Debug, Deserialize)]
pub struct EnumValue {
	pub name: String,
	pub doc: String,
	pub num: String,
}

fn get_false() -> bool { false }

pub fn to_pascal_case(text: &str) -> String {
	let mut s = String::with_capacity(text.len());
	let mut uppercase = true;
	for c in text.chars() {
		if c == '_' {
			uppercase = true;
		} else if uppercase {
			s.push(c.to_uppercase().next().unwrap());
			uppercase = false;
		} else {
			s.push(c);
		}
	}
	s
}

pub fn to_snake_case(text: &str) -> String {
	let mut s = String::with_capacity(text.len());
	for c in text.chars() {
		if c.is_uppercase() {
			if !s.is_empty() {
				s.push('_');
			}
			s.push_str(&c.to_lowercase().to_string());
		} else {
			s.push(c);
		}
	}
	s
}

pub fn is_ref_type(s: &str) -> bool {
	if s.starts_with("Option<") {
		is_ref_type(&s[7..s.len() - 1])
	} else {
		!(s == "bool"
			|| s.starts_with('i')
			|| s.starts_with('u')
			|| s.starts_with('f')
			|| s.ends_with("Id")
			|| s.ends_with("Type")
			|| s.ends_with("Mode"))
	}
}

/// If `is_ref` is `true`, you get e.g. `&str` instead of `String`.
pub fn convert_type(t: &str, is_ref: bool) -> String {
	if t.ends_with("[]") {
		let inner = &t[..(t.len() - 2)];
		if is_ref {
			return format!("&[{}]", convert_type(inner, is_ref));
		} else {
			return format!("Vec<{}>", convert_type(inner, is_ref));
		}
	}
	if t.ends_with('?') {
		let inner = &t[..(t.len() - 1)];
		return format!("Option<{}>", convert_type(inner, is_ref));
	}
	if t.ends_with("T") {
		return convert_type(&t[..(t.len() - 1)], is_ref);
	}

	if t == "str" || t == "string" {
		if is_ref {
			String::from("&str")
		} else {
			String::from("String")
		}
	} else if t == "byte" {
		String::from("u8")
	} else if t == "ushort" {
		String::from("u16")
	} else if t == "int" {
		String::from("i32")
	} else if t == "uint" {
		String::from("u32")
	} else if t == "float" {
		String::from("f32")
	} else if t == "long" {
		String::from("i64")
	} else if t == "ulong" {
		String::from("u64")
	} else if t == "ushort" {
		String::from("u16")
	} else if t == "DateTime" {
		String::from("DateTime<Utc>")
	} else if t.starts_with("Duration") {
		String::from("Duration")
	} else if t == "ClientUid" {
		if is_ref {
			String::from("UidRef")
		} else {
			String::from("Uid")
		}
	} else if t == "TalkPowerRequest" && is_ref {
		String::from("&TalkPowerRequest")
	} else if t == "Ts3ErrorCode" {
		String::from("Error")
	} else if t == "PermissionId" {
		String::from("Permission")
	} else if t == "Uid" && is_ref {
		String::from("UidRef")
	} else {
		t.into()
	}
}

/// Prepend `/// ` to each line of a string.
pub fn doc_comment(s: &str) -> String {
	s.lines().map(|l| format!("/// {}\n", l)).collect()
}

/// Indent a string by a given count using tabs.
pub fn indent<S: AsRef<str>>(s: S, count: usize) -> String {
	let sref = s.as_ref();
	let line_count = sref.lines().count();
	let mut result = String::with_capacity(sref.len() + line_count * count * 4);
	for l in sref.lines() {
		if !l.is_empty() {
			result.push_str(
				std::iter::repeat("\t")
					.take(count)
					.collect::<String>()
					.as_str(),
			);
		}
		result.push_str(l);
		result.push('\n');
	}
	result
}

/// Unindent a string by a given count of tabs.
pub fn unindent(mut s: &mut String) {
	std::mem::swap(&mut s.replace("\n\t", "\n"), &mut s);
	if s.get(0..1) == Some("\t") {
		s.remove(0);
	}
}