pub fn parse(source: &mut dyn ISource) -> Result<Node, String>Expand description
Examples found in repository?
examples/bencode_torrent_to_toml.rs (line 14)
12fn process_torrent_file(file_path: &str) -> Result<(), String> {
13 let mut source = FileSource::new(file_path).map_err(|e| e.to_string())?;
14 let node = parse(&mut source).map_err(|e| e.to_string())?;
15 let mut destination = FileDestination::new(
16 Path::new(file_path)
17 .with_extension("toml")
18 .to_string_lossy()
19 .as_ref(),
20 )
21 .map_err(|e| e.to_string())?;
22 to_toml(&node, &mut destination)?;
23 Ok(())
24}More examples
examples/bencode_torrent_to_yaml.rs (line 14)
12fn process_torrent_file(file_path: &str) -> Result<(), String> {
13 let mut source = FileSource::new(file_path).map_err(|e| e.to_string())?;
14 let node = parse(&mut source).map_err(|e| e.to_string())?;
15 let mut destination = FileDestination::new(
16 Path::new(file_path)
17 .with_extension("yaml")
18 .to_string_lossy()
19 .as_ref(),
20 )
21 .map_err(|e| e.to_string())?;
22 to_yaml(&node, &mut destination)?;
23 Ok(())
24}examples/bencode_torrent_to_xml.rs (line 14)
12fn process_torrent_file(file_path: &str) -> Result<(), String> {
13 let mut source = FileSource::new(file_path).map_err(|e| e.to_string())?;
14 let node = parse(&mut source).map_err(|e| e.to_string())?;
15 let mut destination = FileDestination::new(
16 Path::new(file_path)
17 .with_extension("xml")
18 .to_string_lossy()
19 .as_ref(),
20 )
21 .map_err(|e| e.to_string())?;
22 to_xml(&node, &mut destination).map_err(|e| e.to_string())?;
23 Ok(())
24}examples/bencode_torrent_to_json.rs (line 14)
12fn process_torrent_file(file_path: &str) -> Result<(), String> {
13 let mut source = FileSource::new(file_path).map_err(|e| e.to_string())?;
14 let node = parse(&mut source).map_err(|e| e.to_string())?;
15 let mut destination = FileDestination::new(
16 Path::new(file_path)
17 .with_extension("json")
18 .to_string_lossy()
19 .as_ref(),
20 )
21 .map_err(|e| e.to_string())?;
22 to_json(&node, &mut destination).map_err(|e| e.to_string())?;
23 Ok(())
24}examples/bencode_fibonacci.rs (line 24)
16fn read_sequence(file_path: &Path) -> Result<Node, String> {
17 // Initialize with the default sequence if the file doesn't exist
18 if !file_path.exists() {
19 return Ok(Node::List([Node::Integer(1), Node::Integer(1)].into()));
20 }
21
22 // Try to open and parse the existing file
23 match FileSource::new(file_path) {
24 Ok(mut file) => match parse(&mut file) {
25 Ok(Node::List(list)) => Ok(Node::List(list)),
26 Ok(_) => Err("Invalid file format: expected a list".to_string()),
27 Err(e) => Err(e),
28 },
29 Err(e) => Err(format!("Failed to open file: {}", e)),
30 }
31}examples/bencode_error_handling.rs (line 78)
70fn demonstrate_file_errors() {
71 println!("--- File I/O Error Handling ---");
72
73 // Try to open non-existent file
74 let nonexistent_path = "nonexistent_file.bencode";
75 match FileSource::new(nonexistent_path) {
76 Ok(mut source) => {
77 println!("File opened successfully");
78 match parse(&mut source) {
79 Ok(node) => println!("Parsed: {}", node),
80 Err(e) => println!("Parse error: {}", e),
81 }
82 }
83 Err(e) => println!("Failed to open '{}': {}", nonexistent_path, e),
84 }
85
86 // Try to open invalid path
87 let invalid_path = "";
88 match FileSource::new(invalid_path) {
89 Ok(_) => println!("Unexpected success with empty path"),
90 Err(e) => println!("Failed to open empty path: {}", e),
91 }
92
93 // Demonstrate safe file reading with proper error propagation
94 match safe_read_file("example.bencode") {
95 Ok(node) => println!("Successfully read file: {}", node),
96 Err(e) => println!("Failed to read file: {}", e),
97 }
98
99 println!();
100}
101
102/// Helper function demonstrating proper error propagation
103fn safe_read_file(path: &str) -> Result<Node, String> {
104 let mut source =
105 FileSource::new(path).map_err(|e| format!("Cannot open file '{}': {}", path, e))?;
106
107 let node = parse(&mut source).map_err(|e| format!("Cannot parse file '{}': {}", path, e))?;
108
109 Ok(node)
110}Additional examples can be found in: