use serde::Deserialize;
use std::path::Path;
#[derive(Debug, Clone, Deserialize)]
pub struct CliConfig {
#[allow(dead_code)]
pub room_id: Option<u64>,
pub cookie: Option<String>,
pub message_types: Option<Vec<String>>,
}
impl CliConfig {
pub fn from_file(path: &str) -> Result<Self, Box<dyn std::error::Error>> {
let content = std::fs::read_to_string(path)?;
let config: CliConfig = toml::from_str(&content)?;
Ok(config)
}
}
#[derive(Debug, Clone)]
pub enum CookieSource {
JsonFile(String),
JsonString(String),
InlineString(String),
}
impl CookieSource {
pub fn parse(input: &str) -> Self {
let trimmed = input.trim();
if trimmed.starts_with('{') {
CookieSource::JsonString(input.to_string())
}
else if trimmed.contains('=') {
CookieSource::InlineString(input.to_string())
}
else {
CookieSource::JsonFile(input.to_string())
}
}
pub fn get_content(&self) -> Result<String, Box<dyn std::error::Error>> {
match self {
CookieSource::JsonFile(path) => {
if !Path::new(path).exists() {
return Err(format!("Cookie 文件不存在: {}", path).into());
}
Ok(std::fs::read_to_string(path)?)
}
CookieSource::JsonString(json) => Ok(json.clone()),
CookieSource::InlineString(inline) => {
let mut json_obj = serde_json::Map::new();
for part in inline.split(';') {
let part = part.trim();
if let Some((key, value)) = part.split_once('=') {
json_obj.insert(
key.trim().to_string(),
serde_json::Value::String(value.trim().to_string()),
);
}
}
Ok(serde_json::to_string(&json_obj)?)
}
}
}
}