pub struct PathResolver { /* private fields */ }
Expand description

A resolver that keeps relative paths on a stack for recursive include process.

Examples

use plantuml_parser::PathResolver;
use std::path::PathBuf;

let resolver_0 = PathResolver::new("./dir-1/x1.puml");
assert_eq!(resolver_0.build()?, PathBuf::from("dir-1/x1.puml"));
assert_eq!(resolver_0.build_without_normalize()?, PathBuf::from("./dir-1/x1.puml"));

// For example, `./dir-1/x1.puml` includes `./dir-2/x2.puml`.
let mut resolver_0_0 = resolver_0.clone();
resolver_0_0.add("./dir-2/x2.puml".into());
assert_eq!(resolver_0_0.build()?, PathBuf::from("dir-1/dir-2/x2.puml"));
assert_eq!(resolver_0_0.build_without_normalize()?, PathBuf::from("./dir-1/dir-2/x2.puml"));

// For example, `./dir-1/x1.puml` includes `./dir-3/x3.puml`.
let mut resolver_0_1 = resolver_0.clone();
resolver_0_1.add("./dir-3/x3.puml".into());
assert_eq!(resolver_0_1.build()?, PathBuf::from("dir-1/dir-3/x3.puml"));
assert_eq!(resolver_0_1.build_without_normalize()?, PathBuf::from("./dir-1/dir-3/x3.puml"));

// For example, `./dir-1/dir-3/x3.puml` includes `../../dir-4/x4.puml`.
let mut resolver_0_1_0 = resolver_0_1.clone();
resolver_0_1_0.add("../../dir-4/x4.puml".into());
assert_eq!(resolver_0_1_0.build()?, PathBuf::from("dir-4/x4.puml"));
assert_eq!(resolver_0_1_0.build_without_normalize()?, PathBuf::from("./dir-1/dir-3/../../dir-4/x4.puml"));

Implementations§

source§

impl PathResolver

source

pub fn new<T>(path: T) -> Self
where T: Into<PathBuf>,

Creates a new PathResolver.

  • path - A base path.
Examples
let _ = PathResolver::new("path");
source

pub fn add(&mut self, path: PathBuf)

Adds a PathBuf onto an inner stack.

  • path - A new path.
Examples
let mut resolver = PathResolver::new("dir/file");
resolver.add("file-2".into());
assert_eq!(resolver.build()?, PathBuf::from("dir/file-2"));
source

pub fn build(&self) -> Result<PathBuf, Error>

Returns the resolved normalized path. All PathBufs in the stack are treated as paths relative to the file.

Examples
let mut resolver = PathResolver::new("dir-1/file-1");
assert_eq!(resolver.build()?, PathBuf::from("dir-1/file-1"));
resolver.add("../dir-2/file-2".into());
assert_eq!(resolver.build()?, PathBuf::from("dir-2/file-2"));
resolver.add("../dir-3/file-3".into());
assert_eq!(resolver.build()?, PathBuf::from("dir-3/file-3"));
source

pub fn build_without_normalize(&self) -> Result<PathBuf, Error>

Returns the resolved the path not normalized.

Examples
let mut resolver = PathResolver::new("dir-1/file-1");
assert_eq!(
    resolver.build_without_normalize()?,
    PathBuf::from("dir-1/file-1")
);

resolver.add("../dir-2/file-2".into());
assert_eq!(
    resolver.build_without_normalize()?,
    PathBuf::from("dir-1/../dir-2/file-2")
);

resolver.add("../dir-3/file-3".into());
assert_eq!(
    resolver.build_without_normalize()?,
    PathBuf::from("dir-1/../dir-2/../dir-3/file-3")
);

Trait Implementations§

source§

impl Clone for PathResolver

source§

fn clone(&self) -> PathResolver

Returns a copy 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 Debug for PathResolver

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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,

§

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>,

§

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>,

§

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.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more