Use pub(crate) to enforce doc(hidden).

This commit is contained in:
Sergio Benitez 2017-02-03 02:16:46 -08:00
parent bf1b9e76fd
commit aefa2f1494
17 changed files with 97 additions and 77 deletions

View File

@ -8,9 +8,8 @@ use rocket::config::Environment::*;
fn init() { fn init() {
let cwd = env::current_dir().expect("current working directory"); let cwd = env::current_dir().expect("current working directory");
let tests_dir = cwd.join("tests"); let tests_dir = cwd.join("tests");
let config_dir = tests_dir.join("Rocket.toml");
let config = Config::default_for(Development, &config_dir).unwrap(); let config = Config::build(Development).root(tests_dir).unwrap();
rocket::custom(config, true); rocket::custom(config, true);
} }

View File

@ -96,9 +96,9 @@ impl Catcher {
Catcher { code: code, handler: handler, is_default: false } Catcher { code: code, handler: handler, is_default: false }
} }
#[doc(hidden)]
#[inline(always)] #[inline(always)]
pub fn handle<'r>(&self, err: Error, req: &'r Request) -> response::Result<'r> { pub(crate) fn handle<'r>(&self, err: Error, req: &'r Request)
-> response::Result<'r> {
(self.handler)(err, req) (self.handler)(err, req)
} }
@ -107,9 +107,8 @@ impl Catcher {
Catcher { code: code, handler: handler, is_default: true, } Catcher { code: code, handler: handler, is_default: true, }
} }
#[doc(hidden)]
#[inline(always)] #[inline(always)]
pub fn is_default(&self) -> bool { pub(crate) fn is_default(&self) -> bool {
self.is_default self.is_default
} }
} }

View File

@ -1,4 +1,5 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::path::{Path, PathBuf};
use config::{Result, Config, Value, Environment}; use config::{Result, Config, Value, Environment};
use config::toml_ext::IntoValue; use config::toml_ext::IntoValue;
@ -21,6 +22,8 @@ pub struct ConfigBuilder {
pub session_key: Option<String>, pub session_key: Option<String>,
/// Any extra parameters that aren't part of Rocket's config. /// Any extra parameters that aren't part of Rocket's config.
pub extras: HashMap<String, Value>, pub extras: HashMap<String, Value>,
/// The root directory of this config.
pub root: PathBuf,
} }
impl ConfigBuilder { impl ConfigBuilder {
@ -52,6 +55,7 @@ impl ConfigBuilder {
let config = Config::new(environment) let config = Config::new(environment)
.expect("ConfigBuilder::new(): couldn't get current directory."); .expect("ConfigBuilder::new(): couldn't get current directory.");
let root_dir = PathBuf::from(config.root());
ConfigBuilder { ConfigBuilder {
environment: config.environment, environment: config.environment,
address: config.address, address: config.address,
@ -60,6 +64,7 @@ impl ConfigBuilder {
log_level: config.log_level, log_level: config.log_level,
session_key: None, session_key: None,
extras: config.extras, extras: config.extras,
root: root_dir,
} }
} }
@ -178,6 +183,25 @@ impl ConfigBuilder {
self self
} }
/// Sets the `root` in the configuration being built.
///
/// # Example
///
/// ```rust
/// # use std::path::Path;
/// use rocket::config::{Config, Environment};
///
/// let config = Config::build(Environment::Staging)
/// .root("/my_app/dir")
/// .unwrap();
///
/// assert_eq!(config.root(), Path::new("/my_app/dir"));
/// ```
pub fn root<P: AsRef<Path>>(mut self, path: P) -> Self {
self.root = path.as_ref().to_path_buf();
self
}
/// Adds an extra configuration parameter with `name` and `value` to the /// Adds an extra configuration parameter with `name` and `value` to the
/// configuration being built. The value can be any type that implements /// configuration being built. The value can be any type that implements
/// [IntoValue](/config/trait.IntoValue.html) including `&str`, `String`, /// [IntoValue](/config/trait.IntoValue.html) including `&str`, `String`,
@ -236,6 +260,7 @@ impl ConfigBuilder {
config.set_workers(self.workers); config.set_workers(self.workers);
config.set_log_level(self.log_level); config.set_log_level(self.log_level);
config.set_extras(self.extras); config.set_extras(self.extras);
config.set_root(self.root);
if let Some(key) = self.session_key { if let Some(key) = self.session_key {
config.set_session_key(key)?; config.set_session_key(key)?;

View File

@ -231,6 +231,27 @@ impl Config {
Ok(()) Ok(())
} }
/// Sets the root directory of this configuration to `root`.
///
/// # Example
///
/// ```rust
/// # use std::path::Path;
/// use rocket::config::{Config, Environment};
///
/// # use rocket::config::ConfigError;
/// # fn config_test() -> Result<(), ConfigError> {
/// let mut config = Config::new(Environment::Staging)?;
/// config.set_root("/tmp/my_app");
///
/// assert_eq!(config.root(), Path::new("/tmp/my_app"));
/// # Ok(())
/// # }
/// ```
pub fn set_root<P: AsRef<Path>>(&mut self, path: P) {
self.config_path = path.as_ref().join("Rocket.custom.toml")
}
/// Sets the address of `self` to `address`. /// Sets the address of `self` to `address`.
/// ///
/// # Errors /// # Errors

View File

@ -33,15 +33,13 @@ impl Environment {
} }
/// Returns a string with a comma-seperated list of valid environments. /// Returns a string with a comma-seperated list of valid environments.
#[doc(hidden)] pub(crate) fn valid() -> &'static str {
pub fn valid() -> &'static str {
"development, staging, production" "development, staging, production"
} }
/// Returns a list of all of the possible environments. /// Returns a list of all of the possible environments.
#[inline] #[inline]
#[doc(hidden)] pub(crate) fn all() -> [Environment; 3] {
pub fn all() -> [Environment; 3] {
[Development, Staging, Production] [Development, Staging, Production]
} }
} }

