Skip to content

Url API changes #472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
6 changes: 3 additions & 3 deletions examples/auth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn init(url: Url, orders: &mut impl Orders<Msg>) -> Model {
Model {
email: "[email protected]".to_owned(),
password: "1234".to_owned(),
base_url: url.to_base_url(),
base_url: url.clone().truncate_relative_path(),
page: Page::init(url, user.as_ref(), orders),
secret_message: None,
user,
Expand Down Expand Up @@ -59,7 +59,7 @@ enum Page {

impl Page {
fn init(mut url: Url, user: Option<&LoggedUser>, orders: &mut impl Orders<Msg>) -> Self {
match url.next_path_part() {
match url.pop_relative_path_part() {
None => {
if let Some(user) = user {
send_request_to_top_secret(user.token.clone(), orders)
Expand Down Expand Up @@ -99,7 +99,7 @@ impl<'a> Urls<'a> {
self.base_url()
}
pub fn login(self) -> Url {
self.base_url().add_path_part(LOGIN)
self.base_url().push_path_part(LOGIN)
}
}

Expand Down
6 changes: 3 additions & 3 deletions examples/pages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn init(url: Url, orders: &mut impl Orders<Msg>) -> Model {
ctx: Context {
logged_user: "John Doe",
},
base_url: url.to_base_url(),
base_url: url.clone().truncate_relative_path(),
page: Page::init(url),
}
}
Expand Down Expand Up @@ -47,7 +47,7 @@ enum Page {

impl Page {
fn init(mut url: Url) -> Self {
match url.next_path_part() {
match url.pop_relative_path_part() {
None => Self::Home,
Some(ADMIN) => page::admin::init(url).map_or(Self::NotFound, Self::Admin),
_ => Self::NotFound,
Expand All @@ -65,7 +65,7 @@ impl<'a> Urls<'a> {
self.base_url()
}
pub fn admin_urls(self) -> page::admin::Urls<'a> {
page::admin::Urls::new(self.base_url().add_path_part(ADMIN))
page::admin::Urls::new(self.base_url().push_path_part(ADMIN))
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/pages/src/page/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod page;

pub fn init(mut url: Url) -> Option<Model> {
Some(Model {
report_page: match url.next_path_part() {
report_page: match url.pop_relative_path_part() {
Some(REPORT) => page::report::init(url)?,
_ => None?,
},
Expand All @@ -33,7 +33,7 @@ pub struct Model {
struct_urls!();
impl<'a> Urls<'a> {
pub fn report_urls(self) -> page::report::Urls<'a> {
page::report::Urls::new(self.base_url().add_path_part(REPORT))
page::report::Urls::new(self.base_url().push_path_part(REPORT))
}
}

Expand Down
8 changes: 4 additions & 4 deletions examples/pages/src/page/admin/page/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const WEEKLY: &str = "weekly";
// ------ ------

pub fn init(mut url: Url) -> Option<Model> {
let base_url = url.to_base_url();
let base_url = url.clone().truncate_relative_path();

let frequency = match url.remaining_path_parts().as_slice() {
let frequency = match url.consume_relative_path().as_slice() {
[] => {
Urls::new(&base_url).default().go_and_replace();
Frequency::default()
Expand Down Expand Up @@ -59,10 +59,10 @@ impl<'a> Urls<'a> {
self.daily()
}
pub fn daily(self) -> Url {
self.base_url().add_path_part(DAILY)
self.base_url().push_path_part(DAILY)
}
pub fn weekly(self) -> Url {
self.base_url().add_path_part(WEEKLY)
self.base_url().push_path_part(WEEKLY)
}
}

Expand Down
6 changes: 3 additions & 3 deletions examples/pages_hash_routing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn init(url: Url, orders: &mut impl Orders<Msg>) -> Model {
ctx: Context {
logged_user: "John Doe",
},
base_url: url.to_hash_base_url(),
base_url: url.clone().truncate_relative_hash_path(),
page: Page::init(url),
}
}
Expand Down Expand Up @@ -47,7 +47,7 @@ enum Page {

impl Page {
fn init(mut url: Url) -> Self {
match url.next_hash_path_part() {
match url.pop_relative_hash_path_part() {
None => Self::Home,
Some(ADMIN) => page::admin::init(url).map_or(Self::NotFound, Self::Admin),
_ => Self::NotFound,
Expand All @@ -65,7 +65,7 @@ impl<'a> Urls<'a> {
self.base_url()
}
pub fn admin_urls(self) -> page::admin::Urls<'a> {
page::admin::Urls::new(self.base_url().add_hash_path_part(ADMIN))
page::admin::Urls::new(self.base_url().push_hash_path_part(ADMIN))
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/pages_hash_routing/src/page/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod page;

pub fn init(mut url: Url) -> Option<Model> {
Some(Model {
report_page: match url.next_hash_path_part() {
report_page: match url.pop_relative_hash_path_part() {
Some(REPORT) => page::report::init(url)?,
_ => None?,
},
Expand All @@ -33,7 +33,7 @@ pub struct Model {
struct_urls!();
impl<'a> Urls<'a> {
pub fn report_urls(self) -> page::report::Urls<'a> {
page::report::Urls::new(self.base_url().add_hash_path_part(REPORT))
page::report::Urls::new(self.base_url().push_hash_path_part(REPORT))
}
}

Expand Down
8 changes: 4 additions & 4 deletions examples/pages_hash_routing/src/page/admin/page/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const WEEKLY: &str = "weekly";
// ------ ------

pub fn init(mut url: Url) -> Option<Model> {
let base_url = url.to_hash_base_url();
let base_url = url.clone().truncate_relative_hash_path();

let frequency = match url.remaining_hash_path_parts().as_slice() {
let frequency = match url.consume_relative_hash_path().as_slice() {
[] => {
Urls::new(&base_url).default().go_and_replace();
Frequency::default()
Expand Down Expand Up @@ -59,10 +59,10 @@ impl<'a> Urls<'a> {
self.daily()
}
pub fn daily(self) -> Url {
self.base_url().add_hash_path_part(DAILY)
self.base_url().push_hash_path_part(DAILY)
}
pub fn weekly(self) -> Url {
self.base_url().add_hash_path_part(WEEKLY)
self.base_url().push_hash_path_part(WEEKLY)
}
}

Expand Down
9 changes: 4 additions & 5 deletions examples/pages_keep_state/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ const ADMIN: &str = "admin";
// ------ ------

fn init(url: Url, orders: &mut impl Orders<Msg>) -> Model {
let base_url = url.to_base_url();
orders
.subscribe(Msg::UrlChanged)
.notify(subs::UrlChanged(url));
.notify(subs::UrlChanged(url.clone()));

Model {
ctx: Context {
logged_user: "John Doe",
},
base_url,
base_url: url.truncate_relative_path(),
page_id: None,
admin_model: None,
}
Expand Down Expand Up @@ -61,7 +60,7 @@ impl<'a> Urls<'a> {
self.base_url()
}
pub fn admin_urls(self) -> page::admin::Urls<'a> {
page::admin::Urls::new(self.base_url().add_path_part(ADMIN))
page::admin::Urls::new(self.base_url().push_path_part(ADMIN))
}
}

Expand All @@ -76,7 +75,7 @@ enum Msg {
fn update(msg: Msg, model: &mut Model, _: &mut impl Orders<Msg>) {
match msg {
Msg::UrlChanged(subs::UrlChanged(mut url)) => {
model.page_id = match url.next_path_part() {
model.page_id = match url.pop_relative_path_part() {
None => Some(PageId::Home),
Some(ADMIN) => {
page::admin::init(url, &mut model.admin_model).map(|_| PageId::Admin)
Expand Down
4 changes: 2 additions & 2 deletions examples/pages_keep_state/src/page/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod page;

pub fn init(mut url: Url, model: &mut Option<Model>) -> Option<()> {
let model = model.get_or_insert_with(Model::default);
model.page_id.replace(match url.next_path_part() {
model.page_id.replace(match url.pop_relative_path_part() {
Some(REPORT) => page::report::init(url, &mut model.report_model).map(|_| PageId::Report)?,
_ => None?,
});
Expand Down Expand Up @@ -42,7 +42,7 @@ enum PageId {
struct_urls!();
impl<'a> Urls<'a> {
pub fn report_urls(self) -> page::report::Urls<'a> {
page::report::Urls::new(self.base_url().add_path_part(REPORT))
page::report::Urls::new(self.base_url().push_path_part(REPORT))
}
}

Expand Down
8 changes: 4 additions & 4 deletions examples/pages_keep_state/src/page/admin/page/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ const WEEKLY: &str = "weekly";

pub fn init(mut url: Url, model: &mut Option<Model>) -> Option<()> {
let model = model.get_or_insert_with(|| Model {
base_url: url.to_base_url(),
base_url: url.clone().truncate_relative_path(),
frequency: Frequency::Daily,
});

model.frequency = match url.remaining_path_parts().as_slice() {
model.frequency = match url.consume_relative_path().as_slice() {
[] => {
match model.frequency {
Frequency::Daily => Urls::new(&model.base_url).daily().go_and_replace(),
Expand Down Expand Up @@ -56,10 +56,10 @@ impl<'a> Urls<'a> {
self.base_url()
}
pub fn daily(self) -> Url {
self.base_url().add_path_part(DAILY)
self.base_url().push_path_part(DAILY)
}
pub fn weekly(self) -> Url {
self.base_url().add_path_part(WEEKLY)
self.base_url().push_path_part(WEEKLY)
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/todomvc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
let data = &mut model.data;
match msg {
Msg::UrlChanged(subs::UrlChanged(mut url)) => {
data.filter = match url.next_path_part() {
data.filter = match url.pop_relative_path_part() {
Some(path_part) if path_part == TodoFilter::Active.to_url_path() => {
TodoFilter::Active
}
Expand Down
4 changes: 2 additions & 2 deletions examples/unsaved_changes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn init(url: Url, orders: &mut impl Orders<Msg>) -> Model {

let text = LocalStorage::get(STORAGE_KEY).unwrap_or_default();
Model {
base_url: url.to_base_url(),
base_url: url.clone().truncate_relative_path(),
saved_text_hash: calculate_hash(&text),
text,
}
Expand All @@ -44,7 +44,7 @@ impl<'a> Urls<'a> {
self.base_url()
}
fn no_home(self) -> Url {
self.base_url().add_path_part("no-home")
self.base_url().push_path_part("no-home")
}
}

Expand Down
10 changes: 5 additions & 5 deletions examples/url/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ impl Model {
Self {
base_path,
initial_url: url.clone(),
base_url: url.to_base_url(),
next_path_part: url.next_path_part().map(ToOwned::to_owned),
base_url: url.clone().truncate_relative_path(),
next_path_part: url.pop_relative_path_part().map(ToOwned::to_owned),
remaining_path_parts: url
.remaining_path_parts()
.consume_relative_path()
.into_iter()
.map(ToOwned::to_owned)
.collect(),
Expand Down Expand Up @@ -75,9 +75,9 @@ fn view(model: &Model) -> Node<Msg> {
ev(Ev::Click, |_| {
Url::new()
.set_path(&["ui", "a", "b", "c"])
.set_search(UrlSearch::new(vec![
.set_search(vec![
("x", vec!["1"])
]))
].into_iter().collect())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if it's better or worse: two calls + hidden type VS explicit type UrlSearch that needs to be imported.
Do you think we can write only .set_search(vec![("x", vec!["1"])]) or set_search(UrlSearch::new..) and don't complicate set_search parameter too much?

.set_hash("hash")
.go_and_load()
})
Expand Down
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl<Ms, Mdl, INodes: IntoNodes<Ms> + 'static, GMs: 'static> App<Ms, Mdl, INodes
move |url: Url,
orders: &mut OrdersContainer<Ms, Mdl, INodes, GMs>|
-> AfterMount<Mdl> {
let url = url.skip_base_path(&base_path);
let url = url.try_skip_base_path(&base_path);
let model = init(url, orders);
AfterMount::new(model).url_handling(UrlHandling::None)
}
Expand Down
6 changes: 3 additions & 3 deletions src/browser/service/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub fn setup_popstate_listener<Ms>(
};

notify(Notification::new(subs::UrlChanged(
url.clone().skip_base_path(&base_path),
url.clone().try_skip_base_path(&base_path),
)));

if let Some(routes) = routes {
Expand Down Expand Up @@ -85,7 +85,7 @@ pub fn setup_hashchange_listener<Ms>(
.expect("cast hashchange event url to `Url`");

notify(Notification::new(subs::UrlChanged(
url.clone().skip_base_path(&base_path),
url.clone().try_skip_base_path(&base_path),
)));

if let Some(routes) = routes {
Expand Down Expand Up @@ -117,7 +117,7 @@ pub(crate) fn url_request_handler(
event.prevent_default(); // Prevent page refresh
}
notify(Notification::new(subs::UrlChanged(
url.skip_base_path(&base_path),
url.try_skip_base_path(&base_path),
)));
}
subs::url_requested::UrlRequestStatus::Handled(prevent_default) => {
Expand Down
Loading