Struct robotxt::Robots

source ·
pub struct Robots { /* private fields */ }
Expand description

The Robots struct represents the set of directives related to the specific user-agent in the provided robots.txt file.

Implementations§

source§

impl Robots

source

pub fn from_bytes(robots: &[u8], user_agent: &str) -> Self

Creates a new Robots from the byte slice.

use robotxt::Robots;

let txt = r#"
    User-Agent: foobot
    Disallow: *
    Allow: /example/
    Disallow: /example/nope.txt
"#.as_bytes();

let r = Robots::from_bytes(txt, "foobot");
assert!(r.is_allowed("/example/yeah.txt"));
assert!(!r.is_allowed("/example/nope.txt"));
assert!(!r.is_allowed("/invalid/path.txt"));
source

pub fn from_reader<R: Read>( reader: R, user_agent: &str ) -> Result<Self, IoError>

Creates a new Robots from the generic reader.

use robotxt::Robots;

// Let's pretend it's something that actually needs a reader.
// The std::io::Read trait is implemented for &[u8].
let reader = r#"
    User-Agent: foobot
    Disallow: *
    Allow: /example/
    Disallow: /example/nope.txt
"#.as_bytes();

let r = Robots::from_reader(reader, "foobot").unwrap();
assert!(r.is_allowed("/example/yeah.txt"));
assert!(!r.is_allowed("/example/nope.txt"));
assert!(!r.is_allowed("/invalid/path.txt"));
source

pub fn from_access(access: AccessResult<'_>, user_agent: &str) -> Self

Creates a new Robots from the AccessResult.

use robotxt::{AccessResult, Robots};

let r = Robots::from_access(AccessResult::Redirect, "foobot");
assert!(r.is_allowed("/example/yeah.txt"));
assert!(r.is_allowed("/example/nope.txt"));

let r = Robots::from_access(AccessResult::Unavailable, "foobot");
assert!(r.is_allowed("/example/yeah.txt"));
assert!(r.is_allowed("/example/nope.txt"));

let r = Robots::from_access(AccessResult::Unreachable, "foobot");
assert!(!r.is_allowed("/example/yeah.txt"));
assert!(!r.is_allowed("/example/nope.txt"));
source

pub fn from_always(always: bool, user_agent: &str) -> Self

Creates a new Robots from the global rule.

use robotxt::Robots;

let r = Robots::from_always(true, "foobot");
assert!(r.is_allowed("/example/yeah.txt"));
assert!(r.is_allowed("/example/nope.txt"));
source§

impl Robots

source

pub fn is_allowed(&self, path: &str) -> bool

Returns true if the path is allowed for the user-agent. NOTE: Expects relative path.

use robotxt::Robots;

let txt = r#"
    User-Agent: foobot
    Disallow: *
    Allow: /example/
    Disallow: /example/nope.txt
"#.as_bytes();

let r = Robots::from_bytes(txt, "foobot");
assert!(r.is_allowed("/example/yeah.txt"));
assert!(!r.is_allowed("/example/nope.txt"));
assert!(!r.is_allowed("/invalid/path.txt"));
source

pub fn is_always(&self) -> Option<bool>

Returns Some(_) if the site is fully allowed or disallowed.

use robotxt::Robots;

let r = Robots::from_always(true, "foobot");
assert_eq!(r.is_always(), Some(true));

let r = Robots::from_always(false, "foobot");
assert_eq!(r.is_always(), Some(false));
source

pub fn user_agent(&self) -> &str

Returns the longest matching user-agent.

use robotxt::Robots;

let txt = r#"
    User-Agent: foo
    User-Agent: foobot
"#.as_bytes();

let r = Robots::from_bytes(txt, "foobot-search");
assert_eq!(r.user_agent(), "foobot");
source

pub fn crawl_delay(&self) -> Option<Duration>

Returns the crawl-delay of the user-agent if specified.

use std::time::Duration;
use robotxt::Robots;

let txt = r#"
    User-Agent: foobot
    Crawl-Delay: 5
"#.as_bytes();

let r = Robots::from_bytes(txt, "foobot");
assert_eq!(r.crawl_delay(), Some(Duration::from_secs(5)));
source

pub fn sitemaps(&self) -> &Vec<Url>

Returns all collected sitemaps.

use robotxt::Robots;

let txt = r#"
    Sitemap: https://example.com/sitemap_1.xml
    Sitemap: https://example.com/sitemap_2.xml
"#.as_bytes();

let r = Robots::from_bytes(txt, "foobot");
assert_eq!(r.sitemaps().len(), 2);

Trait Implementations§

source§

impl Clone for Robots

source§

fn clone(&self) -> Robots

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 Robots

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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.