View File

@ -420,8 +420,7 @@ impl RocketConfig {
/// # Panics /// # Panics
/// ///
/// If there is a problem, prints a nice error message and bails. /// If there is a problem, prints a nice error message and bails.
#[doc(hidden)] pub(crate) fn init() -> (&'static Config, bool) {
pub fn init() -> (&'static Config, bool) {
let mut this_init = false; let mut this_init = false;
unsafe { unsafe {
INIT.call_once(|| { INIT.call_once(|| {
@ -433,8 +432,7 @@ pub fn init() -> (&'static Config, bool) {
} }
} }
#[doc(hidden)] pub(crate) fn custom_init(config: Config) -> (&'static Config, bool) {
pub fn custom_init(config: Config) -> (&'static Config, bool) {
let mut this_init = false; let mut this_init = false;
unsafe { unsafe {

View File

@ -82,8 +82,7 @@ impl Data {
DataStream::new(stream, network) DataStream::new(stream, network)
} }
#[doc(hidden)] pub(crate) fn from_hyp(mut h_body: BodyReader) -> Result<Data, &'static str> {
pub fn from_hyp(mut h_body: BodyReader) -> Result<Data, &'static str> {
// FIXME: This is asolutely terrible, thanks to Hyper. // FIXME: This is asolutely terrible, thanks to Hyper.
// Retrieve the underlying HTTPStream from Hyper. // Retrieve the underlying HTTPStream from Hyper.
@ -151,8 +150,7 @@ impl Data {
// Creates a new data object with an internal buffer `buf`, where the cursor // Creates a new data object with an internal buffer `buf`, where the cursor
// in the buffer is at `pos` and the buffer has `cap` valid bytes. The // in the buffer is at `pos` and the buffer has `cap` valid bytes. The
// remainder of the data bytes can be read from `stream`. // remainder of the data bytes can be read from `stream`.
#[doc(hidden)] pub(crate) fn new(mut buf: Vec<u8>,
pub fn new(mut buf: Vec<u8>,
pos: usize, pos: usize,
mut cap: usize, mut cap: usize,
mut stream: StreamReader) mut stream: StreamReader)

View File

@ -19,8 +19,7 @@ pub struct DataStream {
} }
impl DataStream { impl DataStream {
#[doc(hidden)] pub(crate) fn new(stream: InnerStream, network: HttpStream) -> DataStream {
pub fn new(stream: InnerStream, network: HttpStream) -> DataStream {
DataStream { stream: stream, network: network, } DataStream { stream: stream, network: network, }
} }
} }

View File

@ -40,28 +40,27 @@ impl Data {
DataStream { stream: BufReader::new(Cursor::new(self.data)) } DataStream { stream: BufReader::new(Cursor::new(self.data)) }
} }
#[inline(always)] #[inline]
pub fn peek(&self) -> &[u8] { pub fn peek(&self) -> &[u8] {
&self.data[..::std::cmp::min(PEEK_BYTES, self.data.len())] &self.data[..::std::cmp::min(PEEK_BYTES, self.data.len())]
} }
#[inline(always)] #[inline]
pub fn peek_complete(&self) -> bool { pub fn peek_complete(&self) -> bool {
self.data.len() <= PEEK_BYTES self.data.len() <= PEEK_BYTES
} }
#[inline(always)] #[inline]
pub fn stream_to<W: Write>(self, writer: &mut W) -> io::Result<u64> { pub fn stream_to<W: Write>(self, writer: &mut W) -> io::Result<u64> {
io::copy(&mut self.open(), writer) io::copy(&mut self.open(), writer)
} }
#[inline(always)] #[inline]
pub fn stream_to_file<P: AsRef<Path>>(self, path: P) -> io::Result<u64> { pub fn stream_to_file<P: AsRef<Path>>(self, path: P) -> io::Result<u64> {
io::copy(&mut self.open(), &mut File::create(path)?) io::copy(&mut self.open(), &mut File::create(path)?)
} }
#[doc(hidden)] pub(crate) fn from_hyp(mut h_body: BodyReader) -> Result<Data, &'static str> {
pub fn from_hyp(mut h_body: BodyReader) -> Result<Data, &'static str> {
let mut vec = Vec::new(); let mut vec = Vec::new();
if let Err(_) = io::copy(&mut h_body, &mut vec) { if let Err(_) = io::copy(&mut h_body, &mut vec) {
return Err("Reading from body failed."); return Err("Reading from body failed.");
@ -70,8 +69,7 @@ impl Data {
Ok(Data::new(vec)) Ok(Data::new(vec))
} }
#[doc(hidden)] pub(crate) fn new(data: Vec<u8>) -> Data {
pub fn new(data: Vec<u8>) -> Data {
Data { data: data } Data { data: data }
} }
} }

View File

@ -502,9 +502,8 @@ impl<'h> HeaderMap<'h> {
/// Consumes `self` and returns an iterator over all of the headers stored /// Consumes `self` and returns an iterator over all of the headers stored
/// in the map in the way they are stored. This is a low-level machinism and /// in the map in the way they are stored. This is a low-level machinism and
/// should likely not be used. /// should likely not be used.
#[doc(hidden)] #[inline]
#[inline(always)] pub(crate) fn into_iter_raw(self)
pub fn into_iter_raw(self)
-> impl Iterator<Item=(UncasedAscii<'h>, Vec<Cow<'h, str>>)> { -> impl Iterator<Item=(UncasedAscii<'h>, Vec<Cow<'h, str>>)> {
self.headers.into_iter() self.headers.into_iter()
} }

View File

@ -40,11 +40,6 @@ impl Method {
} }
} }
#[inline]
pub(crate) fn to_hyp(&self) -> hyper::Method {
self.to_string().as_str().parse().unwrap()
}
/// Returns `true` ff an HTTP request with the method represented by `self` /// Returns `true` ff an HTTP request with the method represented by `self`
/// supports a payload. /// supports a payload.
/// ///

