Finding an XML element value using Rust

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












The following is a struct to extract a value between named XML element tags. Though it works, I feel there is functional approach in Rust that I am missing. Any other tips on Rust preferred styling are also appreciated.



pub struct XmlDoc 
doc: String,


impl XmlDoc
pub fn new(doc: String) -> XmlDoc
XmlDoc doc

pub fn get_value(&self, element: &str) -> String
let begin_tag = format!("<>", element);
let end_tag = format!("</>", element);
let value_begin = self.doc.find::<&str>(begin_tag.as_str()).unwrap();
let value_end = self.doc.find::<&str>(end_tag.as_str()).unwrap();
self.doc[value_begin + begin_tag.len()..value_end].to_string()



mod tests
use super::*;

#[test]
fn get_value()
let xml = XmlDoc::new("<element>value</element>".to_string());
let value = xml.get_value("element");
assert_eq!(value, "value".to_string(), "", value);




Note, I understand that XML libraries for Rust already exist. This is intended as a learning tool for working with string manipulations inside vanilla Rust.



Edit:
I thought using regex to parse XML may be a good idea. I was wrong.







share|improve this question





















  • I understand a Regex library may be a better option — not for parsing XML, please. I recommend my own Rust XML library, sxd-document.
    – Shepmaster
    Feb 24 at 19:11






  • 1




    a little too C++ currently — can you explain what about this feels like C++?
    – Shepmaster
    Feb 24 at 19:12










  • @Shepmaster As a style, more declarative than functional. I've seen that Rust provides a more concise syntax, in general, for most tasks.
    – John Stritenberger
    Feb 24 at 19:24
















up vote
1
down vote

favorite












The following is a struct to extract a value between named XML element tags. Though it works, I feel there is functional approach in Rust that I am missing. Any other tips on Rust preferred styling are also appreciated.



pub struct XmlDoc 
doc: String,


impl XmlDoc
pub fn new(doc: String) -> XmlDoc
XmlDoc doc

pub fn get_value(&self, element: &str) -> String
let begin_tag = format!("<>", element);
let end_tag = format!("</>", element);
let value_begin = self.doc.find::<&str>(begin_tag.as_str()).unwrap();
let value_end = self.doc.find::<&str>(end_tag.as_str()).unwrap();
self.doc[value_begin + begin_tag.len()..value_end].to_string()



mod tests
use super::*;

#[test]
fn get_value()
let xml = XmlDoc::new("<element>value</element>".to_string());
let value = xml.get_value("element");
assert_eq!(value, "value".to_string(), "", value);




Note, I understand that XML libraries for Rust already exist. This is intended as a learning tool for working with string manipulations inside vanilla Rust.



Edit:
I thought using regex to parse XML may be a good idea. I was wrong.







share|improve this question





















  • I understand a Regex library may be a better option — not for parsing XML, please. I recommend my own Rust XML library, sxd-document.
    – Shepmaster
    Feb 24 at 19:11






  • 1




    a little too C++ currently — can you explain what about this feels like C++?
    – Shepmaster
    Feb 24 at 19:12










  • @Shepmaster As a style, more declarative than functional. I've seen that Rust provides a more concise syntax, in general, for most tasks.
    – John Stritenberger
    Feb 24 at 19:24












up vote
1
down vote

favorite









up vote
1
down vote

favorite











The following is a struct to extract a value between named XML element tags. Though it works, I feel there is functional approach in Rust that I am missing. Any other tips on Rust preferred styling are also appreciated.



pub struct XmlDoc 
doc: String,


impl XmlDoc
pub fn new(doc: String) -> XmlDoc
XmlDoc doc

pub fn get_value(&self, element: &str) -> String
let begin_tag = format!("<>", element);
let end_tag = format!("</>", element);
let value_begin = self.doc.find::<&str>(begin_tag.as_str()).unwrap();
let value_end = self.doc.find::<&str>(end_tag.as_str()).unwrap();
self.doc[value_begin + begin_tag.len()..value_end].to_string()



mod tests
use super::*;

#[test]
fn get_value()
let xml = XmlDoc::new("<element>value</element>".to_string());
let value = xml.get_value("element");
assert_eq!(value, "value".to_string(), "", value);




Note, I understand that XML libraries for Rust already exist. This is intended as a learning tool for working with string manipulations inside vanilla Rust.



Edit:
I thought using regex to parse XML may be a good idea. I was wrong.







share|improve this question













The following is a struct to extract a value between named XML element tags. Though it works, I feel there is functional approach in Rust that I am missing. Any other tips on Rust preferred styling are also appreciated.



pub struct XmlDoc 
doc: String,


impl XmlDoc
pub fn new(doc: String) -> XmlDoc
XmlDoc doc

pub fn get_value(&self, element: &str) -> String
let begin_tag = format!("<>", element);
let end_tag = format!("</>", element);
let value_begin = self.doc.find::<&str>(begin_tag.as_str()).unwrap();
let value_end = self.doc.find::<&str>(end_tag.as_str()).unwrap();
self.doc[value_begin + begin_tag.len()..value_end].to_string()



mod tests
use super::*;

#[test]
fn get_value()
let xml = XmlDoc::new("<element>value</element>".to_string());
let value = xml.get_value("element");
assert_eq!(value, "value".to_string(), "", value);




Note, I understand that XML libraries for Rust already exist. This is intended as a learning tool for working with string manipulations inside vanilla Rust.



Edit:
I thought using regex to parse XML may be a good idea. I was wrong.









share|improve this question












share|improve this question




share|improve this question








edited Feb 25 at 15:43
























asked Feb 24 at 19:09









John Stritenberger

21419




21419











  • I understand a Regex library may be a better option — not for parsing XML, please. I recommend my own Rust XML library, sxd-document.
    – Shepmaster
    Feb 24 at 19:11






  • 1




    a little too C++ currently — can you explain what about this feels like C++?
    – Shepmaster
    Feb 24 at 19:12










  • @Shepmaster As a style, more declarative than functional. I've seen that Rust provides a more concise syntax, in general, for most tasks.
    – John Stritenberger
    Feb 24 at 19:24
















  • I understand a Regex library may be a better option — not for parsing XML, please. I recommend my own Rust XML library, sxd-document.
    – Shepmaster
    Feb 24 at 19:11






  • 1




    a little too C++ currently — can you explain what about this feels like C++?
    – Shepmaster
    Feb 24 at 19:12










  • @Shepmaster As a style, more declarative than functional. I've seen that Rust provides a more concise syntax, in general, for most tasks.
    – John Stritenberger
    Feb 24 at 19:24















I understand a Regex library may be a better option — not for parsing XML, please. I recommend my own Rust XML library, sxd-document.
– Shepmaster
Feb 24 at 19:11




I understand a Regex library may be a better option — not for parsing XML, please. I recommend my own Rust XML library, sxd-document.
– Shepmaster
Feb 24 at 19:11




1




1




a little too C++ currently — can you explain what about this feels like C++?
– Shepmaster
Feb 24 at 19:12




a little too C++ currently — can you explain what about this feels like C++?
– Shepmaster
Feb 24 at 19:12












@Shepmaster As a style, more declarative than functional. I've seen that Rust provides a more concise syntax, in general, for most tasks.
– John Stritenberger
Feb 24 at 19:24




@Shepmaster As a style, more declarative than functional. I've seen that Rust provides a more concise syntax, in general, for most tasks.
– John Stritenberger
Feb 24 at 19:24















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188284%2ffinding-an-xml-element-value-using-rust%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188284%2ffinding-an-xml-element-value-using-rust%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation