Skip to content

Commit 04c2b24

Browse files
Jonathan D.A. Jewellclaude
andcommitted
Implement format converters and uptime tracking
- yaml.rs: Use serde_yaml for actual YAML parsing/serialization - xml.rs: Use quick_xml for actual XML parsing/serialization - toml.rs: Use toml crate for actual TOML parsing/serialization - http.rs: Track actual server uptime via HealthChecker - monitoring.rs: Add uptime_seconds() method to HealthChecker Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent df65208 commit 04c2b24

File tree

5 files changed

+25
-24
lines changed

5 files changed

+25
-24
lines changed

server/src/formats/toml.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
//! TOML format support for document conversion
22
3-
use anyhow::Result;
3+
use anyhow::{Result, Context};
44
use serde_json::Value;
55

66
/// Convert TOML to JSON
7-
pub fn toml_to_json(toml: &str) -> Result<String> {
8-
// Placeholder: use toml crate in production
9-
let value: Value = serde_json::json!({
10-
"toml_content": toml
11-
});
7+
pub fn toml_to_json(toml_str: &str) -> Result<String> {
8+
let value: Value = toml::from_str(toml_str)
9+
.context("Failed to parse TOML")?;
1210
Ok(serde_json::to_string_pretty(&value)?)
1311
}
1412

1513
/// Convert JSON to TOML
1614
pub fn json_to_toml(json: &str) -> Result<String> {
1715
let value: Value = serde_json::from_str(json)?;
18-
// Placeholder: use toml crate for proper serialization
19-
Ok(format!("# TOML\n[data]\ncontent = '''{}'''", value))
16+
toml::to_string_pretty(&value)
17+
.context("Failed to serialize to TOML")
2018
}
2119

2220
/// Validate TOML syntax

server/src/formats/xml.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
//! XML format support for document conversion
22
3-
use anyhow::Result;
3+
use anyhow::{Result, Context};
44
use serde_json::Value;
5+
use quick_xml::de::from_str as xml_from_str;
6+
use quick_xml::se::to_string as xml_to_string;
57

68
/// Convert XML to JSON
79
pub fn xml_to_json(xml: &str) -> Result<String> {
8-
// Placeholder: use quick-xml or serde-xml-rs in production
9-
let value: Value = serde_json::json!({
10-
"xml_root": {
11-
"content": xml
12-
}
13-
});
10+
let value: Value = xml_from_str(xml)
11+
.context("Failed to parse XML")?;
1412
Ok(serde_json::to_string_pretty(&value)?)
1513
}
1614

1715
/// Convert JSON to XML
1816
pub fn json_to_xml(json: &str) -> Result<String> {
1917
let value: Value = serde_json::from_str(json)?;
20-
Ok(format!("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>\n {}\n</root>",
21-
serde_json::to_string_pretty(&value)?))
18+
let xml = xml_to_string(&value)
19+
.context("Failed to serialize to XML")?;
20+
Ok(format!("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n{}", xml))
2221
}
2322

2423
/// Validate XML syntax

server/src/formats/yaml.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,14 @@ use serde_json::Value;
77

88
/// Convert YAML to JSON
99
pub fn yaml_to_json(yaml: &str) -> Result<String> {
10-
// In production, use serde_yaml crate
11-
// Placeholder implementation
12-
let value: Value = serde_json::from_str(&format!(r#"{{"yaml_content": "{}"}}"#, yaml.escape_default()))?;
10+
let value: Value = serde_yaml::from_str(yaml)?;
1311
Ok(serde_json::to_string_pretty(&value)?)
1412
}
1513

1614
/// Convert JSON to YAML
1715
pub fn json_to_yaml(json: &str) -> Result<String> {
18-
// In production, use serde_yaml crate
19-
// Placeholder implementation
2016
let value: Value = serde_json::from_str(json)?;
21-
Ok(format!("# YAML representation\n{}", serde_json::to_string_pretty(&value)?))
17+
Ok(serde_yaml::to_string(&value)?)
2218
}
2319

2420
/// Convert YAML to Markdown

server/src/http.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ async fn validate_document(
177177
async fn get_stats(State(state): State<Arc<ServerState>>) -> Json<ServerStats> {
178178
Json(ServerStats {
179179
document_count: state.documents.count(),
180-
uptime_seconds: 0, // TODO: Track actual uptime
180+
uptime_seconds: state.health_checker.uptime_seconds(),
181181
version: env!("CARGO_PKG_VERSION").to_string(),
182182
})
183183
}

server/src/monitoring.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,14 @@ impl HealthChecker {
255255
}
256256
}
257257

258+
/// Get uptime in seconds
259+
pub fn uptime_seconds(&self) -> u64 {
260+
Utc::now()
261+
.signed_duration_since(self.start_time)
262+
.num_seconds()
263+
.max(0) as u64
264+
}
265+
258266
/// Perform health check
259267
pub async fn check(&self, metrics: &Metrics) -> HealthStatus {
260268
let mut checks = HashMap::new();

0 commit comments

Comments
 (0)