Bots

Struct Bots 

Source
pub struct Bots { /* private fields */ }
Expand description

Wrapper struct to maintain bot regular expression patterns

§Example

use isbot::Bots;

let bots = Bots::default();

Implementations§

Source§

impl Bots

Source

pub fn new(bot_entries: &str) -> Self

Constructs a new instance with bot user-agent regular expression entries delimited by a newline

All user-agent regular expressions are converted to lowercase.

§Example
use isbot::Bots;

let custom_user_agent_patterns = r#"
^Googlebot-Image/
bingpreview/"#;
let bots = Bots::new(custom_user_agent_patterns);

assert!(bots.is_bot("Googlebot-Image/1.0"));
assert!(bots.is_bot("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534+ (KHTML, like Gecko) BingPreview/1.0b"));
assert!(!bots.is_bot("Googlebot"));
Source

pub fn is_bot(&self, user_agent: &str) -> bool

Returns true the user-agent is a known bot.

The user-agent comparison is done using lowercase.

§Example
use isbot::Bots;

let bots = Bots::default();

assert!(bots.is_bot("Googlebot/2.1 (+http://www.google.com/bot.html)"));
assert!(!bots.is_bot("Dalvik/2.1.0 (Linux; U; Android 8.0.0; SM-G930F Build/R16NW)"));
Source

pub fn append(&mut self, bots: &[&str])

Appends bot user-agent regular expressions patterns.

Duplicates are ignored.

§Example
use isbot::Bots;

let mut bots = Bots::default();
assert!(!bots.is_bot("Mozilla/5.0 (CustomNewTestB0T /1.2)"));
bots.append(&[r"CustomNewTestB0T\s/\d\.\d"]);
assert!(bots.is_bot("Mozilla/5.0 (CustomNewTestB0T /1.2)"));

let new_bot_patterns = vec!["GoogleMetaverse", "^Special/"];
bots.append(&new_bot_patterns);
assert!(bots.is_bot("Mozilla/5.0 (GoogleMetaverse/1.0)"));
Source

pub fn remove(&mut self, bots: &[&str])

Removes bot user-agent regular expressions.

§Example
use isbot::Bots;

let mut bots = Bots::default();


assert!(bots.is_bot("Chrome-Lighthouse"));
bots.remove(&["Chrome-Lighthouse"]);
assert!(!bots.is_bot("Chrome-Lighthouse"));

let bot_patterns_to_remove = vec!["bingpreview/", "Google Favicon"];
bots.remove(&bot_patterns_to_remove);
assert!(!bots.is_bot("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534+ (KHTML, like Gecko) BingPreview/1.0b"));
assert!(!bots.is_bot("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36 Google Favicon"));

Trait Implementations§

Source§

impl Debug for Bots

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Bots

Source§

fn default() -> Self

Constructs a new instance with default user-agent patterns.

§Example
use isbot::Bots;

let bots = Bots::default();

assert!(bots.is_bot("Googlebot"));

Auto Trait Implementations§

§

impl Freeze for Bots

§

impl RefUnwindSafe for Bots

§

impl Send for Bots

§

impl Sync for Bots

§

impl Unpin for Bots

§

impl UnwindSafe for Bots

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