..that is just AWFUL, it sounds so demoralising. The company has behaved so unprofessionally towards you it's just appalling.
I got sent home after three hours at a temp job in New York once, it took me ages to get up the confidence to go back into another office, and that was only a summer temp thing.
Like you say this could all be for a reason, you have the prospect of an interview for the library job, and when you are looking for other jobs just remember all the nice stuff that the guy at this job said about you, that should have you standing tall.