use serde::{Deserialize, Serialize};
use serde_json::Value;
use super::Commit;
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Content {
#[serde(default, rename = "type")]
pub kind: String,
#[serde(default)]
pub sha: String,
#[serde(default)]
pub name: String,
#[serde(default)]
pub path: String,
#[serde(default)]
pub content: Value,
#[serde(default)]
pub latest_commit: Option<Commit>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ContentEntry {
#[serde(default, rename = "type")]
pub kind: String,
#[serde(default)]
pub sha: String,
#[serde(default)]
pub name: String,
#[serde(default)]
pub path: String,
}
impl Content {
pub fn is_dir(&self) -> bool {
self.kind == "dir"
}
pub fn entries(&self) -> Vec<ContentEntry> {
self.content
.get("entries")
.cloned()
.and_then(|v| serde_json::from_value(v).ok())
.unwrap_or_default()
}
pub fn size(&self) -> Option<i64> {
self.content.get("size").and_then(Value::as_i64)
}
pub fn bytes(&self) -> Option<Vec<u8>> {
let data = self.content.get("data")?.as_str()?;
match self.content.get("encoding").and_then(Value::as_str) {
Some("base64") | None => decode_base64(data),
Some("utf8" | "utf-8" | "text") => Some(data.as_bytes().to_vec()),
Some(_) => None,
}
}
}
pub fn decode_base64(input: &str) -> Option<Vec<u8>> {
fn value(byte: u8) -> Option<u32> {
Some(match byte {
b'A'..=b'Z' => byte - b'A',
b'a'..=b'z' => byte - b'a' + 26,
b'0'..=b'9' => byte - b'0' + 52,
b'+' | b'-' => 62,
b'/' | b'_' => 63,
_ => return None,
} as u32)
}
let clean: Vec<u8> = input
.bytes()
.filter(|b| !b.is_ascii_whitespace() && *b != b'=')
.collect();
let mut out = Vec::with_capacity(clean.len() * 3 / 4);
for chunk in clean.chunks(4) {
let mut acc = 0u32;
for (i, byte) in chunk.iter().enumerate() {
acc |= value(*byte)? << (18 - 6 * i as u32);
}
let bytes = acc.to_be_bytes();
match chunk.len() {
4 => out.extend_from_slice(&bytes[1..4]),
3 => out.extend_from_slice(&bytes[1..3]),
2 => out.push(bytes[1]),
_ => return None,
}
}
Some(out)
}
pub fn encode_base64(input: &[u8]) -> String {
const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
let mut out = String::with_capacity(input.len().div_ceil(3) * 4);
for chunk in input.chunks(3) {
let n = match chunk.len() {
3 => (chunk[0] as u32) << 16 | (chunk[1] as u32) << 8 | chunk[2] as u32,
2 => (chunk[0] as u32) << 16 | (chunk[1] as u32) << 8,
_ => (chunk[0] as u32) << 16,
};
for i in 0..4 {
if i <= chunk.len() {
out.push(ALPHABET[(n >> (18 - 6 * i) & 63) as usize] as char);
} else {
out.push('=');
}
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn base64_round_trips_every_padding_length() {
for text in ["", "a", "ab", "abc", "abcd", "# AgentNexus\n\n> δΈζ"] {
let encoded = encode_base64(text.as_bytes());
assert_eq!(decode_base64(&encoded).unwrap(), text.as_bytes(), "{text}");
}
assert_eq!(encode_base64(b"hello"), "aGVsbG8=");
assert_eq!(decode_base64("aGVs\nbG8=").unwrap(), b"hello");
assert!(decode_base64("not*base64").is_none());
}
#[test]
fn a_file_decodes_and_a_directory_lists() {
let file: Content = serde_json::from_value(json!({
"type": "file", "name": "README.md", "path": "README.md", "sha": "b72a",
"content": { "data": "IyBBZ2VudE5leHVz", "encoding": "base64", "size": 12 }
}))
.unwrap();
assert_eq!(file.bytes().unwrap(), b"# AgentNexus");
assert_eq!(file.size(), Some(12));
assert!(file.entries().is_empty());
let dir: Content = serde_json::from_value(json!({
"type": "dir", "path": "",
"content": { "entries": [
{ "type": "file", "name": "Cargo.toml", "path": "Cargo.toml", "sha": "1" },
{ "type": "dir", "name": "src", "path": "src", "sha": "2" }
] }
}))
.unwrap();
assert!(dir.is_dir());
assert_eq!(dir.entries().len(), 2);
assert_eq!(dir.entries()[1].kind, "dir");
assert!(dir.bytes().is_none());
}
}