View File

@ -235,8 +235,8 @@ impl Status {
/// Returns a status from a given status code. If the status code is a /// Returns a status from a given status code. If the status code is a
/// standard code, then the reason phrase is populated accordingly. /// standard code, then the reason phrase is populated accordingly.
/// Otherwise the reason phrase is set to "<unknown code>". /// Otherwise the reason phrase is set to "<unknown code>".
#[doc(hidden)]
#[inline] #[inline]
#[doc(hidden)]
pub fn raw(code: u16) -> Status { pub fn raw(code: u16) -> Status {
match Status::from_code(code) { match Status::from_code(code) {
Some(status) => status, Some(status) => status,

View File

@ -124,7 +124,7 @@ impl<S, E, F> Outcome<S, E, F> {
/// let x: Outcome<i32, &str, usize> = Success(10); /// let x: Outcome<i32, &str, usize> = Success(10);
/// assert_eq!(x.unwrap(), 10); /// assert_eq!(x.unwrap(), 10);
/// ``` /// ```
#[inline(always)] #[inline]
pub fn unwrap(self) -> S { pub fn unwrap(self) -> S {
match self { match self {
Success(val) => val, Success(val) => val,
@ -147,7 +147,7 @@ impl<S, E, F> Outcome<S, E, F> {
/// let x: Outcome<i32, &str, usize> = Success(10); /// let x: Outcome<i32, &str, usize> = Success(10);
/// assert_eq!(x.expect("success value"), 10); /// assert_eq!(x.expect("success value"), 10);
/// ``` /// ```
#[inline(always)] #[inline]
pub fn expect(self, message: &str) -> S { pub fn expect(self, message: &str) -> S {
match self { match self {
Success(val) => val, Success(val) => val,
@ -172,7 +172,7 @@ impl<S, E, F> Outcome<S, E, F> {
/// let x: Outcome<i32, &str, usize> = Forward(25); /// let x: Outcome<i32, &str, usize> = Forward(25);
/// assert_eq!(x.is_success(), false); /// assert_eq!(x.is_success(), false);
/// ``` /// ```
#[inline(always)] #[inline]
pub fn is_success(&self) -> bool { pub fn is_success(&self) -> bool {
match *self { match *self {
Success(_) => true, Success(_) => true,
@ -197,7 +197,7 @@ impl<S, E, F> Outcome<S, E, F> {
/// let x: Outcome<i32, &str, usize> = Forward(25); /// let x: Outcome<i32, &str, usize> = Forward(25);
/// assert_eq!(x.is_failure(), false); /// assert_eq!(x.is_failure(), false);
/// ``` /// ```
#[inline(always)] #[inline]
pub fn is_failure(&self) -> bool { pub fn is_failure(&self) -> bool {
match *self { match *self {
Failure(_) => true, Failure(_) => true,
@ -222,7 +222,7 @@ impl<S, E, F> Outcome<S, E, F> {
/// let x: Outcome<i32, &str, usize> = Forward(25); /// let x: Outcome<i32, &str, usize> = Forward(25);
/// assert_eq!(x.is_forward(), true); /// assert_eq!(x.is_forward(), true);
/// ``` /// ```
#[inline(always)] #[inline]
pub fn is_forward(&self) -> bool { pub fn is_forward(&self) -> bool {
match *self { match *self {
Forward(_) => true, Forward(_) => true,
@ -248,7 +248,7 @@ impl<S, E, F> Outcome<S, E, F> {
/// let x: Outcome<i32, &str, usize> = Forward(25); /// let x: Outcome<i32, &str, usize> = Forward(25);
/// assert_eq!(x.succeeded(), None); /// assert_eq!(x.succeeded(), None);
/// ``` /// ```
#[inline(always)] #[inline]
pub fn succeeded(self) -> Option<S> { pub fn succeeded(self) -> Option<S> {
match self { match self {
Success(val) => Some(val), Success(val) => Some(val),
@ -274,7 +274,7 @@ impl<S, E, F> Outcome<S, E, F> {
/// let x: Outcome<i32, &str, usize> = Forward(25); /// let x: Outcome<i32, &str, usize> = Forward(25);
/// assert_eq!(x.failed(), None); /// assert_eq!(x.failed(), None);
/// ``` /// ```
#[inline(always)] #[inline]
pub fn failed(self) -> Option<E> { pub fn failed(self) -> Option<E> {
match self { match self {
Failure(val) => Some(val), Failure(val) => Some(val),
@ -300,7 +300,7 @@ impl<S, E, F> Outcome<S, E, F> {
/// let x: Outcome<i32, &str, usize> = Forward(25); /// let x: Outcome<i32, &str, usize> = Forward(25);
/// assert_eq!(x.forwarded(), Some(25)); /// assert_eq!(x.forwarded(), Some(25));
/// ``` /// ```
#[inline(always)] #[inline]
pub fn forwarded(self) -> Option<F> { pub fn forwarded(self) -> Option<F> {
match self { match self {
Forward(val) => Some(val), Forward(val) => Some(val),
@ -320,7 +320,7 @@ impl<S, E, F> Outcome<S, E, F> {
/// let x: Outcome<i32, &str, usize> = Failure("Hi! I'm an error."); /// let x: Outcome<i32, &str, usize> = Failure("Hi! I'm an error.");
/// assert_eq!(x.as_ref(), Failure(&"Hi! I'm an error.")); /// assert_eq!(x.as_ref(), Failure(&"Hi! I'm an error."));
/// ``` /// ```
#[inline(always)] #[inline]
pub fn as_ref(&self) -> Outcome<&S, &E, &F> { pub fn as_ref(&self) -> Outcome<&S, &E, &F> {
match *self { match *self {
Success(ref val) => Success(val), Success(ref val) => Success(val),
@ -342,7 +342,7 @@ impl<S, E, F> Outcome<S, E, F> {
/// ///
/// assert_eq!(x.unwrap(), 20); /// assert_eq!(x.unwrap(), 20);
/// ``` /// ```
#[inline(always)] #[inline]
pub fn as_mut(&mut self) -> Outcome<&mut S, &mut E, &mut F> { pub fn as_mut(&mut self) -> Outcome<&mut S, &mut E, &mut F> {
match *self { match *self {
Success(ref mut val) => Success(val), Success(ref mut val) => Success(val),
@ -351,8 +351,7 @@ impl<S, E, F> Outcome<S, E, F> {
} }
} }
#[doc(hidden)] #[inline]
#[inline(always)]
fn formatting(&self) -> (Color, &'static str) { fn formatting(&self) -> (Color, &'static str) {
match *self { match *self {
Success(..) => (Green, "Success"), Success(..) => (Green, "Success"),

View File

@ -257,9 +257,8 @@ impl<'r> Request<'r> {
} }
/// Replace all of the cookies in `self` with `cookies`. /// Replace all of the cookies in `self` with `cookies`.
#[doc(hidden)] #[inline]
#[inline(always)] pub(crate) fn set_cookies(&mut self, cookies: Cookies) {
pub fn set_cookies(&mut self, cookies: Cookies) {
self.cookies = cookies; self.cookies = cookies;
} }
@ -315,9 +314,8 @@ impl<'r> Request<'r> {
/// was `route`. This should only be used internally by `Rocket` as improper /// was `route`. This should only be used internally by `Rocket` as improper
/// use may result in out of bounds indexing. /// use may result in out of bounds indexing.
/// TODO: Figure out the mount path from here. /// TODO: Figure out the mount path from here.
#[doc(hidden)] #[inline]
#[inline(always)] pub(crate) fn set_params(&self, route: &Route) {
pub fn set_params(&self, route: &Route) {
*self.params.borrow_mut() = route.get_param_indexes(self.uri()); *self.params.borrow_mut() = route.get_param_indexes(self.uri());
} }
@ -386,24 +384,23 @@ impl<'r> Request<'r> {
} }
/// Get the managed state container, if it exists. For internal use only! /// Get the managed state container, if it exists. For internal use only!
#[doc(hidden)] #[inline]
pub fn get_state(&self) -> Option<&'r Container> { pub(crate) fn get_state(&self) -> Option<&'r Container> {
self.state self.state
} }
/// Set the state. For internal use only! /// Set the state. For internal use only!
#[doc(hidden)] #[inline]
pub fn set_state(&mut self, state: &'r Container) { pub(crate) fn set_state(&mut self, state: &'r Container) {
self.state = Some(state); self.state = Some(state);
} }
/// Convert from Hyper types into a Rocket Request. /// Convert from Hyper types into a Rocket Request.
#[doc(hidden)] pub(crate) fn from_hyp(h_method: hyper::Method,
pub fn from_hyp(h_method: hyper::Method, h_headers: hyper::header::Headers,
h_headers: hyper::header::Headers, h_uri: hyper::RequestUri,
h_uri: hyper::RequestUri, h_addr: SocketAddr,
h_addr: SocketAddr, ) -> Result<Request<'r>, String> {
) -> Result<Request<'r>, String> {
// Get a copy of the URI for later use. // Get a copy of the URI for later use.
let uri = match h_uri { let uri = match h_uri {
hyper::RequestUri::AbsolutePath(s) => s, hyper::RequestUri::AbsolutePath(s) => s,

View File

@ -876,9 +876,8 @@ impl<'r> Response<'r> {
// Makes the `Read`er in the body empty but leaves the size of the body if // Makes the `Read`er in the body empty but leaves the size of the body if
// it exists. Only meant to be used to handle HEAD requests automatically. // it exists. Only meant to be used to handle HEAD requests automatically.
#[doc(hidden)]
#[inline(always)] #[inline(always)]
pub fn strip_body(&mut self) { pub(crate) fn strip_body(&mut self) {
if let Some(body) = self.take_body() { if let Some(body) = self.take_body() {
self.body = match body { self.body = match body {
Body::Sized(_, n) => Some(Body::Sized(Box::new(io::empty()), n)), Body::Sized(_, n) => Some(Body::Sized(Box::new(io::empty()), n)),

View File

@ -176,9 +176,8 @@ impl Rocket {
} }
} }
#[doc(hidden)] #[inline]
#[inline(always)] pub(crate) fn dispatch<'s, 'r>(&'s self, request: &'r mut Request<'s>, data: Data)
pub fn dispatch<'s, 'r>(&'s self, request: &'r mut Request<'s>, data: Data)
-> Response<'r> { -> Response<'r> {
info!("{}:", request); info!("{}:", request);
@ -229,9 +228,8 @@ impl Rocket {
/// until one of the handlers returns success or failure, or there are no /// until one of the handlers returns success or failure, or there are no
/// additional routes to try (forward). The corresponding outcome for each /// additional routes to try (forward). The corresponding outcome for each
/// condition is returned. /// condition is returned.
#[doc(hidden)] #[inline]
#[inline(always)] pub(crate) fn route<'r>(&self, request: &'r Request, mut data: Data)
pub fn route<'r>(&self, request: &'r Request, mut data: Data)
-> handler::Outcome<'r> { -> handler::Outcome<'r> {
// Go through the list of matching routes until we fail or succeed. // Go through the list of matching routes until we fail or succeed.
let matches = self.router.route(request); let matches = self.router.route(request);
@ -258,8 +256,7 @@ impl Rocket {
} }
// TODO: DOC. // TODO: DOC.
#[doc(hidden)] fn handle_error<'r>(&self, status: Status, req: &'r Request) -> Response<'r> {
pub fn handle_error<'r>(&self, status: Status, req: &'r Request) -> Response<'r> {
warn_!("Responding with {} catcher.", Red.paint(&status)); warn_!("Responding with {} catcher.", Red.paint(&status));
// Try to get the active catcher but fallback to user's 500 catcher. // Try to get the active catcher but fallback to user's 500 catcher.

View File

@ -70,8 +70,7 @@ impl Route {
// TODO: Don't return a Vec...take in an &mut [&'a str] (no alloc!) // TODO: Don't return a Vec...take in an &mut [&'a str] (no alloc!)
/// Given a URI, returns a vector of slices of that URI corresponding to the /// Given a URI, returns a vector of slices of that URI corresponding to the
/// dynamic segments in this route. /// dynamic segments in this route.
#[doc(hidden)] pub(crate) fn get_param_indexes(&self, uri: &URI) -> Vec<(usize, usize)> {
pub fn get_param_indexes(&self, uri: &URI) -> Vec<(usize, usize)> {
let route_segs = self.path.segments(); let route_segs = self.path.segments();
let uri_segs = uri.segments(); let uri_segs = uri.segments();
let start_addr = uri.path().as_ptr() as usize; let start_addr = uri.path().as_ptr() as usize;