Position

Struct Position 

Source
pub struct Position<'a> {
    pub path: &'a Path,
    pub src: &'a str,
    pub line: usize,
    pub char: usize,
}

Fields§

§path: &'a Path§src: &'a str§line: usize§char: usize

Implementations§

Source§

impl<'a> Position<'a>

Source

pub fn error(&self, message: String) -> Error<'a>

Examples found in repository?
examples/network.rs (line 15)
13    fn from_value(value: &'b str, position: &'b maddi_xml::Position<'a>) -> Result<'a, Self> {
14        let Ok(ip) = value.parse::<Ipv4Addr>() else {
15            return Err(position.error("expected an ip address".into()));
16        };
17        Ok(IP(ip))
18    }
19}
20
21/// This struct represents the whole configuration file.
22#[derive(Debug)]
23struct Config {
24    servers: Vec<Server>,
25}
26
27impl<'a, 'b> FromElement<'a, 'b> for Config {
28    fn from_element(element: &'b Element<'a>) -> Result<'a, Self> {
29        Ok(Config {
30            servers: element.children("server").collect::<Result<_>>()?,
31        })
32    }
33}
34
35/// This represents a theoretical server that we want to
36/// list information about in our config.
37#[derive(Debug)]
38struct Server {
39    name: String,
40    ip: Ipv4Addr,
41    aliases: Vec<Alias>,
42}
43
44impl<'a, 'b> FromElement<'a, 'b> for Server {
45    fn from_element(element: &'b Element<'a>) -> Result<'a, Self> {
46        Ok(Server {
47            name: element.attribute("name")?,
48            ip: element.attribute::<IP>("ip")?.0,
49            aliases: element.children("alias").collect::<Result<Vec<_>>>()?,
50        })
51    }
52}
53
54/// As alias that the server can be referred to by.
55#[derive(Debug)]
56struct Alias(String);
57
58impl<'a, 'b> FromElement<'a, 'b> for Alias {
59    fn from_element(element: &'b Element<'a>) -> Result<'a, Self> {
60        match element.contents.as_slice() {
61            [] => Err(element.position.error("alias cannot be empty".into())),
62            [Content::Text(alias)] => Ok(Alias(alias.clone())),
63            _ => Err(element
64                .position
65                .error("expected a single alias name".into())),
66        }
67    }
68}
69
70fn main() {
71    // The configuration we're going to parse
72    let config = r#"
73        <config>
74            <server name="localhost" ip="127.0.0.1">
75                <alias>local</alias>
76                <alias>me</alias>
77            </server>
78            <server name="example" ip="10.0.0.101">
79                <alias>example.com</alias>
80            </server>
81        </config>
82    "#;
83    // The parser state machine we'll use to do the work
84    let mut parser = Parser::new(Path::new("example.xml"), config);
85    // Parsing a piece of content from the top of the file.
86    // Content is either a piece of non-xml text or an xml element.
87    match parser.parse::<Option<Result<Content>>>() {
88        Some(Ok(content)) => println!("{content:?}"),
89        Some(Err(e)) => println!("{e}"),
90        None => {
91            let error = parser
92                .position
93                .error("Could not find root config node".into());
94            println!("{error}");
95        }
96    }
97}

Trait Implementations§

Source§

impl<'a> Clone for Position<'a>

Source§

fn clone(&self) -> Position<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Position<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Position<'a>

§

impl<'a> RefUnwindSafe for Position<'a>

§

impl<'a> Send for Position<'a>

§

impl<'a> Sync for Position<'a>

§

impl<'a> Unpin for Position<'a>

§

impl<'a> UnwindSafe for Position<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.